2022-11-23 12:22:24 +00:00
|
|
|
use err_derive::Error;
|
2022-11-18 13:15:30 +00:00
|
|
|
|
2022-11-18 15:26:44 +00:00
|
|
|
use crate::dec::debug;
|
|
|
|
|
2022-11-18 13:15:30 +00:00
|
|
|
/// The type of errors returned by helper functions on `Term`
|
2022-11-23 12:22:24 +00:00
|
|
|
#[derive(Debug, Clone, Error)]
|
2022-11-18 13:15:30 +00:00
|
|
|
pub enum TypeError {
|
|
|
|
/// The term could not be decoded in the given type
|
2022-11-23 12:22:24 +00:00
|
|
|
#[error(display = "Not a {}", _0)]
|
2022-11-18 13:15:30 +00:00
|
|
|
WrongType(&'static str),
|
2022-11-23 12:22:24 +00:00
|
|
|
|
2022-11-18 13:15:30 +00:00
|
|
|
/// The term is not an array of the requested length
|
2022-11-23 12:22:24 +00:00
|
|
|
#[error(display = "Expected {} items, got {}", _0, _1)]
|
2022-11-18 13:15:30 +00:00
|
|
|
WrongLength(usize, usize),
|
2022-11-23 12:22:24 +00:00
|
|
|
|
2022-11-18 13:15:30 +00:00
|
|
|
/// The dictionnary is missing a key
|
2022-11-23 12:22:24 +00:00
|
|
|
#[error(display = "Missing key `{}` in dict", _0)]
|
2022-11-18 13:15:30 +00:00
|
|
|
MissingKey(String),
|
|
|
|
/// The dictionnary contains an invalid key
|
2022-11-23 12:22:24 +00:00
|
|
|
#[error(display = "Spurrious/unexpected key `{}` in dict", _0)]
|
2022-11-18 13:15:30 +00:00
|
|
|
UnexpectedKey(String),
|
|
|
|
/// The underlying raw string contains garbage (should not happen in theory)
|
2022-11-23 12:22:24 +00:00
|
|
|
#[error(display = "Garbage in underlying data")]
|
2022-11-18 13:15:30 +00:00
|
|
|
Garbage,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<std::str::Utf8Error> for TypeError {
|
|
|
|
fn from(_x: std::str::Utf8Error) -> TypeError {
|
|
|
|
TypeError::Garbage
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-18 15:26:44 +00:00
|
|
|
// ----
|
|
|
|
|
|
|
|
/// The error kind returned by the `decode` function.
|
|
|
|
#[derive(Eq, PartialEq)]
|
|
|
|
pub enum DecodeError<'a> {
|
|
|
|
/// Indicates that there is trailing garbage at the end of the decoded string
|
|
|
|
Garbage(&'a [u8]),
|
|
|
|
/// Indicates that the entered string does not represent a complete nettext term
|
|
|
|
IncompleteInput,
|
|
|
|
/// Indicates a syntax error in the decoded term
|
|
|
|
NomError(&'a [u8], nom::error::ErrorKind),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> std::fmt::Display for DecodeError<'a> {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
|
|
|
|
match self {
|
|
|
|
DecodeError::Garbage(g) => write!(f, "Garbage: `{}`", debug(g)),
|
|
|
|
DecodeError::IncompleteInput => write!(f, "Incomplete input"),
|
|
|
|
DecodeError::NomError(s, e) => write!(f, "Nom: {:?}, at: `{}`", e, debug(s)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> std::fmt::Debug for DecodeError<'a> {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
|
|
|
|
std::fmt::Display::fmt(self, f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-23 12:22:24 +00:00
|
|
|
impl<'a> std::error::Error for DecodeError<'a> {}
|
|
|
|
|
2022-11-18 15:26:44 +00:00
|
|
|
impl<'a> From<nom::Err<nom::error::Error<&'a [u8]>>> for DecodeError<'a> {
|
|
|
|
fn from(e: nom::Err<nom::error::Error<&'a [u8]>>) -> DecodeError<'a> {
|
|
|
|
match e {
|
|
|
|
nom::Err::Incomplete(_) => DecodeError::IncompleteInput,
|
|
|
|
nom::Err::Error(e) | nom::Err::Failure(e) => DecodeError::NomError(e.input, e.code),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|