use alloc::boxed::Box;
use alloc::string::String;
use crate::{ESExprTag, ESExprTagSet};
#[derive(Debug, Clone)]
pub struct DecodeError(Box<(DecodeErrorType, DecodeErrorPath)>);
impl DecodeError {
#[must_use]
pub fn new(error_type: DecodeErrorType, path: DecodeErrorPath) -> Self {
DecodeError(Box::new((error_type, path)))
}
#[must_use]
pub fn error_type(&self) -> &DecodeErrorType {
&self.0.0
}
#[must_use]
pub fn error_path(&self) -> &DecodeErrorPath {
&self.0.1
}
pub fn error_path_with(&mut self, f: impl FnOnce(DecodeErrorPath) -> DecodeErrorPath) {
let mut old_path = DecodeErrorPath::Current;
core::mem::swap(&mut old_path, &mut self.0.1);
self.0.1 = f(old_path);
}
}
#[derive(Debug, Clone)]
pub enum DecodeErrorType {
UnexpectedExpr {
expected_tags: ESExprTagSet,
actual_tag: ESExprTag<'static>,
},
OutOfRange(String),
MissingKeyword(String),
MissingPositional,
}
#[derive(Debug, Clone)]
pub enum DecodeErrorPath {
Current,
Constructor(String),
Positional(String, usize, Box<DecodeErrorPath>),
Keyword(String, String, Box<DecodeErrorPath>),
}