anda_db_utils/
lib.rs

1use core::ops::Deref;
2use rustc_hash::{FxBuildHasher, FxHashSet};
3use serde::{
4    de::{Deserialize, DeserializeOwned, Deserializer},
5    ser::{Serialize, Serializer},
6};
7use std::hash::Hash;
8
9mod cbor_size;
10
11pub use cbor_size::estimate_cbor_size;
12
13/// A trait for functional-style method chaining.
14///
15/// Allows any value to be passed through a function, enabling
16/// fluent interfaces and functional programming patterns.
17pub trait Pipe<T> {
18    /// Passes the value through a function.
19    ///
20    /// # Arguments
21    ///
22    /// * `f` - Function to apply to the value
23    ///
24    /// # Returns
25    ///
26    /// The result of applying the function to the value
27    fn pipe<F, R>(self, f: F) -> R
28    where
29        F: FnOnce(Self) -> R,
30        Self: Sized;
31}
32
33impl<T> Pipe<T> for T {
34    fn pipe<F, R>(self, f: F) -> R
35    where
36        F: FnOnce(Self) -> R,
37    {
38        f(self)
39    }
40}
41
42/// A helper utility to efficiently push or extend a `Vec` with unique items.
43///
44/// This struct maintains an internal `HashSet` to keep track of existing items,
45/// providing an optimized way to perform multiple non-existent insertions.
46/// It is designed to be used with a `Vec` that it helps manage.
47///
48/// # Examples
49///
50/// ```rust
51/// use anda_db_utils::UniqueVec;
52///
53/// let vec = vec![1, 2, 3];
54/// let mut extender = UniqueVec::from(vec);
55///
56/// // Push an item that already exists (no change)
57/// extender.push(2);
58/// assert_eq!(extender.as_ref(), &[1, 2, 3]);
59///
60/// // Push a new item
61/// extender.push(4);
62/// assert_eq!(extender.as_ref(), &[1, 2, 3, 4]);
63///
64/// // Extend with a list of items
65/// extender.extend(vec![3, 5, 6]);
66/// assert_eq!(extender.as_ref(), &[1, 2, 3, 4, 5, 6]);
67/// ```
68#[derive(Clone, Debug)]
69pub struct UniqueVec<T> {
70    set: FxHashSet<T>,
71    vec: Vec<T>,
72}
73
74impl<T> Default for UniqueVec<T> {
75    /// Creates an empty `UniqueVec`.
76    fn default() -> Self {
77        Self {
78            set: FxHashSet::default(),
79            vec: Vec::new(),
80        }
81    }
82}
83
84impl<T> From<Vec<T>> for UniqueVec<T>
85where
86    T: Eq + Hash + Clone,
87{
88    /// Creates a `UniqueVec` from a `Vec`.
89    ///
90    /// The extender is initialized with all the unique items from the vector.
91    fn from(vec: Vec<T>) -> Self {
92        let set: FxHashSet<T> = vec.iter().cloned().collect();
93        if set.len() == vec.len() {
94            return Self { set, vec };
95        };
96        let mut this = Self::default();
97        this.extend(vec);
98        this
99    }
100}
101
102impl<T> FromIterator<T> for UniqueVec<T>
103where
104    T: Eq + Hash + Clone,
105{
106    /// Creates a `UniqueVec` from an iterator.
107    fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
108        let vec: Vec<T> = iter.into_iter().collect();
109        vec.into()
110    }
111}
112
113impl<T> From<UniqueVec<T>> for Vec<T> {
114    /// Converts a `UniqueVec` into a `Vec`.
115    fn from(extender: UniqueVec<T>) -> Self {
116        extender.vec
117    }
118}
119
120impl<T> AsRef<[T]> for UniqueVec<T> {
121    /// Returns a slice containing the entire vector.
122    fn as_ref(&self) -> &[T] {
123        &self.vec
124    }
125}
126
127impl<T> Deref for UniqueVec<T> {
128    type Target = Vec<T>;
129
130    /// Dereferences the `UniqueVec` to a `Vec`.
131    fn deref(&self) -> &Self::Target {
132        &self.vec
133    }
134}
135
136impl<T> UniqueVec<T>
137where
138    T: Eq + Hash + Clone,
139{
140    /// Creates a new, empty `UniqueVec`.
141    pub fn new() -> Self {
142        UniqueVec::default()
143    }
144
145    /// Creates a new, empty `UniqueVec` with a specified capacity.
146    pub fn with_capacity(capacity: usize) -> Self {
147        UniqueVec {
148            set: FxHashSet::with_capacity_and_hasher(capacity, FxBuildHasher),
149            vec: Vec::with_capacity(capacity),
150        }
151    }
152
153    /// Returns `true` if the `UniqueVec` contains the specified item.
154    pub fn contains(&self, item: &T) -> bool {
155        self.set.contains(item)
156    }
157
158    /// Pushes an item to the vector if it does not already exist.
159    ///
160    /// # Arguments
161    ///
162    /// * `item` - The item to add.
163    ///
164    /// # Returns
165    ///
166    /// `true` if the item was added, `false` otherwise.
167    pub fn push(&mut self, item: T) -> bool {
168        if self.set.insert(item.clone()) {
169            self.vec.push(item);
170            true
171        } else {
172            false
173        }
174    }
175
176    /// Extends the vector with items from an iterator that do not already exist.
177    ///
178    /// # Arguments
179    ///
180    /// * `items` - An iterator providing the items to add.
181    pub fn extend(&mut self, items: impl IntoIterator<Item = T>) {
182        self.vec.extend(
183            items
184                .into_iter()
185                .filter(|item| self.set.insert(item.clone())),
186        );
187    }
188
189    /// Retains only the elements specified by the predicate.
190    pub fn retain<F>(&mut self, mut f: F)
191    where
192        F: FnMut(&T) -> bool,
193    {
194        self.vec.retain(&mut f);
195        self.set = self.vec.iter().cloned().collect();
196    }
197
198    /// Removes and returns the element at `index`.
199    pub fn remove(&mut self, index: usize) -> T {
200        let item = self.vec.remove(index);
201        self.set.remove(&item);
202        item
203    }
204
205    /// Removes **an element** from the vector and returns it.
206    /// The first element that satisfies the predicate will be removed.
207    pub fn remove_if<P>(&mut self, mut predicate: P) -> Option<T>
208    where
209        P: FnMut(&T) -> bool,
210    {
211        if let Some(index) = self.vec.iter().position(&mut predicate) {
212            let item = self.vec.remove(index);
213            self.set.remove(&item);
214            Some(item)
215        } else {
216            None
217        }
218    }
219
220    /// Removes **an element** from the vector and returns it.
221    /// The last element is swapped into its place.
222    pub fn swap_remove_if<P>(&mut self, mut predicate: P) -> Option<T>
223    where
224        P: FnMut(&T) -> bool,
225    {
226        if let Some(index) = self.vec.iter().position(&mut predicate) {
227            let item = self.vec.swap_remove(index);
228            self.set.remove(&item);
229            Some(item)
230        } else {
231            None
232        }
233    }
234
235    /// Intersects the `UniqueVec` with another `UniqueVec`.
236    pub fn intersect_with<'a>(&'a mut self, other: &'a UniqueVec<T>) {
237        self.set = self.set.intersection(&other.set).cloned().collect();
238        self.vec.retain(|item| self.set.contains(item));
239    }
240
241    /// Returns the inner `Vec` of the `UniqueVec`.
242    pub fn into_vec(self) -> Vec<T> {
243        self.vec
244    }
245
246    /// Returns the inner `FxHashSet` of the `UniqueVec`.
247    pub fn into_set(self) -> FxHashSet<T> {
248        self.set
249    }
250
251    /// Converts the `UniqueVec` to a `Vec`.
252    pub fn to_vec(&self) -> Vec<T> {
253        self.vec.clone()
254    }
255
256    /// Converts the `UniqueVec` to a `FxHashSet`.
257    pub fn to_set(&self) -> FxHashSet<T> {
258        self.set.clone()
259    }
260}
261
262impl<T> Serialize for UniqueVec<T>
263where
264    T: Eq + Hash + Clone + Serialize,
265{
266    /// Serializes the `UniqueVec` as a sequence.
267    #[inline]
268    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
269        serializer.collect_seq(self.vec.iter())
270    }
271}
272
273impl<'de, T> Deserialize<'de> for UniqueVec<T>
274where
275    T: Eq + Hash + Clone + DeserializeOwned,
276{
277    /// Deserializes a sequence into a `UniqueVec`.
278    #[inline]
279    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
280        let vec: Vec<T> = Deserialize::deserialize(deserializer)?;
281        Ok(UniqueVec::from(vec))
282    }
283}
284
285/// Utility for counting the size of serialized CBOR data.
286pub struct CountingWriter {
287    count: usize,
288}
289
290impl Default for CountingWriter {
291    /// Creates a new `CountingWriter` with a count of 0.
292    fn default() -> Self {
293        Self::new()
294    }
295}
296
297impl CountingWriter {
298    /// Creates a new `CountingWriter`.
299    pub fn new() -> Self {
300        CountingWriter { count: 0 }
301    }
302
303    /// Returns the current count of bytes written.
304    pub fn size(&self) -> usize {
305        self.count
306    }
307}
308
309impl std::io::Write for CountingWriter {
310    /// Implements the write method for the Write trait.
311    /// This simply counts the bytes without actually writing them.
312    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
313        let len = buf.len();
314        self.count += len;
315        Ok(len)
316    }
317
318    /// Implements the flush method for the Write trait.
319    /// This is a no-op since we're not actually writing data.
320    fn flush(&mut self) -> std::io::Result<()> {
321        Ok(())
322    }
323}
324
325#[cfg(test)]
326mod tests {
327    use super::*;
328    use std::io::Write;
329
330    #[test]
331    fn test_pipe_trait() {
332        // Test basic pipe functionality
333        let result = 5.pipe(|x| x * 2).pipe(|x| x + 1);
334        assert_eq!(result, 11);
335
336        // Test pipe with different types
337        let string_result = "hello"
338            .pipe(|s| s.to_uppercase())
339            .pipe(|s| format!("{} world", s));
340        assert_eq!(string_result, "HELLO world");
341
342        // Test pipe with closure that changes type
343        let vec_result = vec![1, 2, 3].pipe(|v| v.len()).pipe(|len| len as f64);
344        assert_eq!(vec_result, 3.0);
345    }
346
347    #[test]
348    fn test_unique_vec_new() {
349        let uv: UniqueVec<i32> = UniqueVec::new();
350        assert_eq!(uv.len(), 0);
351        assert!(uv.is_empty());
352    }
353
354    #[test]
355    fn test_unique_vec_with_capacity() {
356        let uv: UniqueVec<i32> = UniqueVec::with_capacity(10);
357        assert_eq!(uv.len(), 0);
358        assert_eq!(uv.capacity(), 10);
359    }
360
361    #[test]
362    fn test_unique_vec_from_vec() {
363        let vec = vec![1, 2, 2, 3, 2, 1];
364        let uv = UniqueVec::from(vec);
365        assert_eq!(uv.len(), 3);
366        assert!(uv.contains(&1));
367        assert!(uv.contains(&2));
368        assert!(uv.contains(&3));
369    }
370
371    #[test]
372    fn test_unique_vec_from_iterator() {
373        let uv: UniqueVec<i32> = [2, 2, 1, 3, 2, 1].iter().cloned().collect();
374        assert_eq!(uv.len(), 3);
375        assert!(uv.contains(&2));
376        assert!(uv.contains(&1));
377        assert!(uv.contains(&3));
378    }
379
380    #[test]
381    fn test_unique_vec_push() {
382        let mut uv = UniqueVec::new();
383
384        // Push new items
385        assert!(uv.push(1));
386        assert!(uv.push(2));
387        assert!(uv.push(3));
388        assert_eq!(uv.len(), 3);
389
390        // Push duplicate items
391        assert!(!uv.push(1));
392        assert!(!uv.push(2));
393        assert_eq!(uv.len(), 3);
394
395        // Verify order is maintained
396        assert_eq!(uv.as_ref(), &[1, 2, 3]);
397    }
398
399    #[test]
400    fn test_unique_vec_extend() {
401        let mut uv = UniqueVec::from(vec![1, 2, 3]);
402
403        // Extend with mix of new and existing items
404        uv.extend(vec![3, 4, 5, 2, 6]);
405
406        assert_eq!(uv.len(), 6);
407        assert_eq!(uv.as_ref(), &[1, 2, 3, 4, 5, 6]);
408    }
409
410    #[test]
411    fn test_unique_vec_retain() {
412        let mut uv = UniqueVec::from(vec![1, 2, 3, 4, 5]);
413
414        // Retain only even numbers
415        uv.retain(|&x| x % 2 == 0);
416
417        assert_eq!(uv.len(), 2);
418        assert_eq!(uv.as_ref(), &[2, 4]);
419        assert!(uv.contains(&2));
420        assert!(uv.contains(&4));
421        assert!(!uv.contains(&1));
422        assert!(!uv.contains(&3));
423        assert!(!uv.contains(&5));
424    }
425
426    #[test]
427    fn test_unique_vec_remove() {
428        let mut uv = UniqueVec::from(vec![1, 2, 3, 4, 5]);
429
430        let removed = uv.remove(2); // Remove element at index 2 (value 3)
431        assert_eq!(removed, 3);
432        assert_eq!(uv.len(), 4);
433        assert_eq!(uv.as_ref(), &[1, 2, 4, 5]);
434        assert!(!uv.contains(&3));
435    }
436
437    #[test]
438    #[should_panic]
439    fn test_unique_vec_remove_out_of_bounds() {
440        let mut uv = UniqueVec::from(vec![1, 2, 3]);
441        uv.remove(5); // Should panic
442    }
443
444    #[test]
445    fn test_unique_vec_remove_if() {
446        let mut uv = UniqueVec::from(vec![1, 2, 3, 4, 5]);
447
448        // Remove first even number
449        let removed = uv.remove_if(|&x| x % 2 == 0);
450        assert_eq!(removed, Some(2));
451        assert_eq!(uv.len(), 4);
452        assert_eq!(uv.as_ref(), &[1, 3, 4, 5]);
453        assert!(!uv.contains(&2));
454
455        // Try to remove non-existent condition
456        let removed = uv.remove_if(|&x| x > 10);
457        assert_eq!(removed, None);
458        assert_eq!(uv.len(), 4);
459    }
460
461    #[test]
462    fn test_unique_vec_swap_remove_if() {
463        let mut uv = UniqueVec::from(vec![1, 2, 3, 4, 5]);
464
465        // Remove first even number (swap with last)
466        let removed = uv.swap_remove_if(|&x| x % 2 == 0);
467        assert_eq!(removed, Some(2));
468        assert_eq!(uv.len(), 4);
469        // After swap_remove, the last element (5) should be in position of removed element
470        assert_eq!(uv.as_ref(), &[1, 5, 3, 4]);
471        assert!(!uv.contains(&2));
472    }
473
474    #[test]
475    fn test_unique_vec_contains() {
476        let uv = UniqueVec::from(vec![1, 2, 3]);
477
478        assert!(uv.contains(&1));
479        assert!(uv.contains(&2));
480        assert!(uv.contains(&3));
481        assert!(!uv.contains(&4));
482    }
483
484    #[test]
485    fn test_unique_vec_intersect_with() {
486        let mut uv1 = UniqueVec::from(vec![1, 2, 3, 4, 5]);
487        let uv2 = UniqueVec::from(vec![3, 4, 5, 6, 7]);
488
489        uv1.intersect_with(&uv2);
490
491        assert_eq!(uv1.len(), 3);
492        assert!(uv1.contains(&3));
493        assert!(uv1.contains(&4));
494        assert!(uv1.contains(&5));
495        assert!(!uv1.contains(&1));
496        assert!(!uv1.contains(&2));
497    }
498
499    #[test]
500    fn test_unique_vec_to_vec() {
501        let uv = UniqueVec::from(vec![1, 2, 2, 3]);
502        let vec = uv.to_vec();
503        assert_eq!(vec, vec![1, 2, 3]);
504    }
505
506    #[test]
507    fn test_unique_vec_to_set() {
508        let uv = UniqueVec::from(vec![1, 2, 3]);
509        let set = uv.to_set();
510        assert_eq!(set.len(), 3);
511        assert!(set.contains(&1));
512        assert!(set.contains(&2));
513        assert!(set.contains(&3));
514    }
515
516    #[test]
517    fn test_unique_vec_as_ref() {
518        let uv = UniqueVec::from(vec![1, 2, 3]);
519        let slice: &[i32] = uv.as_ref();
520        assert_eq!(slice, &[1, 2, 3]);
521    }
522
523    #[test]
524    fn test_unique_vec_deref() {
525        let uv = UniqueVec::from(vec![1, 2, 3]);
526        // Test deref by calling Vec methods directly
527        assert_eq!(uv.len(), 3);
528        assert_eq!(uv[0], 1);
529        assert_eq!(uv[1], 2);
530        assert_eq!(uv[2], 3);
531    }
532
533    #[test]
534    fn test_unique_vec_into_vec() {
535        let uv = UniqueVec::from(vec![1, 2, 3]);
536        let vec: Vec<i32> = uv.into();
537        assert_eq!(vec, vec![1, 2, 3]);
538    }
539
540    #[test]
541    fn test_unique_vec_serialize_deserialize() {
542        let uv = UniqueVec::from(vec![1, 2, 2, 3, 2, 1]); // Duplicates should be removed
543
544        // Serialize
545        let json = serde_json::to_string(&uv).unwrap();
546        assert_eq!(json, "[1,2,3]");
547
548        // Deserialize
549        let deserialized: UniqueVec<i32> = serde_json::from_str("[1,3,2,3,3,2,1]").unwrap();
550        assert_eq!(deserialized.len(), 3);
551        assert_eq!(deserialized.as_ref(), &[1, 3, 2]);
552    }
553
554    #[test]
555    fn test_unique_vec_clone() {
556        let uv1 = UniqueVec::from(vec![1, 2, 3]);
557        let uv2 = uv1.clone();
558
559        assert_eq!(uv1.len(), uv2.len());
560        assert_eq!(uv1.as_ref(), uv2.as_ref());
561    }
562
563    #[test]
564    fn test_counting_writer_new() {
565        let writer = CountingWriter::new();
566        assert_eq!(writer.size(), 0);
567    }
568
569    #[test]
570    fn test_counting_writer_default() {
571        let writer = CountingWriter::default();
572        assert_eq!(writer.size(), 0);
573    }
574
575    #[test]
576    fn test_counting_writer_write() {
577        let mut writer = CountingWriter::new();
578
579        let result = writer.write(b"hello");
580        assert!(result.is_ok());
581        assert_eq!(result.unwrap(), 5);
582        assert_eq!(writer.size(), 5);
583
584        let result = writer.write(b" world");
585        assert!(result.is_ok());
586        assert_eq!(result.unwrap(), 6);
587        assert_eq!(writer.size(), 11);
588    }
589
590    #[test]
591    fn test_counting_writer_flush() {
592        let mut writer = CountingWriter::new();
593        let result = writer.flush();
594        assert!(result.is_ok());
595        assert_eq!(writer.size(), 0); // Flush doesn't change size
596    }
597
598    #[test]
599    fn test_counting_writer_multiple_writes() {
600        let mut writer = CountingWriter::new();
601
602        // Multiple writes should accumulate
603        writer.write_all(b"a").unwrap();
604        assert_eq!(writer.size(), 1);
605
606        writer.write_all(b"bc").unwrap();
607        assert_eq!(writer.size(), 3);
608
609        writer.write_all(b"defg").unwrap();
610        assert_eq!(writer.size(), 7);
611    }
612
613    #[test]
614    fn test_counting_writer_empty_write() {
615        let mut writer = CountingWriter::new();
616
617        let result = writer.write(b"");
618        assert!(result.is_ok());
619        assert_eq!(result.unwrap(), 0);
620        assert_eq!(writer.size(), 0);
621    }
622
623    #[test]
624    fn test_unique_vec_edge_cases() {
625        // Test with empty vector
626        let uv = UniqueVec::from(vec![] as Vec<i32>);
627        assert_eq!(uv.len(), 0);
628        assert!(uv.is_empty());
629
630        // Test with single element
631        let mut uv = UniqueVec::from(vec![42]);
632        assert_eq!(uv.len(), 1);
633        assert!(uv.contains(&42));
634
635        // Test removing the only element
636        let removed = uv.remove(0);
637        assert_eq!(removed, 42);
638        assert_eq!(uv.len(), 0);
639        assert!(!uv.contains(&42));
640    }
641
642    #[test]
643    fn test_unique_vec_string_type() {
644        let mut uv = UniqueVec::new();
645
646        uv.push("hello".to_string());
647        uv.push("world".to_string());
648        uv.push("hello".to_string()); // Duplicate
649
650        assert_eq!(uv.len(), 2);
651        assert!(uv.contains(&"hello".to_string()));
652        assert!(uv.contains(&"world".to_string()));
653    }
654}