1#[derive(Debug, Clone)]
7pub struct InconsistentEdn(pub(super) &'static str);
8
9impl From<core::num::TryFromIntError> for InconsistentEdn {
10 fn from(_: core::num::TryFromIntError) -> Self {
11 InconsistentEdn("Numeric range exceeded")
12 }
13}
14
15impl core::fmt::Display for InconsistentEdn {
16 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> Result<(), core::fmt::Error> {
17 f.write_str(self.0)
18 }
19}
20
21impl std::error::Error for InconsistentEdn {}
22
23#[derive(Debug, Clone)]
28pub struct ParseError(pub(super) peg::error::ParseError<peg::str::LineCol>);
29
30impl ParseError {
31 pub fn line(&self) -> usize {
33 self.0.location.line
34 }
35 pub fn column(&self) -> usize {
37 self.0.location.column
38 }
39 pub fn offset(&self) -> usize {
41 self.0.location.offset
42 }
43}
44
45impl core::fmt::Display for ParseError {
46 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> Result<(), core::fmt::Error> {
47 writeln!(
48 f,
49 "Invalid EDN in line {} column {} (byte {}). Expected any of:",
50 self.line(),
51 self.column(),
52 self.offset()
53 )?;
54 for t in self.0.expected.tokens() {
55 writeln!(f, "* {}", t)?;
56 }
57 Ok(())
58 }
59}
60
61impl std::error::Error for ParseError {}
62
63#[derive(Debug, Clone)]
65pub struct TypeMismatch {
66 expected: &'static str,
67}
68
69impl TypeMismatch {
70 pub(crate) fn expecting(expected: &'static str) -> Self {
71 TypeMismatch { expected }
72 }
73}
74
75impl core::fmt::Display for TypeMismatch {
76 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> Result<(), core::fmt::Error> {
77 write!(f, "Expected {}", self.expected)
78 }
79}
80
81impl std::error::Error for TypeMismatch {}
82
83#[derive(Debug, Clone)]
85pub struct CborError(CborErrorInner);
86
87impl CborError {
88 pub(crate) fn out_of_data(text: &'static str) -> Self {
89 Self(CborErrorInner::OuOfData(text))
90 }
91
92 pub(crate) fn invalid(text: &'static str) -> Self {
93 Self(CborErrorInner::InvalidData(text))
94 }
95
96 pub(crate) fn is_out_of_data(&self) -> bool {
97 match self.0 {
98 CborErrorInner::OuOfData(_) => true,
99 CborErrorInner::InvalidData(_) => false,
100 }
101 }
102}
103
104#[derive(Debug, Clone)]
105enum CborErrorInner {
106 OuOfData(&'static str),
107 InvalidData(&'static str),
108}
109
110impl core::fmt::Display for CborError {
111 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> Result<(), core::fmt::Error> {
112 match self.0 {
113 CborErrorInner::OuOfData(s) => write!(f, "Out of data: {s}"),
114 CborErrorInner::InvalidData(s) => write!(f, "Invalid data: {s}"),
115 }
116 }
117}
118
119impl std::error::Error for CborError {}