assemblyline_markings/
errors.rs

1//! Error types and error handling functions
2
3/// An enumeration of all errors that can occur in the library
4#[derive(Debug)]
5pub enum Errors {
6    /// An invalid classification string was provided
7    InvalidClassification(String),
8    /// An invalid classification config was provided
9    InvalidDefinition(String),
10    /// A name given in a classification definition is an empty string
11    ClassificationNameEmpty
12}
13
14impl std::fmt::Display for Errors {
15    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
16        match self {
17            Errors::InvalidClassification(message) => f.write_fmt(format_args!("An invalid classification string was provided: {message}")),
18            Errors::InvalidDefinition(message) => f.write_fmt(format_args!("An invalid classification config was provided: {message}")),
19            Errors::ClassificationNameEmpty => f.write_str("A name given in the classification definition is an empty string"),
20        }
21    }
22}
23
24// make the Errors struct into an error type
25impl std::error::Error for Errors {}
26
27// Capture certain errors with a fairly direct conversion to InvalidDefinition
28// impl From<serde_json::Error> for Errors {
29//     fn from(value: serde_json::Error) -> Self {
30//         Self::InvalidDefinition(value.to_string())
31//     }
32// }
33impl From<serde_yaml::Error> for Errors {
34    fn from(value: serde_yaml::Error) -> Self {
35        Self::InvalidDefinition(value.to_string())
36    }
37}
38impl From<std::io::Error> for Errors {
39    fn from(value: std::io::Error) -> Self {
40        Self::InvalidDefinition(value.to_string())
41    }
42}