1use std::fmt;
13
14#[derive(Debug)]
22pub enum CodeParseError {
23 InvalidLength { type_name: String, length: usize },
25 InvalidFormat { type_name: String, value: String },
27 InvalidCharacter { type_name: String, c: char },
29 UnknownValue { type_name: String, value: String },
31 CheckDigit(Box<dyn std::error::Error>),
33}
34
35pub 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}
81impl 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 {}