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
use std::{
    borrow::Cow,
    fmt::{Display, Formatter},
    hash::{DefaultHasher, Hash, Hasher},
    path::PathBuf,
};
use url::Url;

#[derive(Clone, Debug, Hash, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum SourcePath {
    /// This is an anonymous identifier
    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),
}

impl Display for SourcePath {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        todo!()
    }
}

/// A type representing a single line of a [`Source`].
#[derive(Copy, Clone, 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 get_file_id(&self) -> SourceID {
        let mut hasher = DefaultHasher::new();
        self.hash(&mut hasher);
        SourceID { hash: hasher.finish() }
    }
}

impl Default for SourcePath {
    fn default() -> Self {
        Self::Anonymous
    }
}