Skip to main content

config_file2/
error.rs

1#[cfg(feature = "toml")]
2use toml_crate as toml;
3
4pub type Result<T> = std::result::Result<T, Error>;
5
6/// This type represents all possible errors that can occur when loading or
7/// storing data from a configuration file.
8#[derive(thiserror::Error, Debug)]
9pub enum Error {
10    /// There was an error while reading the configuration file
11    #[error("couldn't read or write config file")]
12    FileAccess(#[from] std::io::Error),
13
14    #[error("file already exists")]
15    FileExists,
16
17    /// There was an error while parsing the JSON data
18    #[cfg(feature = "json")]
19    #[error("couldn't parse JSON file")]
20    Json(#[from] serde_json::Error),
21
22    /// There was an error while parsing the TOML data
23    #[cfg(feature = "toml")]
24    #[error("couldn't parse TOML file")]
25    Toml(#[from] TomlError),
26
27    /// There was an error while parsing the XML data
28    #[cfg(feature = "xml")]
29    #[error("couldn't parse XML file")]
30    Xml(#[from] XmlError),
31
32    /// There was an error while parsing the YAML data
33    #[cfg(feature = "yaml")]
34    #[error("couldn't parse YAML file")]
35    Yaml(#[from] yaml_serde::Error),
36
37    /// There was an error while parsing the Ron data
38    #[cfg(feature = "ron")]
39    #[error("couldn't parse Ron file")]
40    Ron(#[from] ron_crate::Error),
41
42    /// There was an error while parsing the JSON5 data
43    #[cfg(feature = "json5")]
44    #[error("couldn't parse JSON5 file")]
45    Json5(#[from] Json5Error),
46
47    /// We don't know how to parse this format according to the file extension
48    #[error("don't know how to parse file")]
49    UnsupportedFormat,
50}
51
52/// Merge two TOML errors into one
53#[cfg(feature = "toml")]
54#[derive(Debug, thiserror::Error)]
55pub enum TomlError {
56    /// TOML deserialization error
57    #[error("Toml deserialization error: {0}")]
58    DeserializationError(#[from] toml::de::Error),
59
60    /// TOML serialization error
61    #[error("Toml serialization error: {0}")]
62    SerializationError(#[from] toml::ser::Error),
63}
64
65/// Merge two XML errors into one
66#[cfg(feature = "xml")]
67#[derive(Debug, thiserror::Error)]
68pub enum XmlError {
69    /// XML deserialization error
70    #[error("Xml deserialization error: {0}")]
71    DeserializationError(#[from] quick_xml::DeError),
72
73    /// XML serialization error
74    #[error("Xml serialization error: {0}")]
75    SerializationError(#[from] quick_xml::SeError),
76}
77
78/// Merge two JSON5 errors into one
79#[cfg(feature = "json5")]
80#[derive(Debug, thiserror::Error)]
81pub enum Json5Error {
82    /// JSON5 deserialization error
83    #[error("Json5 deserialization error: {0}")]
84    DeserializationError(#[from] json_five::de::SerdeJSON5Error),
85
86    /// JSON5 serialization error
87    #[error("Json5 serialization error: {0}")]
88    SerializationError(#[from] json_five::ser::SerdeJSON5Error),
89}