1use alloc::string::String;
4use core::fmt;
5
6#[derive(Debug)]
8#[non_exhaustive]
9pub enum Error {
10 Empty,
12 TrailingInput(String),
14 Parse(String),
16}
17
18impl fmt::Display for Error {
19 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
20 match self {
21 Error::Empty => write!(f, "Input is empty"),
22 Error::TrailingInput(input) => write!(f, "Unexpected remaining input: {input:?}"),
23 Error::Parse(e) => write!(f, "Parse error: {e}"),
24 }
25 }
26}
27
28impl core::error::Error for Error {}
29
30#[cfg(test)]
31mod tests {
32 use super::*;
33 use alloc::string::ToString;
34
35 #[test]
36 fn display() {
37 let cases = [
38 (Error::Empty, "Input is empty"),
39 (
40 Error::TrailingInput("extra junk".to_string()),
41 "Unexpected remaining input: \"extra junk\"",
42 ),
43 (
44 Error::Parse("bad token".to_string()),
45 "Parse error: bad token",
46 ),
47 ];
48
49 for (error, expected) in cases {
50 assert_eq!(error.to_string(), expected);
51 }
52 }
53}