DiffBuilder

Struct DiffBuilder 

Source
pub struct DiffBuilder { /* private fields */ }
Expand description

Builder for Diff.

Implementations§

Source§

impl DiffBuilder

Source

pub fn ignore_paths(&mut self, value: Vec<IgnorePath>) -> &mut Self

An array of paths to ignore. Use DiffBuilder::ignore_path to add them in a more convenient way.

Source

pub fn equate_empty_arrays(&mut self, value: bool) -> &mut Self

If true arrays with a length of zero will be equal, regardless of whether they are nil.

Source

pub fn approx_float_eq_epsilon(&mut self, value: f64) -> &mut Self

If not zero a float comparison will be done using approx::relative_eq. It’s useful when you want to ignore small differences, e.g. 0.19999999999999 ~ 0.2.

Source

pub fn approx_date_time_eq_duration(&mut self, value: Duration) -> &mut Self

An acceptable duration difference for the JSON string values that are valid timestamps. Date approximation will only be executed when this value is not zero and a string value is a valid rfc3339 date.

Source

pub fn source(&mut self, value: Value) -> &mut Self

Source JSON value that will be compared with Diff::target.

Examples found in repository?
examples/simple_object_diff.rs (line 13)
1fn main() {
2    let obj1 = serde_json::json!({
3        "user": "John",
4        "age": 31
5    });
6
7    let obj2 = serde_json::json!({
8        "user": "John",
9        "age": 33
10    });
11
12    let diff = sjdiff::DiffBuilder::default()
13        .source(obj1)
14        .target(obj2)
15        .build()
16        .unwrap();
17    let diff = diff.compare();
18
19    serde_json::to_writer_pretty(std::io::stdout(), &diff).unwrap();
20}
More examples
Hide additional examples
examples/ignore_with_rhai_script.rs (line 40)
1fn main() {
2    let obj1 = serde_json::json!({
3            "users": [
4                {
5                    "name": "Joe",
6                    "age": 43,
7                },
8                {
9                    "name": "Ana",
10                    "age": 33,
11                    "animals": {
12                        "type": "dog"
13                    }
14                },
15            ]
16        });
17
18    let obj2 = serde_json::json!({
19            "users": [
20                {
21                    "name": "Joe",
22                    "age": 43,
23                },
24                {
25                    "name": "Ana",
26                    "age": 33,
27                    "animals": {
28                        "type": "cat"
29                    }
30                },
31            ]
32        });
33
34    let script = r#"
35        let res = target.value_by_path("users.[_].age", curr_path);
36        res == 33
37        "#;
38
39    let diff = sjdiff::DiffBuilder::default()
40        .source(obj1)
41        .target(obj2)
42        .ignore_path_with_condition("users.[_].animals.type", sjdiff::IgnorePathCondition::Rhai(script.to_string()))
43        .build();
44    let diff = diff.unwrap().compare();
45    print!("{:?}", diff);
46}
Source

pub fn target(&mut self, value: Value) -> &mut Self

Target JSON value that will be compared with Diff::source.

Examples found in repository?
examples/simple_object_diff.rs (line 14)
1fn main() {
2    let obj1 = serde_json::json!({
3        "user": "John",
4        "age": 31
5    });
6
7    let obj2 = serde_json::json!({
8        "user": "John",
9        "age": 33
10    });
11
12    let diff = sjdiff::DiffBuilder::default()
13        .source(obj1)
14        .target(obj2)
15        .build()
16        .unwrap();
17    let diff = diff.compare();
18
19    serde_json::to_writer_pretty(std::io::stdout(), &diff).unwrap();
20}
More examples
Hide additional examples
examples/ignore_with_rhai_script.rs (line 41)
1fn main() {
2    let obj1 = serde_json::json!({
3            "users": [
4                {
5                    "name": "Joe",
6                    "age": 43,
7                },
8                {
9                    "name": "Ana",
10                    "age": 33,
11                    "animals": {
12                        "type": "dog"
13                    }
14                },
15            ]
16        });
17
18    let obj2 = serde_json::json!({
19            "users": [
20                {
21                    "name": "Joe",
22                    "age": 43,
23                },
24                {
25                    "name": "Ana",
26                    "age": 33,
27                    "animals": {
28                        "type": "cat"
29                    }
30                },
31            ]
32        });
33
34    let script = r#"
35        let res = target.value_by_path("users.[_].age", curr_path);
36        res == 33
37        "#;
38
39    let diff = sjdiff::DiffBuilder::default()
40        .source(obj1)
41        .target(obj2)
42        .ignore_path_with_condition("users.[_].animals.type", sjdiff::IgnorePathCondition::Rhai(script.to_string()))
43        .build();
44    let diff = diff.unwrap().compare();
45    print!("{:?}", diff);
46}
Source

