diffo 0.2.0

Semantic diffing for Rust structs via serde
Documentation
//! Semantic diffing for Rust structs.
//!
//! `diffo` computes structural differences between two values of the same type,
//! leveraging serde for serialization. No derive macros needed - just implement
//! `Serialize` and you're ready to go.
//!
//! # Examples
//!
//! Basic usage:
//!
//! ```
//! use diffo::diff;
//! use serde::Serialize;
//!
//! #[derive(Serialize)]
//! struct User {
//!     id: u64,
//!     name: String,
//! }
//!
//! let old = User { id: 1, name: "Alice".into() };
//! let new = User { id: 1, name: "Bob".into() };
//!
//! let d = diff(&old, &new).unwrap();
//! assert!(!d.is_empty());
//! ```
//!
//! With configuration:
//!
//! ```
//! use diffo::{diff_with, DiffConfig};
//! # use serde::Serialize;
//! # #[derive(Serialize)]
//! # struct Config { password: String }
//! # let old = Config { password: "secret".into() };
//! # let new = Config { password: "new_secret".into() };
//!
//! let config = DiffConfig::new()
//!     .mask("password");
//!
//! let d = diff_with(&old, &new, &config).unwrap();
//! ```
//!
//! # Output Formats
//!
//! - [`Diff::to_pretty`]: Human-readable colored output
//! - [`Diff::to_json`]: JSON representation
//! - [`Diff::to_json_patch`]: RFC 6902 JSON Patch
//! - [`Diff::to_markdown`]: Markdown table
//!
//! # Performance
//!
//! Complexity is O(n) for most operations, where n is the number of fields.
//! Sequence diffing is index-based (O(n)), not LCS-based (O(n²)).

#![deny(missing_docs)]

mod apply;
mod change;
mod config;
mod diff;
mod error;
mod path;
mod sequence_diff;
mod value_helpers;

pub mod format;

pub use apply::apply;
pub use change::Change;
pub use config::{Comparator, DiffConfig};
pub use diff::Diff;
pub use error::{Error, FormatError};
pub use path::Path;
pub use sequence_diff::SequenceDiffAlgorithm;
pub use value_helpers::ValueExt;

use serde::Serialize;

/// Compute diff between two values of the same type.
///
/// # 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());
/// ```
pub fn diff<T: Serialize>(old: &T, new: &T) -> Result<Diff, Error> {
    diff_with(old, new, &DiffConfig::default())
}

/// Compute diff with custom configuration.
///
/// # Examples
///
/// ```
/// use diffo::{diff_with, DiffConfig};
///
/// let config = DiffConfig::new().mask("*.password");
///
/// # let old = vec![1, 2, 3];
/// # let new = vec![1, 2, 4];
/// let d = diff_with(&old, &new, &config).unwrap();
/// ```
pub fn diff_with<T: Serialize>(old: &T, new: &T, config: &DiffConfig) -> Result<Diff, Error> {
    let old_val = serde_value::to_value(old)?;
    let new_val = serde_value::to_value(new)?;
    Ok(diff::diff_values(&old_val, &new_val, Path::root(), config))
}