diff_doc/
lib.rs

1#[cfg(feature="patch")] pub mod patch;
2pub mod txt;
3pub mod diff;
4pub mod generic;
5mod vec_processor;
6mod map_processor;
7
8use std::borrow::Cow;
9use std::fmt;
10use std::fmt::Display;
11
12/// document were implements contract to deal with differences
13pub trait MismatchDoc<T> {
14    fn new(base: &T, input: &T) -> Result<Self, DocError> where Self: Sized;
15
16    /// return false if no intersection between two patches and they can apply in any order
17    fn is_intersect(&self, other: &Self) -> Result<bool, DocError>;
18
19    fn len(&self) -> usize;
20}
21
22/// document update as mutation
23pub trait MismatchDocMut<T> {
24    
25    /// if fail_fast then stop on first error, 
26    /// otherwise try to apply all and collect errors on Ok response
27    /// - non-transactional behavioral, partial updates possible either on fail_fast!
28    fn apply_mut(&self, input: &mut T, fail_fast: bool) -> Result<Vec<DocError>, DocError>;
29}
30
31/// document update Copy on Write
32pub trait MismatchDocCow<T> {
33    fn apply(&self, input: &T) -> Result<T, DocError>;
34}
35
36/// supported type of document
37#[derive(Debug, PartialEq, serde::Serialize, serde::Deserialize)]
38pub enum Mismatches {
39    #[cfg(feature="patch")]
40    /// with initial diff patch content from GNU patch file
41    Patch(patch::Mismatch),
42    /// json or other document
43    Doc(diff::Mismatch),
44    /// line number strings update and trim only
45    Text(txt::Mismatch),
46}
47
48
49#[derive(Debug)]
50pub struct DocError(Cow<'static, str>);
51
52impl DocError {
53    fn new<E: Into<Cow<'static, str>>>(e: E) -> Self {
54        Self(e.into())
55    }
56}
57impl std::error::Error for DocError {}
58
59impl Display for DocError {
60    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
61        write!(f, "{}", self.0)
62    }
63}
64
65impl Display for Mismatches {
66    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
67        match serde_json::to_string(&self) {
68            Ok(json) => write!(f, "{}", json),
69            Err(err) => write!(f, "ERR: {}", err)
70        }
71
72    }
73}
74
75#[cfg(test)]
76mod tests {
77    #[test]
78    fn test_compile() {
79        assert!(true);
80    }
81}