Added optional serde support

This commit is contained in:
Armin Ronacher 2021-10-03 08:46:57 +02:00
parent e3c6e5498a
commit f0e38d862b
6 changed files with 72 additions and 0 deletions

View file

@ -5,6 +5,7 @@ All notable changes to similar are documented here.
## 2.1.0 ## 2.1.0
* Removed deprecated alternative slice diffing functions. * Removed deprecated alternative slice diffing functions.
* Added `serde` feature to allow serialization with serde.
## 2.0.0 ## 2.0.0

View file

@ -28,10 +28,12 @@ bytes = ["bstr", "text"]
[dev-dependencies] [dev-dependencies]
insta = "1.6.2" insta = "1.6.2"
console = "0.14.0" console = "0.14.0"
serde_json = "1.0.68"
[dependencies] [dependencies]
unicode-segmentation = { version = "1.7.1", optional = true } unicode-segmentation = { version = "1.7.1", optional = true }
bstr = { version = "0.2.14", optional = true, default-features = false } bstr = { version = "0.2.14", optional = true, default-features = false }
serde = { version = "1.0.130", optional = true, features = ["derive"] }
[[example]] [[example]]
name = "patience" name = "patience"

View file

@ -142,6 +142,8 @@
//! * `inline`: this feature gives access to additional functionality of the //! * `inline`: this feature gives access to additional functionality of the
//! text diffing to provide inline information about which values changed //! text diffing to provide inline information about which values changed
//! in a line diff. This currently also enables the `unicode` feature. //! in a line diff. This currently also enables the `unicode` feature.
//! * `serde`: this feature enables serialization to some types in this
//! crate. For enums without payload deserialization is then also supported.
#![warn(missing_docs)] #![warn(missing_docs)]
pub mod algorithms; pub mod algorithms;
pub mod iter; pub mod iter;

View file

@ -107,6 +107,7 @@ fn push_values<'s, T: DiffableStr + ?Sized>(
/// ///
/// This is like [`Change`] but with inline highlight info. /// This is like [`Change`] but with inline highlight info.
#[derive(Debug, PartialEq, Eq, Hash, Clone, Ord, PartialOrd)] #[derive(Debug, PartialEq, Eq, Hash, Clone, Ord, PartialOrd)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct InlineChange<'s, T: DiffableStr + ?Sized> { pub struct InlineChange<'s, T: DiffableStr + ?Sized> {
tag: ChangeTag, tag: ChangeTag,
old_index: Option<usize>, old_index: Option<usize>,
@ -317,3 +318,20 @@ fn test_line_ops_inline() {
.collect::<Vec<_>>(); .collect::<Vec<_>>();
insta::assert_debug_snapshot!(&changes); insta::assert_debug_snapshot!(&changes);
} }
#[test]
#[cfg(feature = "serde")]
fn test_serde() {
let diff = TextDiff::from_lines(
"Hello World\nsome stuff here\nsome more stuff here\n\nAha stuff here\nand more stuff",
"Stuff\nHello World\nsome amazing stuff here\nsome more stuff here\n",
);
assert_eq!(diff.newline_terminated(), true);
let changes = diff
.ops()
.iter()
.flat_map(|op| diff.iter_inline_changes(op))
.collect::<Vec<_>>();
let json = serde_json::to_string_pretty(&changes).unwrap();
insta::assert_snapshot!(&json);
}

View file

@ -740,3 +740,31 @@ fn test_lifetimes_on_iter() {
let changes = diff_lines(&a, &b); let changes = diff_lines(&a, &b);
insta::assert_debug_snapshot!(&changes); insta::assert_debug_snapshot!(&changes);
} }
#[test]
#[cfg(feature = "serde")]
fn test_serde() {
let diff = TextDiff::from_lines(
"Hello World\nsome stuff here\nsome more stuff here\n\nAha stuff here\nand more stuff",
"Stuff\nHello World\nsome amazing stuff here\nsome more stuff here\n",
);
let changes = diff
.ops()
.iter()
.flat_map(|op| diff.iter_changes(op))
.collect::<Vec<_>>();
let json = serde_json::to_string_pretty(&changes).unwrap();
insta::assert_snapshot!(&json);
}
#[test]
#[cfg(feature = "serde")]
fn test_serde_ops() {
let diff = TextDiff::from_lines(
"Hello World\nsome stuff here\nsome more stuff here\n\nAha stuff here\nand more stuff",
"Stuff\nHello World\nsome amazing stuff here\nsome more stuff here\n",
);
let changes = diff.ops();
let json = serde_json::to_string_pretty(&changes).unwrap();
insta::assert_snapshot!(&json);
}

View file

@ -7,6 +7,11 @@ use crate::iter::ChangesIter;
/// An enum representing a diffing algorithm. /// An enum representing a diffing algorithm.
#[derive(Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord, Debug)] #[derive(Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord, Debug)]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
serde(rename_all = "snake_case")
)]
pub enum Algorithm { pub enum Algorithm {
/// Picks the myers algorithm from [`crate::algorithms::myers`] /// Picks the myers algorithm from [`crate::algorithms::myers`]
Myers, Myers,
@ -25,6 +30,11 @@ impl Default for Algorithm {
/// The tag of a change. /// The tag of a change.
#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy, Ord, PartialOrd)] #[derive(Debug, PartialEq, Eq, Hash, Clone, Copy, Ord, PartialOrd)]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
serde(rename_all = "snake_case")
)]
pub enum ChangeTag { pub enum ChangeTag {
/// The change indicates equality (not a change) /// The change indicates equality (not a change)
Equal, Equal,
@ -59,6 +69,7 @@ impl fmt::Display for ChangeTag {
/// This type has additional methods that are only available for types /// This type has additional methods that are only available for types
/// implementing [`DiffableStr`](crate::text::DiffableStr). /// implementing [`DiffableStr`](crate::text::DiffableStr).
#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy, Ord, PartialOrd)] #[derive(Debug, PartialEq, Eq, Hash, Clone, Copy, Ord, PartialOrd)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct Change<T> { pub struct Change<T> {
pub(crate) tag: ChangeTag, pub(crate) tag: ChangeTag,
pub(crate) old_index: Option<usize>, pub(crate) old_index: Option<usize>,
@ -98,6 +109,11 @@ impl<T: Clone> Change<T> {
/// ///
/// This is used by [`Capture`](crate::algorithms::Capture). /// This is used by [`Capture`](crate::algorithms::Capture).
#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy)] #[derive(Debug, PartialEq, Eq, Hash, Clone, Copy)]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
serde(rename_all = "snake_case", tag = "op")
)]
pub enum DiffOp { pub enum DiffOp {
/// A segment is equal (see [`DiffHook::equal`]) /// A segment is equal (see [`DiffHook::equal`])
Equal { Equal {
@ -141,6 +157,11 @@ pub enum DiffOp {
/// The tag of a diff operation. /// The tag of a diff operation.
#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy, Ord, PartialOrd)] #[derive(Debug, PartialEq, Eq, Hash, Clone, Copy, Ord, PartialOrd)]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
serde(rename_all = "snake_case")
)]
pub enum DiffTag { pub enum DiffTag {
/// The diff op encodes an equal segment. /// The diff op encodes an equal segment.
Equal, Equal,