use crate::{error::Error, format::CodeStr, parser, validator, wiki::Wiki};
use similar::TextDiff;
use std::path::Path;
pub fn analyze(
wiki_path: &Path,
source_path: &Path,
source_contents: &str,
) -> Result<Wiki, Vec<Error>> {
let wiki = parser::parse(source_path, source_contents)?;
validator::validate(&wiki, wiki_path, source_path, source_contents)?;
Ok(wiki)
}
pub fn check(
wiki_path: &Path,
source_path: &Path,
source_contents: &str,
) -> Result<Wiki, Vec<Error>> {
let wiki = analyze(wiki_path, source_path, 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(),
),
Some(source_path),
None,
None,
)]);
}
Ok(wiki)
}