use std::fs;
use std::path::Path;
use crate::manifest_resolver::ManifestResolver;
use crate::offence::Offence;
use crate::rules::readable_source_rule::ReadableSourceRule;
use crate::source_file::SourceFile;
pub struct SourceReader;
impl SourceReader {
pub fn read(root: &Path, path: &Path) -> Result<SourceFile, Box<Offence>> {
let relative = ManifestResolver::relative_to(root, path);
match fs::read_to_string(path) {
Ok(contents) => Ok(SourceFile::new(&relative, &contents)),
Err(error) => Err(Box::new(
Offence::new(
&relative,
1,
ReadableSourceRule::NAME,
format!("file could not be read: {error}"),
"check that the file exists and that its permissions allow reading it"
.to_string(),
)
.with_subject(&relative),
)),
}
}
}