pub fn build(&self) -> Result<Diff, DiffBuilderError>

Builds a new Diff.

§Errors

If a required field has not been initialized.

Examples found in repository?
examples/simple_object_diff.rs (line 15)
1fn main() {
2    let obj1 = serde_json::json!({
3        "user": "John",
4        "age": 31
5    });
6
7    let obj2 = serde_json::json!({
8        "user": "John",
9        "age": 33
10    });
11
12    let diff = sjdiff::DiffBuilder::default()
13        .source(obj1)
14        .target(obj2)
15        .build()
16        .unwrap();
17    let diff = diff.compare();
18
19    serde_json::to_writer_pretty(std::io::stdout(), &diff).unwrap();
20}
More examples
Hide additional examples
examples/ignore_with_rhai_script.rs (line 43)
1fn main() {
2    let obj1 = serde_json::json!({
3            "users": [
4                {
5                    "name": "Joe",
6                    "age": 43,
7                },
8                {
9                    "name": "Ana",
10                    "age": 33,
11                    "animals": {
12                        "type": "dog"
13                    }
14                },
15            ]
16        });
17
18    let obj2 = serde_json::json!({
19            "users": [
20                {
21                    "name": "Joe",
22                    "age": 43,
23                },
24                {
25                    "name": "Ana",
26                    "age": 33,
27                    "animals": {
28                        "type": "cat"
29                    }
30                },
31            ]
32        });
33
34    let script = r#"
35        let res = target.value_by_path("users.[_].age", curr_path);
36        res == 33
37        "#;
38
39    let diff = sjdiff::DiffBuilder::default()
40        .source(obj1)
41        .target(obj2)
42        .ignore_path_with_condition("users.[_].animals.type", sjdiff::IgnorePathCondition::Rhai(script.to_string()))
43        .build();
44    let diff = diff.unwrap().compare();
45    print!("{:?}", diff);
46}
Source§

impl DiffBuilder

Source

pub fn ignore_path(&mut self, path: &str) -> &mut Self

Set a JSON path using a string format that you want to ignore during the comparison. A string path will be parsed to IgnorePath and appended to Diff::ignore_paths.

§Examples

a.[_].c will ignore c key in any element of array a:

{
    "a": [{"b": "3", "c": "4"}]
}

[_] means any index.

and a.[1].c will ignore c key in the element with index 1.

address.zip will ignore zip key in the address:

{
    "address": {
}

NOTE: if the element is missing in the source or target and you added it to ignore paths, the result diff will still show it as a missing one. Use DiffBuilder::ignore_path_with_missing with ignore_missing set to true instead.

Source

pub fn ignore_path_with_missing( &mut self, path: &str, ignore_missing: bool, ) -> &mut Self

Adds a path to the ignored ones. ignore_missing indicates whether the element should be ignored if it’s missing in the source or target. See documentation for DiffBuilder::ignore_path for usage examples.

Source

pub fn ignore_path_with_condition( &mut self, path: &str, condition: IgnorePathCondition, ) -> &mut Self

Does the same as DiffBuilder::ignore_path but you can pass a custom script as a condition. See the example ignore_with_rhai_script.rs to learn how to use it.

Examples found in repository?
examples/ignore_with_rhai_script.rs (line 42)
1fn main() {
2    let obj1 = serde_json::json!({
3            "users": [
4                {
5                    "name": "Joe",
6                    "age": 43,
7                },
8                {
9                    "name": "Ana",
10                    "age": 33,
11                    "animals": {
12                        "type": "dog"
13                    }
14                },
15            ]
16        });
17
18    let obj2 = serde_json::json!({
19            "users": [
20                {
21                    "name": "Joe",
22                    "age": 43,
23                },
24                {
25                    "name": "Ana",
26                    "age": 33,
27                    "animals": {
28                        "type": "cat"
29                    }
30                },
31            ]
32        });
33
34    let script = r#"
35        let res = target.value_by_path("users.[_].age", curr_path);
36        res == 33
37        "#;
38
39    let diff = sjdiff::DiffBuilder::default()
40        .source(obj1)
41        .target(obj2)
42        .ignore_path_with_condition("users.[_].animals.type", sjdiff::IgnorePathCondition::Rhai(script.to_string()))
43        .build();
44    let diff = diff.unwrap().compare();
45    print!("{:?}", diff);
46}

Trait Implementations§

Source§

impl Clone for DiffBuilder

Source§

fn clone(&self) -> DiffBuilder

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Default for DiffBuilder

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.