esexpr/
error.rs

1use alloc::boxed::Box;
2use alloc::string::String;
3
4use crate::{ESExprTag, ESExprTagSet};
5
6/// An error that occurs when decoding expressions.
7#[derive(Debug, Clone)]
8pub struct DecodeError(Box<(DecodeErrorType, DecodeErrorPath)>);
9
10impl DecodeError {
11	/// Create a `DecodeError`.
12	#[must_use]
13	pub fn new(error_type: DecodeErrorType, path: DecodeErrorPath) -> Self {
14		DecodeError(Box::new((error_type, path)))
15	}
16
17	/// Gets the error type.
18	#[must_use]
19	pub fn error_type(&self) -> &DecodeErrorType {
20		&self.0.0
21	}
22
23	/// Gets the error path.
24	#[must_use]
25	pub fn error_path(&self) -> &DecodeErrorPath {
26		&self.0.1
27	}
28
29	/// Updates the error path, based on the original.
30	pub fn error_path_with(&mut self, f: impl FnOnce(DecodeErrorPath) -> DecodeErrorPath) {
31		let mut old_path = DecodeErrorPath::Current;
32		core::mem::swap(&mut old_path, &mut self.0.1);
33		self.0.1 = f(old_path);
34	}
35}
36
37/// The type of error that occurred while decoding.
38#[derive(Debug, Clone)]
39pub enum DecodeErrorType {
40	/// An expression had a different tag than expected.
41	UnexpectedExpr {
42		/// The tags that were expected.
43		expected_tags: ESExprTagSet,
44
45		/// The actual tag of the expression.
46		actual_tag: ESExprTag<'static>,
47	},
48
49	/// Indicates that a value was not valid for the decoded type.
50	OutOfRange(String),
51
52	/// Indicates that a keyword argument was missing.
53	MissingKeyword(String),
54
55	/// Indicates that a positional argument was missing.
56	MissingPositional,
57}
58
59/// Specifies where in an expression an error occurred.
60#[derive(Debug, Clone)]
61pub enum DecodeErrorPath {
62	/// Error occurred at the current position in the object.
63	Current,
64
65	/// Error occurred at the current position in the object, within a constructor with the specified name.
66	Constructor(String),
67
68	/// Error occurred under a positional argument.
69	Positional(String, usize, Box<DecodeErrorPath>),
70
71	/// Error occurred under a keyword argument.
72	Keyword(String, String, Box<DecodeErrorPath>),
73}