libset/
error.rs

1use thiserror::Error;
2
3/// Custom error type for the library.
4#[derive(Debug, Error)]
5pub enum Error {
6    /// Represents an invalid application name.
7    #[error("'{0}' is not a valid application name, avoid using . or .. .")]
8    InvalidName(String),
9    /// Represents a failure to write to a file.
10    #[error("Failed to write to file: {0}")]
11    Write(atomicwrites::Error<std::io::Error>),
12    /// Represents a filesystem error.
13    #[error("Filesystem error: {0}")]
14    Io(std::io::Error),
15    /// Represents a missing configuration directory.
16    #[error("Config directory not found")]
17    NoConfigDirectory,
18    /// Represents a failure to get a key.
19    #[error("Failed to get key {0} : {1}")]
20    GetKey(String, std::io::Error),
21    /// Represents a failure to parse a ron file.
22    #[cfg(feature = "ron")]
23    #[error("Failed to parse ron file: {0}")]
24    Ron(ron::Error),
25    /// Represents a failure to parse a ron file with span information.
26    #[cfg(feature = "ron")]
27    #[error("Failed to parse ron file: {0}")]
28    RonSpanned(ron::error::SpannedError),
29    /// Represents a failure to parse a json file.
30    #[cfg(feature = "json")]
31    #[error("Failed to parse json file: {0}")]
32    Json(serde_json::Error),
33    /// Represents a failure to serialize a toml file.
34    #[cfg(feature = "toml")]
35    #[error("Failed to serialize toml file: {0}")]
36    TomlSerialize(toml::ser::Error),
37    /// Represents a failure to deserialize a toml file.
38    #[cfg(feature = "toml")]
39    #[error("Failed to deserialize toml file: {0}")]
40    TomlDeserialize(toml::de::Error),
41    /// Represents a generic string error.
42    #[error("An error ocurred: {0}")]
43    Generic(String),
44}
45
46impl From<String> for Error {
47    fn from(f: String) -> Self {
48        Self::Generic(f)
49    }
50}
51
52impl From<atomicwrites::Error<std::io::Error>> for Error {
53    fn from(f: atomicwrites::Error<std::io::Error>) -> Self {
54        Self::Write(f)
55    }
56}
57
58impl From<std::io::Error> for Error {
59    fn from(f: std::io::Error) -> Self {
60        Self::Io(f)
61    }
62}
63
64#[cfg(feature = "ron")]
65impl From<ron::Error> for Error {
66    fn from(f: ron::Error) -> Self {
67        Self::Ron(f)
68    }
69}
70
71#[cfg(feature = "ron")]
72impl From<ron::error::SpannedError> for Error {
73    fn from(f: ron::error::SpannedError) -> Self {
74        Self::RonSpanned(f)
75    }
76}
77
78#[cfg(feature = "json")]
79impl From<serde_json::Error> for Error {
80    fn from(f: serde_json::Error) -> Self {
81        Self::Json(f)
82    }
83}
84
85#[cfg(feature = "toml")]
86impl From<toml::de::Error> for Error {
87    fn from(f: toml::de::Error) -> Self {
88        Self::TomlDeserialize(f)
89    }
90}
91
92#[cfg(feature = "toml")]
93impl From<toml::ser::Error> for Error {
94    fn from(f: toml::ser::Error) -> Self {
95        Self::TomlSerialize(f)
96    }
97}