schematic/
merge.rs

1use crate::config::MergeResult;
2use std::{
3    collections::{BTreeMap, BTreeSet, HashMap, HashSet},
4    hash::Hash,
5};
6
7/// Discard both previous and next values and return [`None`].
8pub fn discard<T, C>(_: T, _: T, _: &C) -> MergeResult<T> {
9    Ok(None)
10}
11
12/// Always preserve the previous value over the next value.
13pub fn preserve<T, C>(prev: T, _: T, _: &C) -> MergeResult<T> {
14    Ok(Some(prev))
15}
16
17/// Always replace the previous value with the next value.
18pub fn replace<T, C>(_: T, next: T, _: &C) -> MergeResult<T> {
19    Ok(Some(next))
20}
21
22/// Append the items from the next vector to the end of the previous vector.
23pub fn append_vec<T, C>(mut prev: Vec<T>, next: Vec<T>, _: &C) -> MergeResult<Vec<T>> {
24    prev.extend(next);
25
26    Ok(Some(prev))
27}
28
29/// Prepend the items from the next vector to the start of the previous vector.
30pub fn prepend_vec<T, C>(prev: Vec<T>, next: Vec<T>, _: &C) -> MergeResult<Vec<T>> {
31    let mut new = vec![];
32    new.extend(next);
33    new.extend(prev);
34
35    Ok(Some(new))
36}
37
38/// Shallow merge the next [`BTreeMap`] into the previous [`BTreeMap`]. Any items in the
39/// next [`BTreeMap`] will overwrite items in the previous [`BTreeMap`] of the same key.
40pub fn merge_btreemap<K, V, C>(
41    mut prev: BTreeMap<K, V>,
42    next: BTreeMap<K, V>,
43    _: &C,
44) -> MergeResult<BTreeMap<K, V>>
45where
46    K: Eq + Hash + Ord,
47{
48    for (key, value) in next {
49        prev.insert(key, value);
50    }
51
52    Ok(Some(prev))
53}
54
55/// Shallow merge the next [`BTreeSet`] into the previous [`BTreeSet`], overwriting duplicates.
56pub fn merge_btreeset<T, C>(
57    mut prev: BTreeSet<T>,
58    next: BTreeSet<T>,
59    _: &C,
60) -> MergeResult<BTreeSet<T>>
61where
62    T: Eq + Hash + Ord,
63{
64    for item in next {
65        prev.insert(item);
66    }
67
68    Ok(Some(prev))
69}
70
71/// Shallow merge the next [`HashMap`] into the previous [`HashMap`]. Any items in the
72/// next [`HashMap`] will overwrite items in the previous [`HashMap`] of the same key.
73pub fn merge_hashmap<K, V, C>(
74    mut prev: HashMap<K, V>,
75    next: HashMap<K, V>,
76    _: &C,
77) -> MergeResult<HashMap<K, V>>
78where
79    K: Eq + Hash,
80{
81    for (key, value) in next {
82        prev.insert(key, value);
83    }
84
85    Ok(Some(prev))
86}
87
88/// Shallow merge the next [`HashSet`] into the previous [`HashSet`], overwriting duplicates.
89pub fn merge_hashset<T, C>(mut prev: HashSet<T>, next: HashSet<T>, _: &C) -> MergeResult<HashSet<T>>
90where
91    T: Eq + Hash,
92{
93    for item in next {
94        prev.insert(item);
95    }
96
97    Ok(Some(prev))
98}