Skip to main content

libconfig/
error.rs

1//! Error types for libconfig serialization and deserialization
2
3use std::fmt::{self, Display};
4
5use serde::{de, ser};
6
7/// A Result type alias where the error is `libconfig::Error`
8pub type Result<T> = std::result::Result<T, Error>;
9
10/// Errors that can occur when serializing or deserializing libconfig data
11#[derive(Debug)]
12pub enum Error {
13    /// A custom error message from Serde
14    Message(String),
15
16    /// Unexpected end of input
17    Eof,
18
19    /// Syntax error in the input
20    Syntax,
21
22    /// Expected a boolean value
23    ExpectedBoolean,
24
25    /// Expected an integer value
26    ExpectedInteger,
27
28    /// Expected a float value
29    ExpectedFloat,
30
31    /// Expected a string value
32    ExpectedString,
33
34    /// Expected an array
35    ExpectedArray,
36
37    /// Expected a comma in an array
38    ExpectedArrayComma,
39
40    /// Expected end of array
41    ExpectedArrayEnd,
42
43    /// Expected a semicolon after array
44    ExpectedArraySemicolon,
45
46    /// Expected a list
47    ExpectedList,
48
49    /// Expected a comma in a list
50    ExpectedListComma,
51
52    /// Expected end of list
53    ExpectedListEnd,
54
55    /// Expected a group (struct)
56    ExpectedGroup,
57
58    /// Expected an equals sign or colon
59    ExpectedEquals,
60
61    /// Expected a semicolon
62    ExpectedSemicolon,
63
64    /// Expected a comma or semicolon
65    ExpectedCommaOrSemicolon,
66
67    /// Expected end of group
68    ExpectedGroupEnd,
69
70    /// Expected an identifier
71    ExpectedIdentifier,
72
73    /// Trailing characters after parsing
74    TrailingCharacters,
75
76    /// Heterogeneous arrays are not supported
77    HeterogeneousArray,
78
79    /// Invalid escape sequence
80    InvalidEscape,
81
82    /// Invalid number format
83    InvalidNumber,
84
85    /// Maps with non-string keys are not supported
86    KeyMustBeAString,
87
88    /// Unit type not supported at top level
89    UnitNotSupported,
90
91    /// Type not supported
92    TypeNotSupported(String),
93}
94
95impl ser::Error for Error {
96    fn custom<T: Display>(msg: T) -> Self {
97        Error::Message(msg.to_string())
98    }
99}
100
101impl de::Error for Error {
102    fn custom<T: Display>(msg: T) -> Self {
103        Error::Message(msg.to_string())
104    }
105}
106
107impl Display for Error {
108    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
109        match self {
110            Error::Message(msg) => formatter.write_str(msg),
111            Error::Eof => formatter.write_str("unexpected end of input"),
112            Error::Syntax => formatter.write_str("syntax error"),
113            Error::ExpectedBoolean => formatter.write_str("expected boolean"),
114            Error::ExpectedInteger => formatter.write_str("expected integer"),
115            Error::ExpectedFloat => formatter.write_str("expected float"),
116            Error::ExpectedString => formatter.write_str("expected string"),
117            Error::ExpectedArray => formatter.write_str("expected array"),
118            Error::ExpectedArrayComma => formatter.write_str("expected comma in array"),
119            Error::ExpectedArrayEnd => formatter.write_str("expected end of array"),
120            Error::ExpectedArraySemicolon => formatter.write_str("expected semicolon after array"),
121            Error::ExpectedList => formatter.write_str("expected list"),
122            Error::ExpectedListComma => formatter.write_str("expected comma in list"),
123            Error::ExpectedListEnd => formatter.write_str("expected end of list"),
124            Error::ExpectedGroup => formatter.write_str("expected group"),
125            Error::ExpectedEquals => formatter.write_str("expected '=' or ':'"),
126            Error::ExpectedSemicolon => formatter.write_str("expected semicolon"),
127            Error::ExpectedCommaOrSemicolon => formatter.write_str("expected comma or semicolon"),
128            Error::ExpectedGroupEnd => formatter.write_str("expected end of group"),
129            Error::ExpectedIdentifier => formatter.write_str("expected identifier"),
130            Error::TrailingCharacters => formatter.write_str("trailing characters"),
131            Error::HeterogeneousArray => formatter.write_str("heterogeneous arrays not supported"),
132            Error::InvalidEscape => formatter.write_str("invalid escape sequence"),
133            Error::InvalidNumber => formatter.write_str("invalid number"),
134            Error::KeyMustBeAString => formatter.write_str("map keys must be strings"),
135            Error::UnitNotSupported => formatter.write_str("unit type not supported"),
136            Error::TypeNotSupported(ty) => write!(formatter, "type not supported: {}", ty),
137        }
138    }
139}
140
141impl std::error::Error for Error {}