-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.rs
More file actions
63 lines (57 loc) · 1.86 KB
/
error.rs
File metadata and controls
63 lines (57 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
use core::fmt::{Debug, Display};
use core::num::TryFromIntError;
/// Structure, representing an error.
#[derive(Debug)]
pub struct NetMonError {
/// The context of the error.
pub context: &'static str,
/// The error's kind.
pub kind: Kind,
}
/// Enumerates different types of errors that can occur.
#[derive(Debug)]
pub enum Kind {
/// Error related to type conversion failures.
Conversion,
/// Error indicating an unsupported operation, value or feature.
Unsupported,
/// Error caused by an unknown value.
Unknown,
/// A general-purpose error category.
General,
}
impl NetMonError {
/// Creates a new `Error` instance.
///
/// # Arguments:
///
/// * `context` - A description of where the error occurred.
/// * `kind` - The specific kind of error.
#[must_use]
pub fn new(context: &'static str, kind: Kind) -> Self {
Self { context, kind }
}
}
// We need to implement a [Display] trait to be able to print the error.
impl Display for NetMonError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "[{}] {}", self.context, self.kind)
}
}
// A sample implementation of the conversion of the error of a different type.
impl From<TryFromIntError> for NetMonError {
fn from(_: TryFromIntError) -> Self {
NetMonError::new("integer conversion failed", Kind::Conversion)
}
}
// We need to implement a [Display] trait to be able to print the Kind of the error.
impl Display for Kind {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match &self {
Kind::Conversion => write!(f, "conversion error"),
Kind::Unsupported => write!(f, "unsupported error"),
Kind::Unknown => write!(f, "unknown value error"),
Kind::General => write!(f, "general error"),
}
}
}