brume 0.1.0

A library for bidirectional file synchronization
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
//! Implementation of a Vec where the elements are always sorted

use std::cmp::Ordering;

/// Trait used for sorting elements of a vec.
///
/// Compared to Ord, this trait allows the sorting to be done not directly on the element
/// themselves, but on keys that can be extracted.
pub trait Sortable {
    type Key: Ord + ?Sized;

    fn key(&self) -> &Self::Key;
}

/// A vec of elements, sorted and without duplicates. This struct is a wrapper over a `Vec<T>`
/// that keeps the sorting invariant.
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct SortedVec<T>(Vec<T>);

impl<T: Sortable> SortedVec<T> {
    /// Creates a new empty vec
    pub fn new() -> Self {
        Self(Vec::new())
    }

    /// Creates a sorted vec from a vec of elements
    pub fn from_vec(mut vec: Vec<T>) -> Self {
        // Sort and remove duplicates
        vec.sort_by(|a, b| a.key().cmp(b.key()));
        vec.dedup_by(|a, b| a.key() == b.key());

        Self(vec)
    }

    /// Inserts a new element inside an existing vec, without overwriting existing ones.
    ///
    /// Returns false if there is already an element with this key and the provided element was not
    /// inserted. Return true otherwise.
    pub fn insert(&mut self, value: T) -> bool {
        match self
            .0
            .binary_search_by(|candidate| candidate.key().cmp(value.key()))
        {
            Ok(_) => false,
            Err(idx) => {
                self.0.insert(idx, value);
                true
            }
        }
    }

    /// Removes the element with the given key from the vec.
    ///
    /// Returns false if the element was not present in the vec, or true otherwise.
    pub fn remove(&mut self, key: &T::Key) -> bool {
        match self
            .0
            .binary_search_by(|candidate| candidate.key().cmp(key))
        {
            Ok(idx) => {
                self.0.remove(idx);
                true
            }
            Err(_) => false,
        }
    }

    /// Removes the element with the given name if the condition returns true.
    ///
    /// Returns None if the element was not present in the vec, Some(false) is the element was found
    /// but the condition returned false, or Some(true) if the condition returned true and the
    /// element was deleted.
    pub fn remove_if<F: FnOnce(&T) -> bool>(&mut self, key: &T::Key, condition: F) -> Option<bool> {
        match self
            .0
            .binary_search_by(|candidate| candidate.key().cmp(key))
        {
            Ok(idx) => {
                if condition(&self.0[idx]) {
                    self.0.remove(idx);
                    Some(true)
                } else {
                    Some(false)
                }
            }
            Err(_) => None,
        }
    }

    /// Finds the element with the provided key in the vec.
    pub fn find(&self, key: &T::Key) -> Option<&T> {
        match self
            .0
            .binary_search_by(|candidate| candidate.key().cmp(key))
        {
            Ok(idx) => Some(&self.0[idx]),
            Err(_) => None,
        }
    }

    /// Finds the element with the provided key in the vec, return a mutable reference.
    pub fn find_mut(&mut self, key: &T::Key) -> Option<&mut T> {
        match self
            .0
            .binary_search_by(|candidate| candidate.key().cmp(key))
        {
            Ok(idx) => Some(&mut self.0[idx]),
            Err(_) => None,
        }
    }

    /// Returns the length of the vec
    pub fn len(&self) -> usize {
        self.0.len()
    }

