watchdiff_tui/diff/
mod.rs

1//! Diff generation and formatting module
2//! 
3//! This module provides a trait-based architecture for generating and formatting
4//! diffs using different algorithms. It supports multiple diff algorithms and
5//! output formats.
6
7pub mod algorithms;
8pub mod generator;
9pub mod formatter;
10
11// Re-export the main types for easier use
12pub use algorithms::{
13    DiffAlgorithm, DiffAlgorithmType, DiffResult, DiffHunk, DiffOperation, DiffStats,
14    MyersAlgorithm, PatienceAlgorithm, LcsAlgorithm,
15};
16
17pub use generator::{DiffGenerator, DiffConfig};
18pub use formatter::{DiffFormatter, DiffFormat};
19
20/// Convenience function to generate a unified diff with default settings
21pub fn generate_unified_diff<P: AsRef<std::path::Path>>(
22    old: &str,
23    new: &str, 
24    old_path: P,
25    new_path: P,
26) -> String {
27    let generator = DiffGenerator::default();
28    let result = generator.generate(old, new);
29    DiffFormatter::format_unified(&result, old_path, new_path)
30}
31
32/// Convenience function to generate a side-by-side diff with default settings
33pub fn generate_side_by_side_diff<P: AsRef<std::path::Path>>(
34    old: &str,
35    new: &str,
36    old_path: P, 
37    new_path: P,
38    width: usize,
39) -> String {
40    let generator = DiffGenerator::default();
41    let result = generator.generate(old, new);
42    DiffFormatter::format_side_by_side(&result, old_path, new_path, width)
43}
44
45/// Convenience function to get diff statistics
46pub fn get_diff_stats(old: &str, new: &str) -> DiffStats {
47    let generator = DiffGenerator::default();
48    let result = generator.generate(old, new);
49    result.stats
50}
51
52#[cfg(test)]
53mod tests {
54    use super::*;
55
56    #[test]
57    fn test_convenience_functions() {
58        let old = "line1\nline2\nline3";
59        let new = "line1\nmodified\nline3";
60        
61        let unified = generate_unified_diff(old, new, "old.txt", "new.txt");
62        assert!(unified.contains("--- old.txt"));
63        assert!(unified.contains("+modified"));
64        
65        let stats = get_diff_stats(old, new);
66        assert_eq!(stats.lines_added, 1);
67        assert_eq!(stats.lines_removed, 1);
68    }
69}