Skip to main content

java_diff_utils_rs/algorithm/
diff_algorithm_factory.rs

1//! Factory interface and implementations for constructing diff algorithms.
2
3use super::{Change, DiffAlgorithm};
4
5pub trait DiffAlgorithmFactory<T> {
6    fn create(&self) -> Box<dyn DiffAlgorithm<T>>
7    where
8        T: PartialEq + 'static;
9
10    fn create_with_equalizer(
11        &self,
12        equalizer: Box<dyn Fn(&T, &T) -> bool + 'static>,
13    ) -> Box<dyn DiffAlgorithm<T>>
14    where
15        T: 'static;
16}
17
18#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
19pub struct MyersDiffFactory;
20
21impl<T: 'static> DiffAlgorithmFactory<T> for MyersDiffFactory {
22    fn create(&self) -> Box<dyn DiffAlgorithm<T>>
23    where
24        T: PartialEq + 'static,
25    {
26        Box::new(|source: &[T], target: &[T]| -> Vec<Change> {
27            super::myers::compute_diff(source, target)
28        })
29    }
30
31    fn create_with_equalizer(
32        &self,
33        equalizer: Box<dyn Fn(&T, &T) -> bool + 'static>,
34    ) -> Box<dyn DiffAlgorithm<T>> {
35        Box::new(move |source: &[T], target: &[T]| -> Vec<Change> {
36            super::myers::compute_diff_with(source, target, &*equalizer)
37        })
38    }
39}
40
41#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
42pub struct MyersLinearDiffFactory;
43
44impl<T: PartialEq + 'static> DiffAlgorithmFactory<T> for MyersLinearDiffFactory {
45    fn create(&self) -> Box<dyn DiffAlgorithm<T>>
46    where
47        T: PartialEq + 'static,
48    {
49        Box::new(super::myers::myers_linear::MyersDiffWithLinearSpace::<T>::new())
50    }
51
52    fn create_with_equalizer(
53        &self,
54        equalizer: Box<dyn Fn(&T, &T) -> bool + 'static>,
55    ) -> Box<dyn DiffAlgorithm<T>> {
56        Box::new(
57            super::myers::myers_linear::MyersDiffWithLinearSpace::<T>::with_equalizer(
58                move |a: &T, b: &T| equalizer(a, b),
59            ),
60        )
61    }
62}
63
64pub use super::histogram::HistogramDiffFactory;