use std::time::Duration;
use axoasset::SourceFile;
use camino::Utf8Path;
use newline_converter::dos2unix;
use crate::errors::{DistError, DistResult};
pub mod ci;
pub mod installer;
pub mod templates;
pub fn diff_files(existing_file: &Utf8Path, new_file_contents: &str) -> DistResult<()> {
let existing = if let Ok(file) = SourceFile::load_local(existing_file) {
file
} else {
SourceFile::new(existing_file.as_str(), String::new())
};
diff_source(existing, new_file_contents)
}
pub(crate) fn diff_source(existing: SourceFile, new_file_contents: &str) -> DistResult<()> {
let a = dos2unix(existing.contents());
let b = dos2unix(new_file_contents);
let diff = similar::TextDiff::configure()
.algorithm(similar::Algorithm::Patience)
.timeout(Duration::from_millis(10))
.diff_lines(&a, &b)
.unified_diff()
.header(existing.origin_path(), existing.origin_path())
.to_string();
if !diff.is_empty() {
Err(DistError::CheckFileMismatch {
file: existing,
diff,
})
} else {
Ok(())
}
}