use crate::{
error::Error, format::CodeStr, parser, path_util::WikiLocation, validator, wiki::Wiki,
};
use similar::TextDiff;
pub fn analyze(location: WikiLocation<'_>, source_contents: &str) -> Result<Wiki, Vec<Error>> {
let wiki = parser::parse(location.display_path(), source_contents)?;
validator::validate(&wiki, location, source_contents)?;
Ok(wiki)
}
pub fn check(location: WikiLocation<'_>, source_contents: &str) -> Result<Wiki, Vec<Error>> {
let wiki = analyze(location, source_contents)?;
let rendered_wiki = wiki.to_string();
if source_contents != rendered_wiki {
let diff = TextDiff::from_lines(source_contents, &rendered_wiki)
.unified_diff()
.header("wiki", "rendered")
.to_string();
return Err(vec![Error::new(
&format!(
"The wiki is not formatted correctly. {} can fix it.\n\n{diff}",
"mull fix".code_str(),
),
location.display_path(),
None,
None,
)]);
}
Ok(wiki)
}