|
| 1 | +use super::HardlinkList; |
| 2 | +use crate::{ |
| 3 | + hardlink::{link_path_list, LinkPathList}, |
| 4 | + inode::InodeNumber, |
| 5 | +}; |
| 6 | +use dashmap::DashMap; |
| 7 | +use derive_more::{From, Into, IntoIterator}; |
| 8 | +use pipe_trait::Pipe; |
| 9 | +use std::collections::HashMap; |
| 10 | + |
| 11 | +#[cfg(feature = "json")] |
| 12 | +use serde::{Deserialize, Serialize}; |
| 13 | + |
| 14 | +/// Intermediate format used for construction and inspection of |
| 15 | +/// [`HardlinkList`]'s internal content. |
| 16 | +/// |
| 17 | +/// **Serialization and deserialization:** _(feature: `json`)_ `Reflection` implements |
| 18 | +/// `Serialize` and `Deserialize` traits, this allows functions in `serde_json` to convert |
| 19 | +/// a `Reflection` into/from JSON. |
| 20 | +#[derive(Debug, Clone, PartialEq, Eq, From, Into, IntoIterator)] |
| 21 | +#[cfg_attr(feature = "json", derive(Deserialize, Serialize))] |
| 22 | +pub struct Reflection<Size>(pub HashMap<InodeNumber, Value<Size>>); |
| 23 | + |
| 24 | +/// Size and list of link paths corresponding to an [`InodeNumber`] in [`Reflection`]. |
| 25 | +#[derive(Debug, Clone, PartialEq, Eq)] |
| 26 | +#[cfg_attr(feature = "json", derive(Deserialize, Serialize))] |
| 27 | +pub struct Value<Size> { |
| 28 | + pub size: Size, |
| 29 | + pub links: link_path_list::Reflection, |
| 30 | +} |
| 31 | + |
| 32 | +impl<Size> Value<Size> { |
| 33 | + /// Create a new value. |
| 34 | + fn new(size: Size, links: LinkPathList) -> Self { |
| 35 | + let links = links.into(); |
| 36 | + Value { size, links } |
| 37 | + } |
| 38 | + |
| 39 | + /// Convert the internal [`link_path_list::Reflection`] into a [`LinkPathList`]. |
| 40 | + fn into_list(self) -> (Size, LinkPathList) { |
| 41 | + let Value { size, links } = self; |
| 42 | + (size, links.into()) |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +impl<Size> From<HardlinkList<Size>> for Reflection<Size> { |
| 47 | + fn from(value: HardlinkList<Size>) -> Self { |
| 48 | + value |
| 49 | + .0 |
| 50 | + .into_iter() |
| 51 | + .map(|(ino, (size, links))| (ino, Value::new(size, links))) |
| 52 | + .collect::<HashMap<_, _>>() |
| 53 | + .pipe(Reflection) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +impl<Size> From<Reflection<Size>> for HardlinkList<Size> { |
| 58 | + fn from(value: Reflection<Size>) -> Self { |
| 59 | + value |
| 60 | + .into_iter() |
| 61 | + .map(|(ino, value)| (ino, value.into_list())) |
| 62 | + .collect::<DashMap<_, _>>() |
| 63 | + .pipe(HardlinkList) |
| 64 | + } |
| 65 | +} |
0 commit comments