diff --git a/src/algorithms/capture.rs b/src/algorithms/capture.rs index fa6a067..11f3d76 100644 --- a/src/algorithms/capture.rs +++ b/src/algorithms/capture.rs @@ -1,6 +1,7 @@ use std::convert::Infallible; -use crate::{group_diff_ops, DiffHook, DiffOp}; +use crate::algorithms::DiffHook; +use crate::{group_diff_ops, DiffOp}; /// A [`DiffHook`] that captures all diff operations. #[derive(Default, Clone)] diff --git a/src/hook.rs b/src/algorithms/hook.rs similarity index 100% rename from src/hook.rs rename to src/algorithms/hook.rs diff --git a/src/algorithms/mod.rs b/src/algorithms/mod.rs index 35b3347..1cba2b3 100644 --- a/src/algorithms/mod.rs +++ b/src/algorithms/mod.rs @@ -6,14 +6,14 @@ //! direct access to these algorithms can be useful in some cases. //! //! All these algorithms provide a `diff` function which takes two indexable -//! objects (for instance slices) and a [`DiffHook`](crate::DiffHook). As the +//! objects (for instance slices) and a [`DiffHook`]. As the //! diff is generated the diff hook is invoked. Note that the diff hook does //! not get access to the actual values but only the indexes. This is why the //! diff hook is not used outside of the raw algorithm implementations as for //! most situations access to the values is useful of required. //! -//! A more generic interface for these algorthms is available on the toplevel -//! module. +//! The algoriths module really is the most low-level module in similar and +//! generally not the place to start. //! //! # Example //! @@ -21,18 +21,64 @@ //! between two sequences and capture the ops into a vector. //! //! ```rust -//! use similar::{Algorithm, capture_diff_slices}; +//! use similar::algorithms::{Algorithm, Replace, Capture, diff_slices}; //! //! let a = vec![1, 2, 3, 4, 5]; //! let b = vec![1, 2, 3, 4, 7]; -//! let ops = capture_diff_slices(Algorithm::Myers, &a, &b); +//! let mut d = Replace::new(Capture::new()); +//! diff_slices(Algorithm::Myers, &mut d, &a, &b).unwrap(); +//! let ops = d.into_inner().into_ops(); //! ``` +//! +//! The above example is equivalen to using +//! [`capture_diff_slices`](crate::capture_diff_slices). mod capture; +mod hook; mod replace; +use std::hash::Hash; +use std::ops::{Index, Range}; + pub use capture::Capture; +pub use hook::DiffHook; pub use replace::Replace; +#[doc(no_inline)] +pub use crate::Algorithm; + pub mod myers; pub mod patience; + +/// Creates a diff between old and new with the given algorithm. +/// +/// Diffs `old`, between indices `old_range` and `new` between indices `new_range`. +pub fn diff( + alg: Algorithm, + d: &mut D, + old: &Old, + old_range: Range, + new: &New, + new_range: Range, +) -> Result<(), D::Error> +where + Old: Index + ?Sized, + New: Index + ?Sized, + D: DiffHook, + Old::Output: Hash + Eq + Ord, + New::Output: PartialEq + Hash + Eq + Ord, +{ + match alg { + Algorithm::Myers => myers::diff(d, old, old_range, new, new_range), + Algorithm::Patience => patience::diff(d, old, old_range, new, new_range), + } +} + +/// Shortcut for diffing slices with a specific algorithm. +pub fn diff_slices(alg: Algorithm, d: &mut D, old: &[T], new: &[T]) -> Result<(), D::Error> +where + D: DiffHook, + T: Eq + Hash + Ord, +{ + diff(alg, d, old, 0..old.len(), new, 0..new.len()) +} diff --git a/src/algorithms/myers.rs b/src/algorithms/myers.rs index 23bb082..2b5700c 100644 --- a/src/algorithms/myers.rs +++ b/src/algorithms/myers.rs @@ -9,7 +9,7 @@ use std::cmp::{max, min}; use std::ops::{Index, Range}; -use crate::DiffHook; +use crate::algorithms::DiffHook; /// Myers' diff algorithm. /// diff --git a/src/algorithms/patience.rs b/src/algorithms/patience.rs index 269319e..b70b8b2 100644 --- a/src/algorithms/patience.rs +++ b/src/algorithms/patience.rs @@ -10,8 +10,7 @@ use std::collections::HashMap; use std::hash::Hash; use std::ops::{Index, Range}; -use crate::algorithms::{myers, Replace}; -use crate::DiffHook; +use crate::algorithms::{myers, DiffHook, Replace}; /// Patience diff algorithm. /// diff --git a/src/algorithms/replace.rs b/src/algorithms/replace.rs index 32485f7..9e70047 100644 --- a/src/algorithms/replace.rs +++ b/src/algorithms/replace.rs @@ -1,4 +1,4 @@ -use crate::DiffHook; +use crate::algorithms::DiffHook; /// A [`DiffHook`] that combines deletions and insertions to give blocks /// of maximal length, and replacements when appropriate. diff --git a/src/common.rs b/src/common.rs index 1ba3fb7..0fb052c 100644 --- a/src/common.rs +++ b/src/common.rs @@ -1,41 +1,8 @@ use std::hash::Hash; use std::ops::{Index, Range}; -use crate::algorithms::{myers, patience, Capture, Replace}; -use crate::{Algorithm, DiffHook, DiffOp}; - -/// Creates a diff between old and new with the given algorithm. -/// -/// Diffs `old`, between indices `old_range` and `new` between indices `new_range`. -pub fn diff( - alg: Algorithm, - d: &mut D, - old: &Old, - old_range: Range, - new: &New, - new_range: Range, -) -> Result<(), D::Error> -where - Old: Index + ?Sized, - New: Index + ?Sized, - D: DiffHook, - Old::Output: Hash + Eq + Ord, - New::Output: PartialEq + Hash + Eq + Ord, -{ - match alg { - Algorithm::Myers => myers::diff(d, old, old_range, new, new_range), - Algorithm::Patience => patience::diff(d, old, old_range, new, new_range), - } -} - -/// Shortcut for diffing slices with a specific algorithm. -pub fn diff_slices(alg: Algorithm, d: &mut D, old: &[T], new: &[T]) -> Result<(), D::Error> -where - D: DiffHook, - T: Eq + Hash + Ord, -{ - diff(alg, d, old, 0..old.len(), new, 0..new.len()) -} +use crate::algorithms::{diff, diff_slices, Capture, Replace}; +use crate::{Algorithm, DiffOp}; /// Creates a diff between old and new with the given algorithm capturing the ops. /// diff --git a/src/lib.rs b/src/lib.rs index b9f1150..686277a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -57,8 +57,6 @@ pub mod algorithms; pub mod text; mod common; -mod hook; mod types; pub use self::common::*; -pub use self::hook::*; pub use self::types::*; diff --git a/src/types.rs b/src/types.rs index c010275..caecb8e 100644 --- a/src/types.rs +++ b/src/types.rs @@ -1,7 +1,7 @@ use std::fmt; use std::ops::{Index, Range}; -use crate::hook::DiffHook; +use crate::algorithms::DiffHook; /// An enum representing a diffing algorithm. #[derive(Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord, Debug)]