    /// Returns true if the vec contains no element
    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }

    /// Returns an iterator over T, by reference.
    pub fn iter(&self) -> std::slice::Iter<T> {
        self.0.iter()
    }

    /// Removes the last element in the vec and returns it
    pub fn pop(&mut self) -> Option<T> {
        self.0.pop()
    }

    /// Iterates on two sorted vecs with the same key types and apply functions on their items:
    ///
    /// - `fself` is applied to items that are only present in "self"
    /// - `fboth` is applied to items that present in both vecs
    /// - `fother` is applied to items that are only present in "other"
    ///
    /// The function calls will be done in order, and the resulting vec will be sorted by the order
    /// of the input vecs.
    pub fn iter_zip_map<U, FSelf, FBoth, FOther, Ret, Err>(
        &self,
        other: &SortedVec<U>,
        mut fself: FSelf,
        mut fboth: FBoth,
        mut fother: FOther,
    ) -> Result<Vec<Ret>, Err>
    where
        U: Sortable<Key = T::Key>,
        FSelf: FnMut(&T) -> Result<Ret, Err>,
        FBoth: FnMut(&T, &U) -> Result<Ret, Err>,
        FOther: FnMut(&U) -> Result<Ret, Err>,
    {
        let mut ret = Vec::new();
        let mut self_iter = self.iter();
        let mut other_iter = other.iter();

        let mut self_item_opt = self_iter.next();
        let mut other_item_opt = other_iter.next();

        while let (Some(self_item), Some(other_item)) = (self_item_opt, other_item_opt) {
            match self_item.key().cmp(other_item.key()) {
                Ordering::Less => {
                    ret.push(fself(self_item)?);
                    self_item_opt = self_iter.next()
                }
                Ordering::Equal => {
                    ret.push(fboth(self_item, other_item)?);
                    self_item_opt = self_iter.next();
                    other_item_opt = other_iter.next();
                }
                Ordering::Greater => {
                    ret.push(fother(other_item)?);
                    other_item_opt = other_iter.next();
                }
            }
        }

        // Handle the remaining elements that are present in an iterator and not the
        // other one
        while let Some(self_item) = self_item_opt {
            ret.push(fself(self_item)?);
            self_item_opt = self_iter.next();
        }

        while let Some(other_item) = other_item_opt {
            ret.push(fother(other_item)?);
            other_item_opt = other_iter.next();
        }

        Ok(ret)
    }

    /// Creates a new [`SortedVec`] from a vector that is already sorted
    ///
    /// If the vec is not sorted, this may result in undefined behavior
    pub fn unchecked_from_vec(vec: Vec<T>) -> Self {
        Self(vec)
    }

    /// Extends a vec with the elements of another one, the vecs are both relatively sorted.
    ///
    /// This means that the last element of self is smaller that the first element of other.
    pub fn unchecked_extend(&mut self, other: Self) {
        self.0.extend(other);
    }

    /// Converts a `Vec<SortedVec>` into a `SortedVec`, assuming that the vecs inside the vec are
    /// already relatively sorted.
    ///
    /// This means that the last element of the vec at index n is always smaller than the
    /// first element of the vec at index n + 1.
    pub fn unchecked_flatten<I: IntoIterator<Item = Self>>(iter: I) -> Self {
        let flattened = iter.into_iter().flat_map(|sorted| sorted.0).collect();

        Self::from_vec(flattened)
    }
}

impl<T> IntoIterator for SortedVec<T> {
    type Item = T;

    type IntoIter = std::vec::IntoIter<T>;

    fn into_iter(self) -> Self::IntoIter {
        self.0.into_iter()
    }
}

impl<T: Sortable> From<Vec<T>> for SortedVec<T> {
    fn from(value: Vec<T>) -> Self {
        Self::from_vec(value)
    }
}

impl<T: Sortable> From<SortedVec<T>> for Vec<T> {
    fn from(value: SortedVec<T>) -> Self {
        value.0
    }
}

impl<T: Sortable + Clone> From<&[T]> for SortedVec<T> {
    fn from(value: &[T]) -> Self {
        Self::from_vec(value.to_vec())
    }
}

impl<const N: usize, T: Sortable + Clone> From<[T; N]> for SortedVec<T> {
    fn from(value: [T; N]) -> Self {
        Self::from_vec(value.to_vec())
    }
}

impl<T: Sortable> Default for SortedVec<T> {
    fn default() -> Self {
        Self::new()
    }
}

impl<T: Clone + Sortable> SortedVec<T> {
    /// Insert a new element inside an existing vec, and eventually overwrite exisisting elements.
    ///
    /// Return the replaced element if present, or None if there was no element with this key.
    pub fn replace(&mut self, value: T) -> Option<T> {
        match self
            .0
            .binary_search_by(|candidate| candidate.key().cmp(value.key()))
        {
            Ok(idx) => {
                let prev = self.0[idx].clone();
                self.0[idx] = value;
                Some(prev)
            }
            Err(idx) => {
                self.0.insert(idx, value);
                None
            }
        }
    }
}

