Diff

Struct Diff 

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

Represents a complete diff between two values.

Contains a map of paths to changes, sorted by path for deterministic iteration.

§Examples

use diffo::diff;

let old = vec![1, 2, 3];
let new = vec![1, 2, 4];

let d = diff(&old, &new).unwrap();
assert!(!d.is_empty());
assert_eq!(d.changes().len(), 1);

Implementations§

Source§

impl Diff

Source

pub fn new() -> Self

Create a new empty diff.

§Examples
use diffo::Diff;

let diff = Diff::new();
assert!(diff.is_empty());
Source

pub fn is_empty(&self) -> bool

Returns true if the diff contains no changes.

§Examples
use diffo::diff;

let v1 = vec![1, 2, 3];
let v2 = vec![1, 2, 3];

let d = diff(&v1, &v2).unwrap();
assert!(d.is_empty());
Source

pub fn len(&self) -> usize

Returns the number of changes in the diff.

§Examples
use diffo::diff;

let old = vec![1, 2, 3];
let new = vec![1, 2, 4];

let d = diff(&old, &new).unwrap();
assert_eq!(d.len(), 1);
Source

pub fn changes(&self) -> &BTreeMap<Path, Change>

Get a reference to all changes.

§Examples
use diffo::diff;

let old = vec![1, 2, 3];
let new = vec![1, 2, 4];

let d = diff(&old, &new).unwrap();
for (path, change) in d.changes() {
    println!("{}: {:?}", path, change);
}
Source

pub fn get(&self, path: &str) -> Option<&Change>

Get a specific change by path.

§Examples
use diffo::diff;

let old = vec![1, 2, 3];
let new = vec![1, 2, 4];

let d = diff(&old, &new).unwrap();
assert!(d.get("[2]").is_some());
assert!(d.get("[0]").is_none());
Source

pub fn paths(&self) -> impl Iterator<Item = &Path>

Get an iterator over all paths with changes.

§Examples
use diffo::diff;

let old = vec![1, 2, 3];
let new = vec![1, 2, 4, 5];

let d = diff(&old, &new).unwrap();
let paths: Vec<_> = d.paths().map(|p| p.as_str()).collect();
assert!(paths.contains(&"[2]"));
Source

pub fn insert(&mut self, path: Path, change: Change)

Insert a change at the given path.

§Examples
use diffo::{Diff, Path, Change};
use serde_value::Value;

let mut diff = Diff::new();
diff.insert(Path::root().field("x"), Change::Added(Value::I64(42)));
assert!(!diff.is_empty());
Source

pub fn merge(&mut self, other: Diff)

Merge another diff into this one.

§Examples
use diffo::{Diff, Path, Change};
use serde_value::Value;

let mut d1 = Diff::new();
d1.insert(Path::root().field("a"), Change::Added(Value::I64(1)));

let mut d2 = Diff::new();
d2.insert(Path::root().field("b"), Change::Added(Value::I64(2)));

d1.merge(d2);
assert_eq!(d1.len(), 2);
Source§

impl Diff

Source

pub fn to_pretty(&self) -> String

Format as pretty colored output.

§Examples
use diffo::diff;

let old = vec![1, 2, 3];
let new = vec![1, 2, 4];

let d = diff(&old, &new).unwrap();
println!("{}", d.to_pretty());
Source

pub fn to_json(&self) -> Result<String, FormatError>

Format as JSON.

§Examples
use diffo::diff;

let old = vec![1, 2, 3];
let new = vec![1, 2, 4];

let d = diff(&old, &new).unwrap();
let json = d.to_json().unwrap();
Source

pub fn to_json_patch(&self) -> Result<String, FormatError>

Format as JSON Patch (RFC 6902).

§Examples
use diffo::diff;

let old = vec![1, 2, 3];
let new = vec![1, 2, 4];

let d = diff(&old, &new).unwrap();
let patch = d.to_json_patch().unwrap();
Source

pub fn to_markdown(&self) -> Result<String, FormatError>

Format as Markdown table.

§Examples
use diffo::diff;

let old = vec![1, 2, 3];
let new = vec![1, 2, 4];

let d = diff(&old, &new).unwrap();
let markdown = d.to_markdown().unwrap();

Trait Implementations§

Source§

impl Clone for Diff

Source§

fn clone(&self) -> Diff

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 Debug for Diff

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Diff

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl Freeze for Diff

§

impl RefUnwindSafe for Diff

§

impl Send for Diff

§

impl Sync for Diff

§

impl Unpin for Diff

§

impl UnwindSafe for Diff

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.