Skip to main content

wonfy_tools/
error.rs

1#[derive(Debug)]
2pub struct UnknownError {
3    pub name: String,
4    pub value: String,
5    pub expected: Vec<(Vec<String>, String)>,
6}
7
8impl std::fmt::Display for UnknownError {
9    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
10        write!(
11            f,
12            "Unknown {} variant {}. \nexpected: ",
13            self.name, self.value
14        )?;
15
16        for (items, name) in self.expected.iter() {
17            write!(f, "\n- ")?;
18
19            for (index, item) in items.iter().enumerate() {
20                write!(
21                    f,
22                    "{}{}",
23                    item,
24                    if index == items.len() - 1 { "" } else { " | " }
25                )?;
26            }
27
28            write!(f, " => {}.", name)?;
29        }
30
31        Ok(())
32    }
33}
34
35impl std::error::Error for UnknownError {}
36
37#[derive(Debug)]
38pub struct MissingFieldError(pub String);
39
40impl std::fmt::Display for MissingFieldError {
41    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
42        write!(f, "Did not set the field \"{}\" when building", self.0)
43    }
44}
45
46macro_rules! unknown_error_expected {
47    ($($head:literal $(| $tail:literal)* => $type:literal),*) => {
48        vec![
49            $(
50                (
51                    vec![$head.to_string() $(, $tail.to_string())*],
52                    $type.to_string()
53                )
54            ),*
55        ]
56    };
57}
58
59pub(crate) use unknown_error_expected;