Skip to main content

cumulo_dipa/
lib.rs

1//! dipa makes it easy to efficiently delta encode large Rust data structures.
2//!
3//! # Repository
4//!
5//! The source code is available at [https://github.com/chinedufn/dipa]
6//!
7//! # Documentation
8//!
9//! The Dipa Book introduces you to dipa and teaches you how to use it.
10//!
11//! It is available online at [https://chinedufn.github.io/dipa/]
12//!
13//! You can also view the book offline:
14//!
15//! ```sh,no_run,ignore
16//! # Do this once while online
17//! git clone git@github.com:chinedufn/dipa.git && cd dipa
18//! cargo install mdbook
19//!
20//! # This works offline
21//! ./bin/serve-book.sh
22//! ```
23
24#[macro_use]
25extern crate serde;
26
27#[cfg(feature = "derive")]
28pub use cumulo_dipa_derive::DiffPatch;
29
30mod sequence;
31
32mod bool;
33mod cow;
34mod float;
35mod integer;
36mod map;
37mod null;
38mod option;
39mod set;
40mod string;
41mod tuple;
42
43#[macro_use]
44mod number_impl;
45
46mod delta_n;
47
48#[cfg(any(test, feature = "impl-tester"))]
49mod dipa_impl_tester;
50#[cfg(any(test, feature = "impl-tester"))]
51pub use self::dipa_impl_tester::DipaImplTester;
52
53/// The type returned by [Diffable.create_delta_towards].
54pub struct CreatedDelta<D> {
55    /// The diff between the start and end value.
56    pub delta: D,
57    /// True if changed, false if same.
58    pub did_change: bool,
59}
60
61/// Allows a type to be diffed with another type.
62pub trait Diffable<'s, 'e, Other: ?Sized> {
63    /// This will typically hold references to data from the structs that are being diffed.
64    type Delta;
65
66    /// This will typically be an owned version of [`Self::Delta`].
67    type DeltaOwned;
68
69    /// Diff self with some target end state, generating a patch that would convert
70    ///  self -> end_state.
71    fn create_delta_towards(&'s self, end_state: &'e Other) -> CreatedDelta<Self::Delta>;
72}
73
74/// Modifies a type using n a patch.
75///
76/// You would typically create this patch using [`Diffable.create_delta_towards`].
77///
78/// You'll typically serialize to a [`Diffable::Delta`], send it over a network and then deserialize
79/// to [`Diffable::DeltaOwned`]. Then you would use that owned delta as the patch to apply via
80/// [`Patchable.apply_patch`].
81///
82/// FIXME: This should return a result since it is possible to, for example, accidentally apply a
83///  a patch to the wrong data structure do to a logical bug.
84pub trait Patchable<P> {
85    /// Apply a patch.
86    fn apply_patch(&mut self, patch: P);
87}