aneubeck_daachorse/
errors.rs

1//! Definition of errors.
2
3use core::result;
4
5use alloc::fmt;
6use alloc::string::String;
7
8/// Errors in daachorse.
9#[derive(Debug)]
10pub enum DaachorseError {
11    /// Contains [`InvalidArgumentError`].
12    InvalidArgument(InvalidArgumentError),
13
14    /// Contains [`DuplicatePatternError`].
15    DuplicatePattern(DuplicatePatternError),
16
17    /// Contains [`AutomatonScaleError`].
18    AutomatonScale(AutomatonScaleError),
19
20    /// Contains [`InvalidConversionError`].
21    InvalidConversion(InvalidConversionError),
22}
23
24impl fmt::Display for DaachorseError {
25    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
26        match self {
27            Self::InvalidArgument(e) => e.fmt(f),
28            Self::DuplicatePattern(e) => e.fmt(f),
29            Self::AutomatonScale(e) => e.fmt(f),
30            Self::InvalidConversion(e) => e.fmt(f),
31        }
32    }
33}
34
35impl DaachorseError {
36    pub(crate) const fn invalid_argument(arg: &'static str, op: &'static str, value: u32) -> Self {
37        Self::InvalidArgument(InvalidArgumentError { arg, op, value })
38    }
39
40    pub(crate) const fn duplicate_pattern(pattern: String) -> Self {
41        Self::DuplicatePattern(DuplicatePatternError { pattern })
42    }
43
44    pub(crate) const fn automaton_scale(arg: &'static str, max_value: u32) -> Self {
45        Self::AutomatonScale(AutomatonScaleError { arg, max_value })
46    }
47
48    pub(crate) const fn invalid_conversion(arg: &'static str, target: &'static str) -> Self {
49        Self::InvalidConversion(InvalidConversionError { arg, target })
50    }
51}
52
53/// Error used when the argument is invalid.
54#[derive(Debug)]
55pub struct InvalidArgumentError {
56    /// Name of the argument.
57    arg: &'static str,
58
59    /// Condition operator.
60    op: &'static str,
61
62    /// Condition value.
63    value: u32,
64}
65
66impl fmt::Display for InvalidArgumentError {
67    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
68        write!(
69            f,
70            "InvalidArgumentError: {} must be {} {}",
71            self.arg, self.op, self.value
72        )
73    }
74}
75
76/// Error used when some patterns are duplicated.
77#[derive(Debug)]
78pub struct DuplicatePatternError {
79    /// A duplicate pattern.
80    pattern: String,
81}
82
83impl fmt::Display for DuplicatePatternError {
84    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
85        write!(f, "DuplicatePatternError: {}", self.pattern)
86    }
87}
88
89/// Error used when the scale of the automaton exceeds the expected one.
90#[derive(Debug)]
91pub struct AutomatonScaleError {
92    /// Name of the argument.
93    arg: &'static str,
94
95    /// The maximum value (inclusive).
96    max_value: u32,
97}
98
99impl fmt::Display for AutomatonScaleError {
100    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
101        write!(
102            f,
103            "AutomatonScaleError: {} must be <= {}",
104            self.arg, self.max_value
105        )
106    }
107}
108
109/// Error used when the conversion fails.
110#[derive(Debug)]
111pub struct InvalidConversionError {
112    /// Name of the argument.
113    arg: &'static str,
114
115    /// Target type.
116    target: &'static str,
117}
118
119impl fmt::Display for InvalidConversionError {
120    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
121        write!(
122            f,
123            "InvalidConversionError: {} cannot be converted to {}",
124            self.arg, self.target
125        )
126    }
127}
128
129/// A specialized Result type for Daachorse.
130pub type Result<T, E = DaachorseError> = result::Result<T, E>;