use std::path::{Path, PathBuf};
use yog_script_analyze::macros::ModFiles;
pub struct FilesBeside {
pub base: PathBuf,
}
impl ModFiles for FilesBeside {
fn read(&self, source: &str, path: &str) -> Result<String, String> {
let beside = self
.base
.join(source)
.parent()
.map_or_else(|| self.base.clone(), Path::to_path_buf)
.join(path);
let file = beside.canonicalize().map_err(|e| e.to_string())?;
let base = self.base.canonicalize().map_err(|e| e.to_string())?;
if !file.starts_with(&base) {
return Err(format!("it's outside `{}`", base.display()));
}
std::fs::read_to_string(&file).map_err(|e| e.to_string())
}
}