-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from fwcd/diff-trait
Replace `NodeDiff` with derived `Diff` trait
- Loading branch information
Showing
9 changed files
with
262 additions
and
141 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,7 @@ | ||
mod diff; | ||
mod modifier; | ||
mod node; | ||
mod shape; | ||
|
||
pub(crate) use diff::*; | ||
pub use modifier::*; | ||
pub use node::*; | ||
pub use shape::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use super::{IdPath, IdPathBuf}; | ||
|
||
/// A type that can be diffed in terms of id paths. | ||
pub trait Diff: Sized { | ||
/// Appends the difference to "construct" this type from the given other one | ||
/// to the given difference. | ||
fn record_diff<'a>(&'a self, old: &'a Self, id_path: &IdPath, difference: &mut Difference<&'a Self>); | ||
|
||
/// Computes the difference to "construct" this type from the given other one. | ||
fn diff<'a>(&'a self, old: &'a Self) -> Difference<&'a Self> { | ||
let mut difference = Difference::new(); | ||
self.record_diff(old, IdPath::root(), &mut difference); | ||
return difference; | ||
} | ||
} | ||
|
||
/// The difference between two values. | ||
#[derive(Debug, PartialEq, Eq, Clone)] | ||
pub struct Difference<T> { | ||
pub removed: Vec<(IdPathBuf, T)>, | ||
pub changed: Vec<(IdPathBuf, T, T)>, | ||
pub added: Vec<(IdPathBuf, T)>, | ||
} | ||
|
||
impl<T> Difference<T> { | ||
pub fn new() -> Self { | ||
Self { | ||
removed: Vec::new(), | ||
changed: Vec::new(), | ||
added: Vec::new(), | ||
} | ||
} | ||
|
||
pub fn map<U>(self, mut f: impl FnMut(T) -> U) -> Difference<U> { | ||
Difference { | ||
removed: self.removed.into_iter().map(|(p, x)| (p, f(x))).collect(), | ||
changed: self.changed.into_iter().map(|(p, x, y)| (p, f(x), f(y))).collect(), | ||
added: self.added.into_iter().map(|(p, x)| (p, f(x))).collect(), | ||
} | ||
} | ||
} | ||
|
||
impl<T> Default for Difference<T> { | ||
fn default() -> Self { | ||
Self::new() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,3 +12,4 @@ proc-macro = true | |
[dependencies] | ||
syn = "2.0" | ||
quote = "1.0" | ||
proc-macro2 = "1.0.86" |
Oops, something went wrong.