just/src/loader.rs

35 lines
659 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 {
2024-05-14 20:07:41 -07:00
Self {
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)))
}
}