| 1 | //! Implementations for types in std. |
| 2 | |
| 3 | use crate::Diffable; |
| 4 | use std::{ |
| 5 | collections::{HashMap, HashSet}, |
| 6 | ffi::{OsStr, OsString}, |
| 7 | hash::Hash, |
| 8 | path::{Path, PathBuf}, |
| 9 | }; |
| 10 | |
| 11 | leaf! { Path, OsStr } |
| 12 | leaf_deref! { PathBuf => Path, OsString => OsStr } |
| 13 | |
| 14 | map_diff!( |
| 15 | /// A diff of two [`HashMap`] instances. |
| 16 | /// |
| 17 | /// The diff contains three elements: |
| 18 | /// |
| 19 | /// - `common`: Entries that are present in both maps, with their values |
| 20 | /// stored as a [`Leaf`][crate::Leaf]. |
| 21 | /// - `added`: Entries present in `after`, but not in `before`. |
| 22 | /// - `removed`: Entries present in `before`, but not in `after`. |
| 23 | /// |
| 24 | /// If `V` implements `Eq`, `common` can be split into |
| 25 | /// [`unchanged`][Self::unchanged] and [`modified`][Self::modified] entries. |
| 26 | /// Additionally, if `V` implements [`Diffable`], |
| 27 | /// [`modified_diff`][Self::modified_diff] can be used to recursively diff |
| 28 | /// modified entries. |
| 29 | /// |
| 30 | /// # Example |
| 31 | /// |
| 32 | /// ``` |
| 33 | /// # #[cfg(feature = "std")] { |
| 34 | /// use daft::{Diffable, HashMapDiff, Leaf}; |
| 35 | /// use std::collections::HashMap; |
| 36 | /// |
| 37 | /// let a: HashMap<usize, &str> = |
| 38 | /// [(0, "lorem"), (1, "ipsum"), (2, "dolor")].into_iter().collect(); |
| 39 | /// let b: HashMap<usize, &str> = |
| 40 | /// [(1, "ipsum"), (2, "sit"), (3, "amet")].into_iter().collect(); |
| 41 | /// |
| 42 | /// let changes = a.diff(&b); |
| 43 | /// let expected = HashMapDiff { |
| 44 | /// // Keys are stored by reference and matched by equality. |
| 45 | /// common: [ |
| 46 | /// (&1, Leaf { before: &"ipsum", after: &"ipsum" }), |
| 47 | /// (&2, Leaf { before: &"dolor", after: &"sit" }), |
| 48 | /// ].into_iter().collect(), |
| 49 | /// added: [(&3, &"amet")].into_iter().collect(), |
| 50 | /// removed: [(&0, &"lorem")].into_iter().collect(), |
| 51 | /// }; |
| 52 | /// |
| 53 | /// assert_eq!(changes, expected); |
| 54 | /// |
| 55 | /// // If the values are `Eq`, it's also possible to get lists of |
| 56 | /// // modified and unchanged entries. |
| 57 | /// assert!(changes.is_unchanged(&1)); |
| 58 | /// assert!(changes.is_modified(&2)); |
| 59 | /// let mut unchanged = changes.unchanged().collect::<Vec<_>>(); |
| 60 | /// unchanged.sort_by_key(|(k, _)| *k); |
| 61 | /// let mut modified = changes.modified().collect::<Vec<_>>(); |
| 62 | /// modified.sort_by_key(|(k, _)| *k); |
| 63 | /// |
| 64 | /// assert_eq!(unchanged, [(&1, &"ipsum")]); |
| 65 | /// assert_eq!(modified, [(&2, Leaf { before: &"dolor", after: &"sit" })]); |
| 66 | /// # } |
| 67 | /// ``` |
| 68 | HashMap, Hash |
| 69 | ); |
| 70 | set_diff!( |
| 71 | /// A diff of two [`HashSet`] instances. |
| 72 | /// |
| 73 | /// The diff contains three elements: |
| 74 | /// |
| 75 | /// - `common`: Entries that are present in both sets. |
| 76 | /// - `added`: Entries present in `after`, but not in `before`. |
| 77 | /// - `removed`: Entries present in `before`, but not in `after`. |
| 78 | /// |
| 79 | /// # Example |
| 80 | /// |
| 81 | /// ``` |
| 82 | /// # #[cfg(feature = "std")] { |
| 83 | /// use daft::{Diffable, HashSetDiff}; |
| 84 | /// use std::collections::HashSet; |
| 85 | /// |
| 86 | /// let a: HashSet<usize> = [0, 1].into_iter().collect(); |
| 87 | /// let b: HashSet<usize> = [1, 2].into_iter().collect(); |
| 88 | /// |
| 89 | /// let changes = a.diff(&b); |
| 90 | /// let expected = HashSetDiff { |
| 91 | /// // Entries are stored by reference and matched by equality. |
| 92 | /// common: [&1].into_iter().collect(), |
| 93 | /// added: [&2].into_iter().collect(), |
| 94 | /// removed: [&0].into_iter().collect(), |
| 95 | /// }; |
| 96 | /// |
| 97 | /// assert_eq!(changes, expected); |
| 98 | /// # } |
| 99 | /// ``` |
| 100 | HashSet, Hash |
| 101 | ); |
| 102 | |
| 103 | #[cfg(test)] |
| 104 | mod tests { |
| 105 | use super::*; |
| 106 | use crate::Leaf; |
| 107 | |
| 108 | #[test] |
| 109 | fn hash_set_diff() { |
| 110 | let a: HashSet<_> = [0, 1, 2, 3, 4, 5].into_iter().collect(); |
| 111 | let b: HashSet<_> = [3, 4, 5, 6, 7, 8].into_iter().collect(); |
| 112 | let changes = a.diff(&b); |
| 113 | |
| 114 | let expected = HashSetDiff { |
| 115 | added: [&6, &7, &8].into_iter().collect(), |
| 116 | removed: [&0, &1, &2].into_iter().collect(), |
| 117 | common: [&3, &4, &5].into_iter().collect(), |
| 118 | }; |
| 119 | assert_eq!(expected, changes); |
| 120 | } |
| 121 | |
| 122 | #[test] |
| 123 | fn hash_map_diff() { |
| 124 | let a: HashMap<_, _> = [(0, 1), (1, 1), (2, 1)].into_iter().collect(); |
| 125 | let b: HashMap<_, _> = [(0, 2), (2, 1), (3, 1)].into_iter().collect(); |
| 126 | |
| 127 | let changes = a.diff(&b); |
| 128 | let expected = HashMapDiff { |
| 129 | common: [ |
| 130 | (&0, Leaf { before: &1, after: &2 }), |
| 131 | (&2, Leaf { before: &1, after: &1 }), |
| 132 | ] |
| 133 | .into_iter() |
| 134 | .collect(), |
| 135 | added: [(&3, &1)].into_iter().collect(), |
| 136 | removed: [(&1, &1)].into_iter().collect(), |
| 137 | }; |
| 138 | |
| 139 | assert_eq!(changes, expected); |
| 140 | |
| 141 | // Ensure that keys don't need to be Diffable, and values don't need to |
| 142 | // be Eq or Diffable. |
| 143 | #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] |
| 144 | struct K(i32); |
| 145 | |
| 146 | #[derive(Debug)] |
| 147 | #[expect(dead_code)] |
| 148 | struct V(f64); |
| 149 | |
| 150 | let floats_a: HashMap<K, V> = |
| 151 | [(K(0), V(1.0)), (K(1), V(1.0)), (K(2), V(1.0))] |
| 152 | .into_iter() |
| 153 | .collect(); |
| 154 | let floats_b: HashMap<K, V> = |
| 155 | [(K(0), V(2.0)), (K(2), V(1.0)), (K(3), V(1.0))] |
| 156 | .into_iter() |
| 157 | .collect(); |
| 158 | |
| 159 | let diff = floats_a.diff(&floats_b); |
| 160 | assert_eq!(diff.added.keys().copied().collect::<Vec<_>>(), [&K(3)]); |
| 161 | assert_eq!(diff.removed.keys().copied().collect::<Vec<_>>(), [&K(1)]); |
| 162 | // HashMaps have non-deterministic order, so they should be sorted |
| 163 | // before comparison. |
| 164 | let mut common = diff.common.keys().copied().collect::<Vec<_>>(); |
| 165 | common.sort(); |
| 166 | assert_eq!(common, [&K(0), &K(2)]); |
| 167 | } |
| 168 | } |