use crate::{SourceID, SourcePath, SourceText, Url};
use std::{borrow::Cow, collections::HashMap, path::Path};
mod display;
#[derive(Default, Debug, Clone)]
pub struct SourceCache {
files: HashMap<SourceID, SourceText>,
}
impl SourceCache {
pub fn load_local<P>(&mut self, path: P) -> Result<SourceID, std::io::Error>
where
P: AsRef<Path>,
{
let path = path.as_ref();
let text = std::fs::read_to_string(&path)?;
let source = SourceText::from(text).with_path(path);
let name_hash = source.source_id();
self.files.insert(name_hash, source);
Ok(name_hash)
}
pub fn load_remote(&mut self, url: Url) -> Result<SourceID, std::io::Error> {
let path = url.as_ref();
let text = std::fs::read_to_string(&path)?;
let source = SourceText::from(text).with_remote(url);
let name_hash = source.source_id();
self.files.insert(name_hash, source);
Ok(name_hash)
}
pub fn load_text<T, N>(&mut self, text: T, name: N) -> SourceID
where
T: ToString,
N: ToString,
{
let source = SourceText::snippet(text.to_string(), name.to_string());
let name_hash = source.source_id();
self.files.insert(name_hash, source);
name_hash
}
pub unsafe fn set_source<N>(&mut self, file: SourceID, source: N) -> bool
where
N: Into<Cow<'static, str>>,
{
match self.files.get_mut(&file) {
Some(s) => {
s.set_source(SourcePath::Snippet(source.into()));
true
}
None => false,
}
}
pub fn fetch(&self, file: &SourceID) -> Result<&SourceText, std::io::Error> {
match self.files.get(file) {
Some(source) => Ok(source),
None => Err(std::io::Error::new(std::io::ErrorKind::NotFound, format!("File {:?} not found", file))),
}
}
pub fn source_path(&self, file: &SourceID) -> Option<&SourcePath> {
Some(&self.files.get(file)?.get_source())
}
}