Skip to main content

zlink_idl/parse/
error.rs

1//! Errors returned when parsing Varlink IDL.
2
3use alloc::string::String;
4use core::fmt;
5
6/// An error that occurred while parsing Varlink IDL.
7#[derive(Debug)]
8#[non_exhaustive]
9pub enum Error {
10    /// The input was empty.
11    Empty,
12    /// Input remained after the interface was parsed.
13    TrailingInput(String),
14    /// The input could not be parsed.
15    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}