arcs_ctf_yaml_parser/files/
structs.rs

1use std::fmt::Debug;
2use std::path::{PathBuf, Path};
3
4
5#[derive(Clone, PartialEq)]
6pub struct Files(pub (super) Vec<File>);
7impl Files {
8    pub fn iter_paths(&self) -> impl Iterator<Item = &Path> {
9        self.0.iter().map(|f| f.path.as_path())
10    }
11    pub fn iter(&self) -> impl Iterator<Item = &File> {
12        self.0.iter()
13    }
14    pub fn slice(&self) -> &[File] {
15        &self.0
16    }
17}
18impl Debug for Files {
19    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
20        write!(f, "Files ")?;
21        f.debug_list()
22            .entries(self.0.iter())
23            .finish()
24    }
25}
26
27#[derive(Clone, Copy, PartialEq)]
28pub enum ContainerType {
29    Nc,
30    Admin,
31    Web,
32    Static,
33}
34impl ContainerType {
35    pub fn try_from_str(s: &str) -> Option<Self> {
36        match s {
37            "nc" => Some(ContainerType::Nc),
38            "admin" => Some(ContainerType::Admin),
39            "web" => Some(ContainerType::Web),
40            "static" => Some(ContainerType::Static),
41            _ => None,
42        }
43    }
44    pub fn to_str(&self) -> &'static str {
45        match self {
46            ContainerType::Nc => "nc",
47            ContainerType::Admin => "admin",
48            ContainerType::Web => "web",
49            ContainerType::Static => "static",
50        }
51    }
52}
53impl Debug for ContainerType {
54    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
55        f.write_str(self.to_str())
56    }
57}
58
59
60#[derive(Clone, PartialEq)]
61pub struct File {
62    pub (super) path: PathBuf,
63    pub (super) visible: bool,
64    pub (super) alias: Option<String>,
65
66    pub (super) data: once_cell::sync::OnceCell<Vec<u8>>,
67
68    pub (super) container: Option<ContainerType>,
69}
70impl File {
71    pub fn path(&self) -> &Path { &self.path }
72    pub fn visible(&self) -> bool { self.visible }
73    pub fn alias(&self) -> Option<&str> { self.alias.as_deref() }
74    pub fn container(&self) -> Option<ContainerType> { self.container }
75    pub fn data(&self) -> Option<&[u8]> { self.data.get().map(Vec::as_slice) }
76    pub fn data_vec(self) -> Option<Vec<u8>> { self.data.into_inner() }
77    pub fn data_vec_cloned(&self) -> Option<Vec<u8>> { self.data.get().cloned() }
78}
79impl Debug for File {
80    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
81        write!(f, "File< ")?;
82        
83        if let Some(alias) = self.alias.as_ref() {
84            write!(f, "@{} => {alias}", self.path.display())?;
85        } else {
86            write!(f, "@{}", self.path.display())?;
87        }
88        write!(f, " | ")?;
89
90        if let Some(cont_type) = self.container {
91            write!(f, "{}", cont_type.to_str())?;
92            if self.visible {
93                write!(f, ", visible")?;
94            }
95        } else if self.visible {
96            write!(f, "visible")?;
97        } else {
98            write!(f, "unused")?;
99        }
100        write!(f, " >")?;
101
102        Ok(())
103    }
104}