parallel_disk_usage/hardlink/link_path_list/
reflection.rs

1use super::LinkPathList;
2use derive_more::{From, Into, IntoIterator};
3use pipe_trait::Pipe;
4use std::{collections::HashSet, path::PathBuf};
5
6#[cfg(feature = "json")]
7use serde::{Deserialize, Serialize};
8
9/// Intermediate format used for construction and inspection of [`LinkPathList`]'s
10/// internal content.
11///
12/// **Equality:** `Reflection` implements `PartialEq` and `Eq` traits.
13///
14/// **Serialization and deserialization:** _(feature: `json`)_ `Reflection` implements
15/// `Serialize` and `Deserialize` traits, this allows functions in `serde_json` to convert
16/// a `Reflection` into/from JSON.
17#[derive(Debug, Default, Clone, PartialEq, Eq, From, Into, IntoIterator)]
18#[cfg_attr(feature = "json", derive(Deserialize, Serialize))]
19pub struct Reflection(pub HashSet<PathBuf>);
20
21impl Reflection {
22    /// Create an empty reflection.
23    #[inline]
24    pub fn new() -> Self {
25        Reflection::default()
26    }
27
28    /// Get the number of paths in the reflection.
29    #[inline]
30    pub fn len(&self) -> usize {
31        self.0.len()
32    }
33
34    /// Check whether the reflection has any path.
35    #[inline]
36    pub fn is_empty(&self) -> bool {
37        self.0.is_empty()
38    }
39}
40
41impl From<LinkPathList> for Reflection {
42    fn from(value: LinkPathList) -> Self {
43        value.0.into_iter().collect::<HashSet<_>>().pipe(Reflection)
44    }
45}
46
47impl From<Reflection> for LinkPathList {
48    fn from(value: Reflection) -> Self {
49        value.0.into_iter().collect::<Vec<_>>().pipe(LinkPathList)
50    }
51}