1use core::error;
2use core::fmt::{self, Debug};
3
4pub type Result<T> = core::result::Result<T, Error>;
5
6pub struct Error {
7 pub code: Code,
9 pub line: Option<usize>,
11 pub column: Option<usize>,
13 pub ptr: Option<usize>,
15}
16
17#[derive(Debug, Eq, PartialEq)]
18#[non_exhaustive]
19pub enum Code {
20 HashMapDuplicateKey,
22 SetDuplicateKey,
23
24 InvalidChar,
26 InvalidEscape,
27 InvalidKeyword,
28 InvalidNumber,
29 InvalidRadix(Option<u8>),
30 InvalidTag,
31 UnexpectedEOF,
32 UnmatchedDelimiter(char),
33
34 NoFloatFeature,
36
37 #[cfg(feature = "serde")]
39 Serde(alloc::string::String),
40}
41
42impl Error {
43 pub(crate) const fn from_position(code: Code, position: crate::parse::Position) -> Self {
44 Self { code, line: Some(position.line), column: Some(position.column), ptr: Some(position.ptr) }
45 }
46}
47
48impl error::Error for Error {}
49
50impl Debug for Error {
51 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
52 write!(
53 f,
54 "EdnError {{ code: {:?}, line: {:?}, column: {:?}, ptr: {:?} }}",
55 self.code, self.line, self.column, self.ptr
56 )
57 }
58}
59
60impl alloc::fmt::Display for Error {
61 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
62 write!(f, "{self:?}")
63 }
64}