just/src/loader.rs

35 lines
661 B
Rust
Raw Normal View History

use super::*;
pub(crate) struct Loader {
srcs: Arena<String>,
paths: Arena<PathBuf>,
}
impl Loader {
pub(crate) fn new() -> Self {
Loader {
srcs: Arena::new(),
paths: Arena::new(),
}
}
pub(crate) fn load<'src>(
&'src self,
root: &Path,
path: &Path,
) -> RunResult<(&'src Path, &'src str)> {
let src = fs::read_to_string(path).map_err(|io_error| Error::Load {
path: path.to_owned(),
io_error,
})?;
2023-01-12 19:25:28 -08:00
let relative = if let Ok(path) = path.strip_prefix(root.parent().unwrap()) {
path
} else {
path
};
Ok((self.paths.alloc(relative.into()), self.srcs.alloc(src)))
}
}