Skip to main content

config/
error.rs

1use crate::{de, ser};
2use std::{fmt::Debug, path::PathBuf};
3use thiserror::Error;
4
5/// Defines the possible configuration errors.
6#[derive(Error, Debug)]
7pub enum Error {
8    /// Indicates a custom configuration error has occurred.
9    #[error("{0}")]
10    Custom(String),
11
12    /// Indicates an invalid configuration file has been provided.
13    #[error("{message}")]
14    InvalidFile {
15        /// Gets the error message.
16        message: String,
17
18        /// Gets the path of the file being loaded.
19        path: PathBuf,
20    },
21
22    /// Indicates a required configuration file is missing.
23    #[error("The configuration file '{0}' was not found, but is required.")]
24    MissingFile(PathBuf),
25
26    /// Indicates that a reification operation failed.
27    #[error(transparent)]
28    ReifyFailed(#[from] de::Error),
29
30    /// Indicates that a serialization operation failed.
31    #[error(transparent)]
32    SerializeFailed(#[from] ser::Error),
33
34    /// Indicates that an unknown [error](std::error::Error) occurred.
35    #[error(transparent)]
36    Unknown(#[from] Box<dyn std::error::Error + Send + Sync>),
37}
38
39impl Error {
40    /// Creates a new [custom](Self::Custom) error.
41    ///
42    /// # Arguments
43    ///
44    /// * `message` - The error message
45    #[inline]
46    pub fn custom(message: impl Into<String>) -> Self {
47        Self::Custom(message.into())
48    }
49
50    /// Creates a new [unknown](Self::Unknown) error.
51    ///
52    /// # Arguments
53    ///
54    /// * `error` - The unknown [error](std::error::Error)
55    #[inline]
56    pub fn unknown(error: impl std::error::Error + Send + Sync + 'static) -> Self {
57        Self::Unknown(Box::new(error))
58    }
59}