mod context;
use std::borrow::Cow;
use std::fmt::Display;
pub use context::{Context, Location};
#[derive(Debug, thiserror::Error)]
pub struct Error {
context: Context,
kind: ErrorKind,
}
impl Error {
pub fn new(kind: ErrorKind) -> Error {
Error { context: Context::new(), kind }
}
pub fn custom(error: impl Into<CustomError>) -> Error {
Error::new(ErrorKind::Custom(error.into()))
}
pub fn kind(&self) -> &ErrorKind {
&self.kind
}
pub fn context(&self) -> &Context {
&self.context
}
pub fn at(mut self, loc: Location) -> Self {
self.context.push(loc);
Error { context: self.context, kind: self.kind }
}
pub fn at_idx(mut self, idx: usize) -> Self {
self.context.push(Location::idx(idx));
Error { context: self.context, kind: self.kind }
}
pub fn at_field(mut self, field: impl Into<Cow<'static, str>>) -> Self {
self.context.push(Location::field(field));
Error { context: self.context, kind: self.kind }
}
pub fn at_variant(mut self, variant: impl Into<Cow<'static, str>>) -> Self {
self.context.push(Location::variant(variant));
Error { context: self.context, kind: self.kind }
}
}
impl Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let path = self.context.path();
let kind = &self.kind;
write!(f, "Error at {path}: {kind}")
}
}
impl From<crate::visitor::DecodeError> for Error {
fn from(err: crate::visitor::DecodeError) -> Error {
Error::new(err.into())
}
}
#[derive(Debug, thiserror::Error)]
pub enum ErrorKind {
#[error("Error decoding bytes given the type ID and registry provided: {0}")]
VisitorDecodeError(#[from] crate::visitor::DecodeError),
#[error("Number {value} is out of range")]
NumberOutOfRange {
value: String,
},
#[error("Cannot find variant {got}; expects one of {expected:?}")]
CannotFindVariant {
got: String,
expected: Vec<&'static str>,
},
#[error("Cannot decode from type; expected length {expected_len} but got length {actual_len}")]
WrongLength {
actual_len: usize,
expected_len: usize,
},
#[error("Field {name} does not exist in our encoded data")]
CannotFindField {
name: String,
},
#[error("Custom error: {0}")]
Custom(CustomError),
}
type CustomError = Box<dyn std::error::Error + Send + Sync + 'static>;
#[cfg(test)]
mod test {
use super::*;
#[derive(thiserror::Error, Debug)]
enum MyError {
#[error("Foo!")]
Foo,
}
#[test]
fn custom_error() {
Error::custom(MyError::Foo);
}
}