1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
use crate::SourceSpan;
use std::{
    borrow::Cow,
    fmt::{Debug, Display, Formatter},
    hash::{DefaultHasher, Hash, Hasher},
    ops::Range,
    path::PathBuf,
};
use url::Url;

mod display;

#[derive(Clone, Debug, Default, Hash, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum SourcePath {
    /// This is an anonymous identifier
    #[default]
    Anonymous,
    /// This is a snippet of identifier
    Snippet(Cow<'static, str>),
    /// This is a local identifier
    Local(PathBuf),
    /// This is a remote identifier
    Remote(Url),
}

/// A type representing a single line of a [`Source`].
#[derive(Copy, Clone, Default, Eq, PartialEq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct SourceID {
    pub(crate) hash: u64,
}

impl SourcePath {
    /// Calculate the file from the identifier
    pub fn source_id(&self) -> SourceID {
        let mut hasher = DefaultHasher::new();
        self.hash(&mut hasher);
        SourceID { hash: hasher.finish() }
    }
}

impl SourceID {
    /// Create a new [`SourceID`] with the given ID.
    pub unsafe fn new(id: u64) -> Self {
        Self { hash: id }
    }

    /// Create a new [`SourceID`] with the given ID.
    pub fn with_range(self, range: Range<u32>) -> SourceSpan {
        SourceSpan { start: range.start, end: range.end, file: self }
    }
}