nettext/src/dec/error.rs
2022-11-18 14:15:30 +01:00

35 lines
1.2 KiB
Rust

use std::fmt;
/// The type of errors returned by helper functions on `Term`
#[derive(Debug, Clone)]
pub enum TypeError {
/// The term could not be decoded in the given type
WrongType(&'static str),
/// The term is not an array of the requested length
WrongLength(usize, usize),
/// The dictionnary is missing a key
MissingKey(String),
/// The dictionnary contains an invalid key
UnexpectedKey(String),
/// The underlying raw string contains garbage (should not happen in theory)
Garbage,
}
impl From<std::str::Utf8Error> for TypeError {
fn from(_x: std::str::Utf8Error) -> TypeError {
TypeError::Garbage
}
}
impl std::fmt::Display for TypeError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
TypeError::WrongType(t) => write!(f, "Not a {}", t),
TypeError::WrongLength(n, m) => write!(f, "Expected {} items, got {}", m, n),
TypeError::MissingKey(k) => write!(f, "Missing key `{}` in dict", k),
TypeError::UnexpectedKey(k) => write!(f, "Spurrious/unexpected key `{}` in dict", k),
TypeError::Garbage => write!(f, "Garbage in underlying data"),
}
}
}