Skip to main content

java_diff_utils_rs/patch/
equal_delta.rs

1use std::fmt;
2
3use super::chunk::Chunk;
4use super::delta::Delta;
5use super::delta_type::DeltaType;
6use super::error::PatchError;
7
8/// Represents an unchanged (equal) region of data between the source and target sequences.
9#[derive(Debug, Clone, PartialEq, Eq, Hash)]
10pub struct EqualDelta<T> {
11    inner: Delta<T>,
12}
13
14impl<T> EqualDelta<T> {
15    /// Creates a new `EqualDelta` with the given source and target chunks.
16    pub fn new(source: Chunk<T>, target: Chunk<T>) -> Self {
17        Self {
18            inner: Delta::new(DeltaType::Equal, source, target),
19        }
20    }
21
22    /// Returns a reference to the underlying inner [`Delta`].
23    #[inline]
24    pub fn delta(&self) -> &Delta<T> {
25        &self.inner
26    }
27
28    /// Consumes `self` and returns the inner [`Delta`].
29    #[inline]
30    pub fn into_delta(self) -> Delta<T> {
31        self.inner
32    }
33
34    /// Returns a reference to the source chunk.
35    #[inline]
36    pub fn source(&self) -> &Chunk<T> {
37        self.inner.source()
38    }
39
40    /// Returns a reference to the target chunk.
41    #[inline]
42    pub fn target(&self) -> &Chunk<T> {
43        self.inner.target()
44    }
45
46    /// Applies this equal delta to the target vector.
47    ///
48    /// Since the lines are identical, this is a no-op that always succeeds.
49    pub fn apply_to(&self, _target: &mut Vec<T>) -> Result<(), PatchError> {
50        Ok(())
51    }
52
53    /// Restores (un-applies) this equal delta on the target vector.
54    ///
55    /// Since the lines are identical, this is a no-op that always succeeds.
56    pub fn restore(&self, _target: &mut Vec<T>) -> Result<(), PatchError> {
57        Ok(())
58    }
59
60    /// Applies fuzzy patching for equal lines.
61    ///
62    /// Since the content is equal, no modification occurs.
63    pub fn apply_fuzzy_to_at(
64        &self,
65        _target: &mut Vec<T>,
66        _fuzz: usize,
67        _position: usize,
68    ) -> Result<(), PatchError> {
69        Ok(())
70    }
71
72    /// Creates a new `EqualDelta` with custom source and target chunks.
73    #[must_use]
74    pub fn with_chunks(&self, original: Chunk<T>, revised: Chunk<T>) -> Self {
75        Self::new(original, revised)
76    }
77}
78
79impl<T: fmt::Debug> fmt::Display for EqualDelta<T> {
80    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
81        write!(
82            f,
83            "[EqualDelta, position: {}, lines: {:?}]",
84            self.source().position(),
85            self.source().lines()
86        )
87    }
88}
89
90impl<T> From<EqualDelta<T>> for Delta<T> {
91    fn from(equal: EqualDelta<T>) -> Self {
92        equal.inner
93    }
94}