1use std::fmt::{Display, Formatter};
2
3pub type Result<T> = std::result::Result<T, Error>;
5
6#[derive(Debug, Hash, PartialEq, Eq)]
8pub enum Error {
9 InvalidData,
12
13 InvalidPrefix,
15
16 InvalidFormat,
18
19 InvalidChar { found: char },
22
23 InvalidLength { expected: usize, found: usize },
26}
27
28impl Error {
29 pub fn message(&self) -> String {
31 match self {
32 Self::InvalidData => "input contains invalid data".to_owned(),
33 Self::InvalidPrefix => "prefix is not valid".to_owned(),
34 Self::InvalidFormat => "format is not correct".to_owned(),
35 Self::InvalidChar { found } => format!("invalid character: {}", found),
36 Self::InvalidLength { expected, found } => format!(
37 "input is the wrong length: expected {} but found {}",
38 expected, found
39 ),
40 }
41 }
42}
43
44impl Display for Error {
45 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
46 write!(f, "{}", self.message())
47 }
48}
49
50impl std::error::Error for Error {}