scandir/def/
toc.rs

1use std::path::PathBuf;
2
3#[cfg(feature = "bincode")]
4use bincode::error::EncodeError;
5#[cfg(feature = "speedy")]
6use speedy::{Readable, Writable};
7
8#[cfg_attr(feature = "speedy", derive(Readable, Writable))]
9#[cfg_attr(
10    any(feature = "bincode", feature = "json"),
11    derive(Deserialize, Serialize)
12)]
13#[derive(Debug, Clone, PartialEq)]
14pub struct Toc {
15    pub dirs: Vec<String>,
16    pub files: Vec<String>,
17    pub symlinks: Vec<String>,
18    pub other: Vec<String>,
19    pub errors: Vec<String>,
20}
21
22impl Toc {
23    pub fn new() -> Self {
24        Toc {
25            dirs: Vec::new(),
26            files: Vec::new(),
27            symlinks: Vec::new(),
28            other: Vec::new(),
29            errors: Vec::new(),
30        }
31    }
32
33    pub fn clear(&mut self) {
34        self.dirs.clear();
35        self.files.clear();
36        self.symlinks.clear();
37        self.other.clear();
38        self.errors.clear();
39    }
40
41    pub fn dirs(&self) -> Vec<String> {
42        self.dirs.clone()
43    }
44
45    pub fn files(&self) -> Vec<String> {
46        self.files.clone()
47    }
48
49    pub fn symlinks(&self) -> Vec<String> {
50        self.symlinks.clone()
51    }
52
53    pub fn other(&self) -> Vec<String> {
54        self.other.clone()
55    }
56
57    pub fn errors(&self) -> Vec<String> {
58        self.errors.clone()
59    }
60
61    pub fn is_empty(&self) -> bool {
62        self.dirs.is_empty()
63            && self.files.is_empty()
64            && self.symlinks.is_empty()
65            && self.other.is_empty()
66            && self.errors.is_empty()
67    }
68
69    pub fn extend(&mut self, root_dir: &str, other: &Toc) {
70        self.dirs.extend_from_slice(
71            &other
72                .dirs
73                .iter()
74                .map(|x| PathBuf::from(root_dir).join(x).to_str().unwrap().to_owned())
75                .collect::<Vec<String>>(),
76        );
77        self.files.extend_from_slice(
78            &other
79                .files
80                .iter()
81                .map(|x| PathBuf::from(root_dir).join(x).to_str().unwrap().to_owned())
82                .collect::<Vec<String>>(),
83        );
84        self.symlinks.extend_from_slice(
85            &other
86                .symlinks
87                .iter()
88                .map(|x| PathBuf::from(root_dir).join(x).to_str().unwrap().to_owned())
89                .collect::<Vec<String>>(),
90        );
91        self.other.extend_from_slice(
92            &other
93                .other
94                .iter()
95                .map(|x| PathBuf::from(root_dir).join(x).to_str().unwrap().to_owned())
96                .collect::<Vec<String>>(),
97        );
98        self.errors.extend_from_slice(
99            &other
100                .errors
101                .iter()
102                .map(|x| PathBuf::from(root_dir).join(x).to_str().unwrap().to_owned())
103                .collect::<Vec<String>>(),
104        );
105    }
106
107    #[cfg(feature = "speedy")]
108    pub fn to_speedy(&self) -> Result<Vec<u8>, speedy::Error> {
109        self.write_to_vec()
110    }
111
112    #[cfg(feature = "bincode")]
113    pub fn to_bincode(&self) -> Result<Vec<u8>, EncodeError> {
114        bincode::serde::encode_to_vec(self, bincode::config::legacy())
115    }
116
117    #[cfg(feature = "json")]
118    pub fn to_json(&self) -> serde_json::Result<String> {
119        serde_json::to_string(self)
120    }
121}
122
123impl Default for Toc {
124    fn default() -> Self {
125        Self::new()
126    }
127}