#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("input truncated at offset {offset}: {needed} more bytes needed")]
Truncated {
offset: usize,
needed: usize,
},
#[error("expected {expected}, found {found} at offset {offset}")]
Type {
expected: &'static str,
found: &'static str,
offset: usize,
},
#[error("integer {value} out of range for {target}")]
IntRange {
value: i128,
target: &'static str,
},
#[error("invalid UTF-8 string at offset {offset}")]
Utf8 {
offset: usize,
},
#[error("wrong message tag: expected `{expected}`, found `{found}`")]
Tag {
expected: &'static str,
found: String,
},
#[error("missing required field `{0}`")]
MissingField(&'static str),
#[error("{0} trailing bytes after the message")]
TrailingBytes(usize),
#[error("{0}")]
Invalid(String),
#[error("field `{field}`: {source}")]
Field {
field: &'static str,
#[source]
source: Box<Error>,
},
}
impl Error {
pub fn in_field(self, field: &'static str) -> Self {
Error::Field {
field,
source: Box::new(self),
}
}
}
pub type Result<T> = std::result::Result<T, Error>;