1use core::fmt;
9
10#[derive(Debug, Clone, Copy, PartialEq, Eq)]
12#[non_exhaustive]
13pub enum ParseError {
14 UnexpectedEof {
16 needed: usize,
18 available: usize,
20 },
21 ExpectedList {
23 found: u8,
25 },
26 EmptyNode,
28 UnexpectedTag {
30 tag: u8,
32 },
33 UnknownToken {
36 dictionary: Option<u8>,
38 index: u16,
40 },
41 NonStringTag,
43 NonStringAttr,
45 InvalidPackedLength {
47 byte: u8,
49 },
50 TrailingBytes(usize),
53 DepthLimitExceeded {
55 limit: usize,
57 },
58}
59
60impl fmt::Display for ParseError {
61 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
62 match self {
63 Self::UnexpectedEof { needed, available } => write!(
64 f,
65 "unexpected end of frame: needed {needed} byte(s), {available} available"
66 ),
67 Self::ExpectedList { found } => {
68 write!(f, "expected a list tag, found {found:#04x}")
69 }
70 Self::EmptyNode => f.write_str("node list is empty; every node has at least a tag"),
71 Self::UnexpectedTag { tag } => write!(f, "unexpected tag {tag:#04x}"),
72 Self::UnknownToken { dictionary, index } => match dictionary {
73 Some(dict) => write!(f, "no token {index} in dictionary {dict}"),
74 None => write!(f, "no single-byte token {index}"),
75 },
76 Self::NonStringTag => f.write_str("node tag is not a string"),
77 Self::NonStringAttr => f.write_str("attribute key or value is not a string"),
78 Self::InvalidPackedLength { byte } => {
79 write!(f, "invalid packed length byte {byte:#04x}")
80 }
81 Self::TrailingBytes(count) => {
82 write!(f, "{count} trailing byte(s) after the root node")
83 }
84 Self::DepthLimitExceeded { limit } => {
85 write!(f, "nesting deeper than the limit of {limit}")
86 }
87 }
88 }
89}
90
91impl core::error::Error for ParseError {}
92
93#[cfg(test)]
94mod tests {
95 use super::*;
96 extern crate alloc;
97 use alloc::string::ToString;
98
99 #[test]
100 fn every_variant_renders_its_detail() {
101 let cases: [(ParseError, &[&str]); 10] = [
102 (
103 ParseError::UnexpectedEof {
104 needed: 4,
105 available: 1,
106 },
107 &["4", "1"],
108 ),
109 (ParseError::ExpectedList { found: 0x07 }, &["0x07"]),
110 (ParseError::EmptyNode, &["empty"]),
111 (ParseError::UnexpectedTag { tag: 0xAB }, &["0xab"]),
112 (
113 ParseError::UnknownToken {
114 dictionary: Some(2),
115 index: 300,
116 },
117 &["2", "300"],
118 ),
119 (
120 ParseError::UnknownToken {
121 dictionary: None,
122 index: 240,
123 },
124 &["240"],
125 ),
126 (ParseError::NonStringTag, &["tag"]),
127 (ParseError::NonStringAttr, &["attribute"]),
128 (ParseError::InvalidPackedLength { byte: 0x80 }, &["0x80"]),
129 (ParseError::TrailingBytes(9), &["9"]),
130 ];
131 for (error, fragments) in cases {
132 let text = error.to_string();
133 assert!(!text.is_empty());
134 for fragment in fragments {
135 assert!(text.contains(fragment), "{text:?} lacks {fragment:?}");
136 }
137 }
138 assert!(
139 ParseError::DepthLimitExceeded { limit: 64 }
140 .to_string()
141 .contains("64")
142 );
143 }
144
145 #[test]
146 fn errors_are_std_errors_and_comparable() {
147 fn assert_error<E: core::error::Error>(_: &E) {}
148 assert_error(&ParseError::EmptyNode);
149 assert_eq!(ParseError::EmptyNode, ParseError::EmptyNode);
150 assert_ne!(ParseError::EmptyNode, ParseError::NonStringTag);
151 assert!(!alloc::format!("{:?}", ParseError::EmptyNode).is_empty());
152 }
153}