#[cfg(test)]
mod test {
    use crate::{
        test_utils::TestNode::{D, F},
        vfs::dir_tree::NodeKind,
    };

    use super::SortedVec;

    #[test]
    fn test_ordering_creation() {
        let test_nodes = vec![
            F("f1"),
            D("a", vec![]),
            D("f1", vec![]),
            F("f2"),
            F("a"),
            D("b", vec![]),
        ]
        .into_iter()
        .map(|val| val.into_node())
        .collect();

        let vec = SortedVec::from_vec(test_nodes);

        let reference: Vec<_> = [D("a", vec![]), D("b", vec![]), F("f1"), F("f2")]
            .into_iter()
            .map(|val| val.into_node())
            .collect();

        assert!(
            vec.0
                .iter()
                .zip(reference.iter())
                .all(|(a, b)| a.structural_eq(b))
        )
    }

    #[test]
    fn test_insertion() {
        let test_nodes = [D("a", vec![]), D("b", vec![]), F("f1"), F("f2")]
            .into_iter()
            .map(|val| val.into_node())
            .collect();

        let mut vec = SortedVec::from_vec(test_nodes);

        assert!(!vec.insert(F("b").into_node()));
        assert!(vec.insert(D("e", vec![]).into_node()));
        assert!(!vec.insert(D("a", vec![]).into_node()));
        assert!(vec.insert(F("f3").into_node()));

        let reference: Vec<_> = vec![
            D("a", vec![]),
            D("b", vec![]),
            D("e", vec![]),
            F("f1"),
            F("f2"),
            F("f3"),
        ]
        .into_iter()
        .map(|val| val.into_node())
        .collect();

        assert!(
            vec.0
                .iter()
                .zip(reference.iter())
                .all(|(a, b)| a.structural_eq(b))
        )
    }

    #[test]
    fn test_replacement() {
        let test_nodes = [D("a", vec![]), D("b", vec![]), F("f1"), F("f2")]
            .into_iter()
            .map(|val| val.into_node())
            .collect();

        let mut vec = SortedVec::from_vec(test_nodes);

        assert!(vec.replace(F("b").into_node()).is_some());
        assert!(vec.replace(D("e", vec![]).into_node()).is_none());
        assert!(vec.replace(D("a", vec![]).into_node()).is_some());
        assert!(vec.replace(F("f3").into_node()).is_none());

        let reference: Vec<_> = vec![
            D("a", vec![]),
            F("b"),
            D("e", vec![]),
            F("f1"),
            F("f2"),
            F("f3"),
        ]
        .into_iter()
        .map(|val| val.into_node())
        .collect();

        assert!(
            vec.0
                .iter()
                .zip(reference.iter())
                .all(|(a, b)| a.structural_eq(b))
        )
    }

    #[test]
    fn test_removal() {
        let test_nodes = vec![
            D("a", vec![]),
            D("b", vec![]),
            D("e", vec![]),
            F("f1"),
            F("f2"),
            F("f3"),
        ]
        .into_iter()
        .map(|val| val.into_node())
        .collect();

        let mut vec = SortedVec::from_vec(test_nodes);

        assert!(vec.remove("e"));
        assert!(!vec.remove("j"));
        assert!(!vec.remove("e"));
        assert!(
            vec.remove_if("f3", |node| node.kind() == NodeKind::File)
                .unwrap()
        );
        assert_eq!(
            vec.remove_if("a", |node| node.kind() == NodeKind::File),
            Some(false)
        );

        let reference: Vec<_> = [D("a", vec![]), D("b", vec![]), F("f1"), F("f2")]
            .into_iter()
            .map(|val| val.into_node())
            .collect();

        assert!(
            vec.0
                .iter()
                .zip(reference.iter())
                .all(|(a, b)| a.structural_eq(b))
        )
    }

    #[test]
    fn test_pop() {
        let test_nodes = vec![D("a", vec![]), D("b", vec![]), D("c", vec![])]
            .into_iter()
            .map(|val| val.into_node())
            .collect();

        let mut vec = SortedVec::from_vec(test_nodes);

        assert_eq!(vec.pop().unwrap().name(), "c");
        assert_eq!(vec.pop().unwrap().name(), "b");
        assert_eq!(vec.pop().unwrap().name(), "a");
        assert!(vec.pop().is_none());
    }
}