codes_common/
error.rs

1/*!
2One-line description.
3
4More detailed description, with
5
6# Example
7
8YYYYY
9
10*/
11
12use std::fmt;
13
14// ------------------------------------------------------------------------------------------------
15// Public Types
16// ------------------------------------------------------------------------------------------------
17
18///
19/// Common `Error` type, mainly used for `FromStr` failures.
20///
21#[derive(Debug)]
22pub enum CodeParseError {
23    /// The string to parse was either too short or too long.
24    InvalidLength { type_name: String, length: usize },
25    /// The value is incorrectly formatted
26    InvalidFormat { type_name: String, value: String },
27    /// The value contains an invalid character
28    InvalidCharacter { type_name: String, c: char },
29    /// The string value did not represent a known value.
30    UnknownValue { type_name: String, value: String },
31    /// An error in check digit calculation/verification.
32    CheckDigit(Box<dyn std::error::Error>),
33}
34
35// ------------------------------------------------------------------------------------------------
36// Public Functions
37// ------------------------------------------------------------------------------------------------
38
39pub fn invalid_length<S>(type_name: S, length: usize) -> CodeParseError
40where
41    S: Into<String>,
42{
43    CodeParseError::InvalidLength {
44        type_name: type_name.into(),
45        length,
46    }
47}
48
49pub fn invalid_format<S1, S2>(type_name: S1, value: S2) -> CodeParseError
50where
51    S1: Into<String>,
52    S2: Into<String>,
53{
54    CodeParseError::InvalidFormat {
55        type_name: type_name.into(),
56        value: value.into(),
57    }
58}
59
60pub fn invalid_character<S, C>(type_name: S, c: C) -> CodeParseError
61where
62    S: Into<String>,
63    C: Into<char>,
64{
65    CodeParseError::InvalidCharacter {
66        type_name: type_name.into(),
67        c: c.into(),
68    }
69}
70
71pub fn unknown_value<S1, S2>(type_name: S1, value: S2) -> CodeParseError
72where
73    S1: Into<String>,
74    S2: Into<String>,
75{
76    CodeParseError::UnknownValue {
77        type_name: type_name.into(),
78        value: value.into(),
79    }
80}
81// ------------------------------------------------------------------------------------------------
82// Implementations
83// ------------------------------------------------------------------------------------------------
84
85impl fmt::Display for CodeParseError {
86    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
87        write!(
88            f,
89            "{}",
90            match self {
91                Self::InvalidLength { type_name, length } => format!(
92                    "The string passed is an invalid length for the type `{}`; length: {}",
93                    type_name, length
94                ),
95                Self::InvalidFormat { type_name, value } => format!(
96                    "The string passed is incorrectly formatted for type `{}`; value: {:?}",
97                    type_name, value
98                ),
99                Self::InvalidCharacter { type_name, c } => format!(
100                    "The string passed contains characters not legal for type `{}`; character: {:?}",
101                    type_name, c
102                ),
103                Self::UnknownValue { type_name, value } => format!(
104                    "The string passed is an invalid length for the not a known value of type `{}`; value: {:?}",
105                    type_name, value
106                ),
107                Self::CheckDigit(e) => e.to_string(),
108            }
109        )
110    }
111}
112
113impl std::error::Error for CodeParseError {}