Skip to main content

clojure_reader/
error.rs

1use core::error;
2use core::fmt::{self, Debug};
3
4pub type Result<T> = core::result::Result<T, Error>;
5
6pub struct Error {
7  /// Error code. This is `non_exhaustive`.
8  pub code: Code,
9  /// Line number, counting from 1.
10  pub line: Option<usize>,
11  /// Column number, counting from 1. The count is utf-8 chars.
12  pub column: Option<usize>,
13  /// This is a pointer offset of the str trying to be parsed, not a utf-8 char offset
14  pub ptr: Option<usize>,
15}
16
17#[derive(Debug, Eq, PartialEq)]
18#[non_exhaustive]
19pub enum Code {
20  /// Elaboration errors
21  HashMapDuplicateKey,
22  SetDuplicateKey,
23
24  /// Parse errors
25  InvalidChar,
26  InvalidEscape,
27  InvalidKeyword,
28  InvalidNumber,
29  InvalidRadix(Option<u8>),
30  InvalidTag,
31  UnexpectedEOF,
32  UnmatchedDelimiter(char),
33
34  /// Feature errors
35  NoFloatFeature,
36
37  /// Serde
38  #[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}