2022-06-18 21:56:31 -07:00
|
|
|
use super::*;
|
2021-07-26 01:26:06 -07:00
|
|
|
|
|
|
|
pub(crate) struct Loader {
|
2023-11-21 20:17:38 -08:00
|
|
|
srcs: Arena<String>,
|
|
|
|
paths: Arena<PathBuf>,
|
2021-07-26 01:26:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Loader {
|
2023-11-21 11:28:59 -08:00
|
|
|
pub(crate) fn new() -> Self {
|
2021-07-26 01:26:06 -07:00
|
|
|
Loader {
|
2023-11-21 20:17:38 -08:00
|
|
|
srcs: Arena::new(),
|
|
|
|
paths: Arena::new(),
|
2021-07-26 01:26:06 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-21 20:17:38 -08:00
|
|
|
pub(crate) fn load<'src>(
|
|
|
|
&'src self,
|
|
|
|
root: &Path,
|
|
|
|
path: &Path,
|
|
|
|
) -> RunResult<(&'src Path, &'src str)> {
|
2023-11-21 11:28:59 -08:00
|
|
|
let src = fs::read_to_string(path).map_err(|io_error| Error::Load {
|
2021-07-26 01:26:06 -07:00
|
|
|
path: path.to_owned(),
|
|
|
|
io_error,
|
2023-11-21 11:28:59 -08:00
|
|
|
})?;
|
2023-01-12 19:25:28 -08:00
|
|
|
|
2023-11-21 20:17:38 -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)))
|
2021-07-26 01:26:06 -07:00
|
|
|
}
|
|
|
|
}
|