streamson_lib/
error.rs

1//! Module containing errors
2
3use std::{error::Error, fmt, io, str::Utf8Error};
4
5/// Matcher related errors
6#[derive(Debug, PartialEq, Clone)]
7pub enum Matcher {
8    Parse(String),
9}
10
11impl Error for Matcher {}
12
13impl fmt::Display for Matcher {
14    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
15        match self {
16            Self::Parse(input) => write!(f, "Failed to parse matcher'{}", input),
17        }
18    }
19}
20
21/// Handler related errors
22#[derive(Debug, PartialEq, Clone)]
23pub struct Handler {
24    reason: String,
25}
26
27impl Handler {
28    pub fn new<T>(reason: T) -> Self
29    where
30        T: ToString,
31    {
32        Self {
33            reason: reason.to_string(),
34        }
35    }
36}
37
38impl Error for Handler {}
39
40impl fmt::Display for Handler {
41    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
42        write!(f, "Handler failed - {}", self.reason)
43    }
44}
45
46/// Incorrect input error
47#[derive(Debug, PartialEq, Clone)]
48pub struct IncorrectInput {
49    byte: u8,
50    idx: usize,
51}
52
53impl IncorrectInput {
54    pub fn new(byte: u8, idx: usize) -> Self {
55        Self { byte, idx }
56    }
57}
58
59impl Error for IncorrectInput {}
60
61impl fmt::Display for IncorrectInput {
62    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
63        write!(
64            f,
65            "Incorrect input (byte '{}' on idx {})",
66            self.byte, self.idx
67        )
68    }
69}
70
71/// Input data stream ended but data were still expected
72#[derive(Debug, PartialEq, Clone)]
73pub struct InputTerminated {
74    idx: usize,
75}
76
77impl InputTerminated {
78    pub fn new(idx: usize) -> Self {
79        Self { idx }
80    }
81}
82
83impl Error for InputTerminated {}
84
85impl fmt::Display for InputTerminated {
86    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
87        write!(f, "InputTerminated (idx '{}')", self.idx)
88    }
89}
90
91/// Path related error
92#[derive(Debug, PartialEq, Clone)]
93pub struct Path {
94    path: String,
95}
96
97impl Path {
98    pub fn new<T>(path: T) -> Self
99    where
100        T: ToString,
101    {
102        Self {
103            path: path.to_string(),
104        }
105    }
106}
107
108impl Error for Path {}
109
110impl fmt::Display for Path {
111    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
112        write!(f, "Wrong path '{}'", self.path)
113    }
114}
115
116/// General error
117#[derive(Debug)]
118pub enum General {
119    Path(Path),
120    Handler(Handler),
121    Matcher(Matcher),
122    Utf8Error(Utf8Error),
123    IncorrectInput(IncorrectInput),
124    InputTerminated(InputTerminated),
125    IoError(io::Error),
126}
127
128impl Error for General {}
129impl fmt::Display for General {
130    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
131        match self {
132            Self::Path(err) => err.fmt(f),
133            Self::Handler(err) => err.fmt(f),
134            Self::Matcher(err) => err.fmt(f),
135            Self::Utf8Error(err) => err.fmt(f),
136            Self::IncorrectInput(err) => err.fmt(f),
137            Self::InputTerminated(err) => err.fmt(f),
138            Self::IoError(err) => err.fmt(f),
139        }
140    }
141}
142
143macro_rules! impl_into_general {
144    ($tp:path, $inner: path) => {
145        impl From<$tp> for General {
146            fn from(entity: $tp) -> Self {
147                $inner(entity)
148            }
149        }
150    };
151}
152
153impl_into_general!(Path, Self::Path);
154impl_into_general!(Handler, Self::Handler);
155impl_into_general!(Matcher, Self::Matcher);
156impl_into_general!(Utf8Error, Self::Utf8Error);
157impl_into_general!(IncorrectInput, Self::IncorrectInput);
158impl_into_general!(InputTerminated, Self::InputTerminated);
159impl_into_general!(io::Error, Self::IoError);