arcs_ctf_yaml_parser/
structs.rs

1
2use std::fmt::Display;
3
4use serde_yaml::Error as YamlError;
5use serde_yaml::Value as YamlValue;
6
7use crate::categories::CategoryError;
8use crate::correctness::YamlCorrectness;
9use crate::deploy::error::DeployOptionsError;
10use crate::files::errors::FileErrors;
11use crate::flag::FlagError;
12use crate::lists::structs::AuthorError;
13use crate::lists::structs::HintError;
14
15#[derive(Debug, Clone, Copy, PartialEq)]
16pub struct ValueType { type_enum: ValueTypeEnum }
17
18#[derive(Debug, Clone, Copy, PartialEq)]
19pub enum ValueTypeEnum { Null, Bool, Number, String, Sequence, Mapping, Tagged }
20pub fn get_type(value: &YamlValue) -> ValueType {
21    use YamlValue::*;
22    use ValueTypeEnum as VTyp;
23    let type_enum = match value {
24        Null => VTyp::Null,
25        Bool(_) => VTyp::Bool,
26        Number(_) => VTyp::Number,
27        String(_) => VTyp::String,
28        Sequence(_) => VTyp::Sequence,
29        Mapping(_) => VTyp::Mapping,
30        Tagged(_) => VTyp::Tagged,
31    };
32    ValueType { type_enum }
33}
34
35impl ValueType {
36    pub const NULL: ValueType = ValueType { type_enum: ValueTypeEnum::Null };
37    pub const BOOL: ValueType = ValueType { type_enum: ValueTypeEnum::Bool };
38    pub const NUMB: ValueType = ValueType { type_enum: ValueTypeEnum::Number };
39    pub const STRI: ValueType = ValueType { type_enum: ValueTypeEnum::String };
40    pub const SEQN: ValueType = ValueType { type_enum: ValueTypeEnum::Sequence };
41    pub const MAPP: ValueType = ValueType { type_enum: ValueTypeEnum::Mapping };
42    pub const TAGG: ValueType = ValueType { type_enum: ValueTypeEnum::Tagged };
43
44    pub fn get_str(self) -> &'static str {
45        use ValueTypeEnum::*;
46
47        match self.type_enum {
48            Null => "`null`",
49            Bool => "a boolean",
50            Number => "a number",
51            String => "a string",
52            Sequence => "a list",
53            Mapping => "a dictionary",
54            Tagged => "an enum",
55        }
56    }
57}
58impl Display for ValueType {
59    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
60        f.write_str(self.get_str())
61    }
62}
63
64#[derive(Debug, Clone)]
65pub enum YamlAttribVerifyError {
66    Categories(CategoryError),
67    Authors(AuthorError),
68    Hints(HintError),
69    Flag(FlagError),
70    Files(FileErrors),
71
72    Deploy(DeployOptionsError),
73
74    NameNotString(ValueType),
75    PointsNotInt(ValueType),
76
77    DescNotString(ValueType),
78    VisNotBool(ValueType),
79}
80
81#[derive(Debug)]
82pub enum YamlVerifyError {
83    Unparsable(YamlError),
84    BaseNotMap(ValueType),
85    PartErrors(Vec<YamlAttribVerifyError>),
86    Correctness(YamlCorrectness),
87    OsError,
88}
89
90impl Display for YamlAttribVerifyError {
91    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
92        use YamlAttribVerifyError::*;
93        match self {
94            NameNotString(vtype) => writeln!(f, "The name should be a string, not {vtype}."),
95            DescNotString(vtype) => writeln!(f, "The description should be a string, not {vtype}."),
96            VisNotBool(vtype) => writeln!(f, "The visibility switch should be a boolean, not {vtype}."),
97            
98            PointsNotInt(ValueType { type_enum: ValueTypeEnum::Number }) => writeln!(f, "The value should be an positive integer, not negative or fractional."),
99            PointsNotInt(vtype)  => writeln!(f, "The value should be an positive integer, not {vtype}."),
100            
101            Flag(flag_err) => writeln!(f, "{flag_err}"),
102            Categories(cat_err) => writeln!(f, "{cat_err}"),
103            Authors(author_err) => writeln!(f, "{author_err}"),
104            Hints(hint_err) => writeln!(f, "{hint_err}"),
105            Files(file_errors) => writeln!(f, "{file_errors}"),
106
107            Deploy(deploy_err) => writeln!(f, "{deploy_err}"),
108        }
109    }
110}
111
112impl Display for YamlVerifyError {
113    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
114        use YamlVerifyError::*;
115        match self {
116            Unparsable(e) => writeln!(f, "Invalid YAML:\n{e}"),
117            BaseNotMap(_) => writeln!(f, "The yaml file must have `key: value` pairs"),
118            PartErrors(errs) => {
119                writeln!(f, "Yaml failed to verify:")?;
120                for err in errs {
121                    write!(f, "    {err}")?;
122                }
123                Ok(())
124            }
125            Correctness(correctness) => writeln!(f, "{correctness}"),
126            OsError => writeln!(f, "An unknown error occurred"),
127        }
128    }
129}