source_cache/identifier/
mod.rs

1use crate::SourceSpan;
2use std::{
3    borrow::Cow,
4    fmt::{Debug, Display, Formatter},
5    hash::{DefaultHasher, Hash, Hasher},
6    ops::Range,
7    path::PathBuf,
8};
9use url::Url;
10
11mod display;
12
13/// The source path
14#[derive(Clone, Debug, Default, Hash, PartialEq, Eq)]
15#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
16pub enum SourcePath {
17    /// This is an anonymous identifier
18    #[default]
19    Anonymous,
20    /// This is a snippet of identifier
21    Snippet(Cow<'static, str>),
22    /// This is a local identifier
23    Local(PathBuf),
24    /// This is a remote identifier
25    Remote(Url),
26}
27
28/// A type representing a single line of a [`Source`].
29#[derive(Copy, Clone, Default, Eq, PartialEq, Hash)]
30#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
31pub struct SourceID {
32    pub(crate) hash: u64,
33}
34
35impl SourcePath {
36    /// Calculate the file from the identifier
37    pub fn source_id(&self) -> SourceID {
38        let mut hasher = DefaultHasher::new();
39        self.hash(&mut hasher);
40        SourceID { hash: hasher.finish() }
41    }
42}
43
44impl SourceID {
45    /// Create a new [`SourceID`] with the given ID.
46    pub unsafe fn new(id: u64) -> Self {
47        Self { hash: id }
48    }
49
50    /// Create a new [`SourceID`] with the given ID.
51    pub fn with_range(self, range: Range<u32>) -> SourceSpan {
52        SourceSpan { start: range.start, end: range.end, file: self }
53    }
54}