aneubeck_daachorse/
errors.rs1use core::result;
4
5use alloc::fmt;
6use alloc::string::String;
7
8#[derive(Debug)]
10pub enum DaachorseError {
11 InvalidArgument(InvalidArgumentError),
13
14 DuplicatePattern(DuplicatePatternError),
16
17 AutomatonScale(AutomatonScaleError),
19
20 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#[derive(Debug)]
55pub struct InvalidArgumentError {
56 arg: &'static str,
58
59 op: &'static str,
61
62 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#[derive(Debug)]
78pub struct DuplicatePatternError {
79 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#[derive(Debug)]
91pub struct AutomatonScaleError {
92 arg: &'static str,
94
95 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#[derive(Debug)]
111pub struct InvalidConversionError {
112 arg: &'static str,
114
115 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
129pub type Result<T, E = DaachorseError> = result::Result<T, E>;