arcs_ctf_yaml_parser/lists/
structs.rs

1use std::fmt::{Display, Debug};
2
3use crate::structs::ValueType;
4use super::StrList;
5
6
7#[derive(Default, Clone, PartialEq)]
8pub struct Authors(Vec<String>);
9
10#[derive(Default, Debug, Clone)]
11pub enum AuthorError { BadEntryType(Vec<ValueType>), BadType(ValueType), #[default] MissingKey }
12impl StrList for Authors {
13    type Error = AuthorError; 
14    fn from_iter<'a>(iter: impl Iterator<Item = &'a str>) -> Result<Self, Self::Error> {
15        Ok(Authors(iter.map(str::to_string).collect()))
16    }
17
18    fn from_value_mismatch(iter: impl Iterator<Item = ValueType>) -> Self::Error {
19        AuthorError::BadEntryType(iter.collect())
20    }
21
22    fn not_sequence(type_enum: ValueType) -> Self::Error {
23        AuthorError::BadType(type_enum)
24    }
25}
26impl Authors {
27    pub fn iter(&self) -> impl Iterator<Item = &str> {
28        self.0.iter().map(String::as_str)
29    }
30    pub fn slice(&self) -> &[String] {
31        &self.0
32    }
33}
34
35impl Display for AuthorError {
36    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
37        use AuthorError::*;
38        match self {
39            &BadEntryType(_) => writeln!(f, "Author names must be strings."),
40            &BadType(t) => writeln!(f, "Authors should be a list, not {t}."),
41            MissingKey => writeln!(f, "You have to define `authors`."),
42        }
43    }
44}
45impl Debug for Authors {
46    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
47        write!(f, "Authors< ")?;
48        if let Some(name) = self.0.first() {
49            write!(f, "{name}")?;
50        }
51        for name in self.0.iter().skip(1) {
52            write!(f, ", {name}")?;
53        }
54        write!(f, " >")
55    }
56}
57
58
59
60
61#[derive(Default, Clone, PartialEq)]
62pub struct Hints(Vec<String>);
63#[derive(Default, Debug, Clone)]
64pub enum HintError { BadEntryType(Vec<ValueType>), BadType(ValueType), #[default] MissingKey }
65impl StrList for Hints {
66    type Error = HintError; 
67    fn from_iter<'a>(iter: impl Iterator<Item = &'a str>) -> Result<Self, Self::Error> {
68        Ok(Hints(iter.map(str::to_string).collect()))
69    }
70
71    fn from_value_mismatch(iter: impl Iterator<Item = ValueType>) -> Self::Error {
72        HintError::BadEntryType(iter.collect())
73    }
74
75    fn not_sequence(type_enum: ValueType) -> Self::Error {
76        HintError::BadType(type_enum)
77    }
78}
79impl Hints {
80    pub fn iter(&self) -> impl Iterator<Item = &str> {
81        self.0.iter().map(String::as_str)
82    }
83    pub fn slice(&self) -> &[String] {
84        &self.0
85    }
86}
87
88impl Display for HintError {
89    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
90        use HintError::*;
91        match self {
92            &BadEntryType(_) => writeln!(f, "Hints must be strings."),
93            &BadType(t) => writeln!(f, "Hints should be in a list, not {t}."),
94            MissingKey => writeln!(f, "You have to define `hints`."),
95        }
96    }
97}
98impl Debug for Hints {
99    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
100        write!(f, "Hints ")?;
101        f.debug_list()
102            .entries(self.0.iter())
103            .finish()
104    }
105}
106
107