Skip to main content

diffs_rs/highlight/
input.rs

1use super::*;
2use crate::source::CheckedPair;
3
4#[derive(Clone)]
5pub enum FileSource {
6    Patch(Arc<FileIndex>),
7    Plain(Arc<FileIndex>),
8    Complete(Arc<CheckedPair>),
9}
10
11#[derive(Clone, Copy, Debug, PartialEq, Eq)]
12enum Context {
13    Patch,
14    Plain,
15    Complete(u64),
16}
17
18/// Identifies the immutable file and parser context that produced a style.
19#[derive(Clone, Copy, Debug, PartialEq, Eq)]
20pub struct Origin {
21    store: u64,
22    file: usize,
23    context: Context,
24}
25
26impl Origin {
27    #[cfg(test)]
28    pub(super) fn patch(store: u64, file: usize) -> Self {
29        Self {
30            store,
31            file,
32            context: Context::Patch,
33        }
34    }
35}
36
37impl FileSource {
38    pub(crate) fn info(&self, id: crate::view::LineId) -> anyhow::Result<crate::storage::LineInfo> {
39        anyhow::ensure!(
40            id.belongs_to(self.file()),
41            "Style source belongs to another file"
42        );
43        match id {
44            crate::view::LineId::Patch(id) => Ok(self.file().info(id)),
45            crate::view::LineId::Context { old, new, .. } => match self {
46                Self::Complete(pair) => pair.context_info(old.get(), new.get()),
47                _ => anyhow::bail!("Context requires checked source"),
48            },
49        }
50    }
51
52    pub fn file(&self) -> &Arc<FileIndex> {
53        match self {
54            Self::Patch(file) | Self::Plain(file) => file,
55            Self::Complete(pair) => pair.file(),
56        }
57    }
58
59    pub fn origin(&self) -> Origin {
60        Origin {
61            store: self.file().spool.identity(),
62            file: self.file().id,
63            context: match self {
64                Self::Patch(_) => Context::Patch,
65                Self::Plain(_) => Context::Plain,
66                Self::Complete(pair) => Context::Complete(pair.identity),
67            },
68        }
69    }
70}
71
72impl From<Arc<FileIndex>> for FileSource {
73    fn from(file: Arc<FileIndex>) -> Self {
74        Self::Patch(file)
75    }
76}