source_cache/cache/
mod.rs

1use crate::{SourceID, SourcePath, SourceText, Url};
2use std::{borrow::Cow, collections::HashMap, path::Path};
3mod display;
4
5/// A [`Cache`] that fetches [`SourceText`]s from the filesystem.
6#[derive(Default, Debug, Clone)]
7pub struct SourceCache {
8    cache: HashMap<SourceID, SourceText>,
9}
10
11impl SourceCache {
12    /// Create a new [`SourceCache`].
13    pub fn load_local<P>(&mut self, path: P) -> Result<SourceID, std::io::Error>
14    where
15        P: AsRef<Path>,
16    {
17        let path = path.as_ref();
18        let text = std::fs::read_to_string(&path)?;
19        let source = SourceText::from(text).with_path(path);
20        let name_hash = source.source_id();
21        self.cache.insert(name_hash, source);
22        Ok(name_hash)
23    }
24    /// Create a new [`SourceCache`].
25    pub fn load_remote(&mut self, url: Url) -> Result<SourceID, std::io::Error> {
26        let path = url.as_ref();
27        let text = std::fs::read_to_string(&path)?;
28        let source = SourceText::from(text).with_remote(url);
29        let name_hash = source.source_id();
30        self.cache.insert(name_hash, source);
31        Ok(name_hash)
32    }
33
34    /// Create a new [`SourceCache`].
35    pub fn load_text<T, N>(&mut self, text: T, name: N) -> SourceID
36    where
37        T: ToString,
38        N: ToString,
39    {
40        let source = SourceText::snippet(text.to_string(), name.to_string());
41        let name_hash = source.source_id();
42        self.cache.insert(name_hash, source);
43        name_hash
44    }
45    /// Set the file identifier buy not update the context
46    pub unsafe fn set_source<N>(&mut self, file: SourceID, source: N) -> bool
47    where
48        N: Into<Cow<'static, str>>,
49    {
50        match self.cache.get_mut(&file) {
51            Some(s) => {
52                s.set_source(SourcePath::Snippet(source.into()));
53                true
54            }
55            None => false,
56        }
57    }
58    /// Create a new [`SourceCache`].
59    pub fn fetch(&self, file: &SourceID) -> Result<&SourceText, std::io::Error> {
60        match self.cache.get(file) {
61            Some(source) => Ok(source),
62            None => Err(std::io::Error::new(std::io::ErrorKind::NotFound, format!("File {:?} not found", file))),
63        }
64    }
65    /// Create a new [`SourceCache`].
66    pub fn source_path(&self, file: &SourceID) -> Option<&SourcePath> {
67        Some(&self.cache.get(file)?.get_source())
68    }
69}