arcs_ctf_yaml_parser/files/
errors.rs1use std::path::PathBuf;
2use std::fmt::{ Display, Debug };
3
4use crate::structs::ValueType;
5
6#[derive(Debug, Clone, PartialEq)]
7pub enum FileParseErr {
8 ItemNotMapping(ValueType),
9
10 Parts {
11 path: Option<FilePathErr>,
12 visible: Option<ValueType>,
13 alias: Option<ValueType>,
14 container: Option<ContainerTypeErr>,
15 data: Option<DataReadErr>,
16 },
17}
18impl Display for FileParseErr {
19 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
20 use FileParseErr::*;
21 match self {
22 ItemNotMapping(t) => write!(f, "each file should be a map of attributes, not {t}."),
23 Parts {
24 path,
25 visible,
26 alias,
27 container,
28 data,
29 } => {
30 writeln!(f, "There were issues with certain parts of this file entry:")?;
31 if let Some(path_err) = path {
32 writeln!(f, " {path_err}")?;
33 }
34 if let Some(vis_err) = visible {
35 writeln!(f, " The visibility of a file must be a boolean or undefined, not {vis_err}")?;
36 }
37 if let Some(alias_err) = alias {
38 writeln!(f, " The display name of a file must be a string or undefined, not {alias_err}")?;
39 }
40 if let Some(cont_err) = container {
41 writeln!(f, " {cont_err}")?;
42 }
43 if let Some(data_err) = data {
44 writeln!(f, " {data_err}")?;
45 }
46
47 Ok(())
48 },
49 }
50 }
51}
52
53#[derive(Debug, Clone, PartialEq)]
54pub enum FilePathErr {
55 NoExist,
56 NotStr(ValueType),
57 BadPath(String),
58}
59impl Display for FilePathErr {
60 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
61 use FilePathErr::*;
62 match self {
63 NoExist => write!(f, "You must define `src`"),
64 NotStr(t) => write!(f, "The source path must be a filepath, not {t}"),
65 BadPath(s) => write!(f, "The source `{s}` is not a valid filepath"),
66 }
67 }
68}
69
70#[derive(Debug, Clone, PartialEq)]
71pub enum ContainerTypeErr {
72 NotStr(ValueType),
73 BadType(String),
74}
75impl Display for ContainerTypeErr {
76 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
77 use ContainerTypeErr::*;
78 match self {
79 NotStr(t) => write!(f, "The container must be `nc`, `admin`, or `web`, not {t}"),
80 BadType(s) => write!(f, "The container must be `nc`, `admin`, or `web`. \"{s}\" is not one of those."),
81 }
82 }
83}
84
85#[derive(Debug, Clone, PartialEq)]
86pub enum DataReadErr {
87 DoesntExist(PathBuf),
88 Canonicalize(PathBuf),
89 OsError(PathBuf),
90}
91impl Display for DataReadErr {
92 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
93 use DataReadErr::*;
94 match self {
95 DoesntExist(p) => write!(f, "The file path `{}` doesn't exist.", p.display()),
96 Canonicalize(p) => write!(f, "There was an issue finding the absolute path to `{}`. Maybe it doesn't exist.", p.display()),
97 OsError(p) => write!(f, "There was an issue reading the file at `{}`. Maybe check permissions?", p.display()),
98 }
99 }
100}
101
102#[derive(Debug, Clone, PartialEq)]
103pub enum FileErrors {
104 BadBaseType(ValueType),
105 EntryErrors(Vec<Option<FileParseErr>>),
106}
107impl Display for FileErrors {
108 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
109 use FileErrors::*;
110 match self {
111 BadBaseType(t) => write!(f, "Files should be a list, not {t}."),
112 EntryErrors(errs) => {
113 writeln!(f, "Some entries under `files` are invalid:")?;
114 let iter = errs
115 .iter()
116 .enumerate()
117 .filter_map(|(idx, err)| err.as_ref().map(|e| (idx, e)));
118
119 for (idx, err) in iter {
120 writeln!(f, " {idx}: {err}")?;
121 }
122 Ok(())
123 },
124 }
125 }
126}