north_config/
error.rs

1use std::fmt::Display;
2
3/// # Error
4///
5/// Custom error enum for handling various kinds of errors in the application.
6///
7/// The `Error` enum has several variants:
8///
9/// - `IoError`: Represents an I/O error encountered while performing file I/O operations. It contains an `std::io::Error` as the inner error.
10/// - `JsonParseError`: Represents an error encountered while parsing JSON data. It contains a `serde_json::Error` as the inner error.
11/// - `RonParseError`: Represents an error encountered while parsing RON data. This variant is only available if the `"ron"` feature is enabled. It contains
12#[derive(Debug)]
13pub enum Error {
14    IoError(std::io::Error),
15    JsonParseError(serde_json::Error),
16    #[cfg(feature = "ron")]
17    RonParseError(ron::Error),
18    #[cfg(feature = "yaml")]
19    YamlParseError(serde_yaml::Error),
20    #[cfg(feature = "toml")]
21    TomlParseError(toml::de::Error),
22}
23
24impl Display for Error {
25    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
26        match self {
27            Error::IoError(io_error) => write!(f, "{}", io_error),
28            Error::JsonParseError(io_error) => write!(f, "{}", io_error),
29            #[cfg(feature = "ron")]
30            Error::RonParseError(error) => write!(f, "{}", error),
31            #[cfg(feature = "yaml")]
32            Error::YamlParseError(error) => write!(f, "{}", error),
33            #[cfg(feature = "toml")]
34            Error::TomlParseError(error) => write!(f, "{}", error),
35        }
36    }
37}
38
39impl std::error::Error for Error {}
40
41impl From<serde_json::Error> for Error {
42    fn from(e: serde_json::Error) -> Self {
43        Error::JsonParseError(e)
44    }
45}
46
47#[cfg(feature = "ron")]
48impl From<ron::Error> for Error {
49    fn from(e: ron::Error) -> Self {
50        Error::RonParseError(e)
51    }
52}
53
54#[cfg(feature = "yaml")]
55impl From<serde_yaml::Error> for Error {
56    fn from(e: serde_yaml::Error) -> Self {
57        Error::YamlParseError(e)
58    }
59}
60
61#[cfg(feature = "toml")]
62impl From<toml::de::Error> for Error {
63    fn from(e: toml::de::Error) -> Self {
64        Error::TomlParseError(e)
65    }
66}
67
68impl From<std::io::Error> for Error {
69    fn from(e: std::io::Error) -> Self {
70        Error::IoError(e)
71    }
72}