watchdiff_tui/diff/
generator.rs1use super::algorithms::{DiffAlgorithm, DiffAlgorithmType, DiffResult};
2
3pub struct DiffGenerator {
5 algorithm: Box<dyn DiffAlgorithm>,
6}
7
8impl DiffGenerator {
9 pub fn new(algorithm_type: DiffAlgorithmType) -> Self {
11 Self {
12 algorithm: algorithm_type.create(),
13 }
14 }
15
16 pub fn with_algorithm(algorithm: Box<dyn DiffAlgorithm>) -> Self {
18 Self { algorithm }
19 }
20
21 pub fn generate(&self, old: &str, new: &str) -> DiffResult {
23 self.algorithm.diff(old, new)
24 }
25
26 pub fn algorithm_name(&self) -> &str {
28 self.algorithm.name()
29 }
30
31 pub fn algorithm_description(&self) -> &str {
33 self.algorithm.description()
34 }
35}
36
37impl Default for DiffGenerator {
38 fn default() -> Self {
39 Self::new(DiffAlgorithmType::default())
40 }
41}
42
43pub struct DiffConfig {
45 algorithm: DiffAlgorithmType,
46 context_lines: usize,
47}
48
49impl DiffConfig {
50 pub fn new() -> Self {
51 Self {
52 algorithm: DiffAlgorithmType::default(),
53 context_lines: 3,
54 }
55 }
56
57 pub fn algorithm(mut self, algorithm: DiffAlgorithmType) -> Self {
58 self.algorithm = algorithm;
59 self
60 }
61
62 pub fn context_lines(mut self, lines: usize) -> Self {
63 self.context_lines = lines;
64 self
65 }
66
67 pub fn build(self) -> DiffGenerator {
68 DiffGenerator::new(self.algorithm)
69 }
70}
71
72impl Default for DiffConfig {
73 fn default() -> Self {
74 Self::new()
75 }
76}
77
78#[cfg(test)]
79mod tests {
80 use super::*;
81
82 #[test]
83 fn test_diff_generator() {
84 let generator = DiffGenerator::new(DiffAlgorithmType::Myers);
85 let result = generator.generate("a\nb\nc", "a\nx\nc");
86
87 assert_eq!(generator.algorithm_name(), "Myers");
88 assert_eq!(result.stats.lines_added, 1);
89 assert_eq!(result.stats.lines_removed, 1);
90 }
91
92 #[test]
93 fn test_diff_config_builder() {
94 let generator = DiffConfig::new()
95 .algorithm(DiffAlgorithmType::Patience)
96 .context_lines(5)
97 .build();
98
99 assert_eq!(generator.algorithm_name(), "Patience");
100 }
101}