use std::io::Write;
use super::{diff_algorithm::Algorithm, draw_diff::DrawDiff, themes::Theme};
pub fn diff(w: &mut dyn Write, old: &str, new: &str, theme: &dyn Theme) -> std::io::Result<()> {
if !Algorithm::has_available_algorithms() {
return write!(
w,
"Error: No diff algorithms are available. Enable either 'myers' or 'similar' feature."
);
}
let output: DrawDiff<'_> = DrawDiff::new(old, new, theme);
write!(w, "{output}")
}
pub fn diff_with_algorithm(
w: &mut dyn Write,
old: &str,
new: &str,
theme: &dyn Theme,
algorithm: Algorithm,
) -> std::io::Result<()> {
if !Algorithm::has_available_algorithms() {
return write!(
w,
"Error: No diff algorithms are available. Enable either 'myers' or 'similar' feature."
);
}
let available_algorithms = Algorithm::available_algorithms();
if !available_algorithms.contains(&algorithm) {
if let Some(available_algo) = Algorithm::first_available() {
let output: DrawDiff<'_> = DrawDiff::with_algorithm(old, new, theme, available_algo);
return write!(w, "{output}");
}
return write!(
w,
"Error: No diff algorithms are available. Enable either 'myers' or 'similar' feature."
);
}
let output: DrawDiff<'_> = DrawDiff::with_algorithm(old, new, theme, algorithm);
write!(w, "{output}")
}