sjdiff

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)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
fn main() {
    let obj1 = serde_json::json!({
        "user": "John",
        "age": 31
    });

    let obj2 = serde_json::json!({
        "user": "John",
        "age": 33
    });

    let diff = sjdiff::DiffBuilder::default()
        .source(obj1)
        .target(obj2)
        .build()
        .unwrap();
    let diff = diff.compare();

    serde_json::to_writer_pretty(std::io::stdout(), &diff).unwrap();
}
More examples
Hide additional examples
examples/ignore_with_rhai_script.rs (line 40)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
fn main() {
    let obj1 = serde_json::json!({
            "users": [
                {
                    "name": "Joe",
                    "age": 43,
                },
                {
                    "name": "Ana",
                    "age": 33,
                    "animals": {
                        "type": "dog"
                    }
                },
            ]
        });

    let obj2 = serde_json::json!({
            "users": [
                {
                    "name": "Joe",
                    "age": 43,
                },
                {
                    "name": "Ana",
                    "age": 33,
                    "animals": {
                        "type": "cat"
                    }
                },
            ]
        });

    let script = r#"
        let res = target.value_by_path("users.[_].age", curr_path);
        res == 33
        "#;

    let diff = sjdiff::DiffBuilder::default()
        .source(obj1)
        .target(obj2)
        .ignore_path_with_condition("users.[_].animals.type", sjdiff::IgnorePathCondition::Rhai(script.to_string()))
        .build();
    let diff = diff.unwrap().compare();
    print!("{:?}", diff);
}
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)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
fn main() {
    let obj1 = serde_json::json!({
        "user": "John",
        "age": 31
    });

    let obj2 = serde_json::json!({
        "user": "John",
        "age": 33
    });

    let diff = sjdiff::DiffBuilder::default()
        .source(obj1)
        .target(obj2)
        .build()
        .unwrap();
    let diff = diff.compare();

    serde_json::to_writer_pretty(std::io::stdout(), &diff).unwrap();
}
More examples
Hide additional examples
examples/ignore_with_rhai_script.rs (line 41)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
fn main() {
    let obj1 = serde_json::json!({
            "users": [
                {
                    "name": "Joe",
                    "age": 43,
                },
                {
                    "name": "Ana",
                    "age": 33,
                    "animals": {
                        "type": "dog"
                    }
                },
            ]
        });

    let obj2 = serde_json::json!({
            "users": [
                {
                    "name": "Joe",
                    "age": 43,
                },
                {
                    "name": "Ana",
                    "age": 33,
                    "animals": {
                        "type": "cat"
                    }
                },
            ]
        });

    let script = r#"
        let res = target.value_by_path("users.[_].age", curr_path);
        res == 33
        "#;

    let diff = sjdiff::DiffBuilder::default()
        .source(obj1)
        .target(obj2)
        .ignore_path_with_condition("users.[_].animals.type", sjdiff::IgnorePathCondition::Rhai(script.to_string()))
        .build();
    let diff = diff.unwrap().compare();
    print!("{:?}", diff);
}
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)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
fn main() {
    let obj1 = serde_json::json!({
        "user": "John",
        "age": 31
    });

    let obj2 = serde_json::json!({
        "user": "John",
        "age": 33
    });

    let diff = sjdiff::DiffBuilder::default()
        .source(obj1)
        .target(obj2)
        .build()
        .unwrap();
    let diff = diff.compare();

    serde_json::to_writer_pretty(std::io::stdout(), &diff).unwrap();
}
More examples
Hide additional examples
examples/ignore_with_rhai_script.rs (line 43)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
fn main() {
    let obj1 = serde_json::json!({
            "users": [
                {
                    "name": "Joe",
                    "age": 43,
                },
                {
                    "name": "Ana",
                    "age": 33,
                    "animals": {
                        "type": "dog"
                    }
                },
            ]
        });

    let obj2 = serde_json::json!({
            "users": [
                {
                    "name": "Joe",
                    "age": 43,
                },
                {
                    "name": "Ana",
                    "age": 33,
                    "animals": {
                        "type": "cat"
                    }
                },
            ]
        });

    let script = r#"
        let res = target.value_by_path("users.[_].age", curr_path);
        res == 33
        "#;

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

    let obj2 = serde_json::json!({
            "users": [
                {
                    "name": "Joe",
                    "age": 43,
                },
                {
                    "name": "Ana",
                    "age": 33,
                    "animals": {
                        "type": "cat"
                    }
                },
            ]
        });

    let script = r#"
        let res = target.value_by_path("users.[_].age", curr_path);
        res == 33
        "#;

    let diff = sjdiff::DiffBuilder::default()
        .source(obj1)
        .target(obj2)
        .ignore_path_with_condition("users.[_].animals.type", sjdiff::IgnorePathCondition::Rhai(script.to_string()))
        .build();
    let diff = diff.unwrap().compare();
    print!("{:?}", diff);
}

Trait Implementations§

Source§

impl Clone for DiffBuilder

Source§

fn clone(&self) -> DiffBuilder

Returns a copy 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, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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.