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
impl Diff
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new empty diff.
§Examples
use diffo::Diff;
let diff = Diff::new();
assert!(diff.is_empty());Sourcepub fn is_empty(&self) -> bool
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());Sourcepub fn len(&self) -> usize
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);Sourcepub fn changes(&self) -> &BTreeMap<Path, Change>
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);
}Sourcepub fn get(&self, path: &str) -> Option<&Change>
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());Sourcepub fn paths(&self) -> impl Iterator<Item = &Path>
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]"));Sourcepub fn insert(&mut self, path: Path, change: Change)
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());Sourcepub fn merge(&mut self, other: Diff)
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
impl Diff
Sourcepub fn to_pretty(&self) -> String
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());Sourcepub fn to_json(&self) -> Result<String, FormatError>
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();Sourcepub fn to_json_patch(&self) -> Result<String, FormatError>
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();Sourcepub fn to_markdown(&self) -> Result<String, FormatError>
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§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more