Skip to main content

confy_rs/
error.rs

1//! Error types returned by this crate.
2
3use std::path::PathBuf;
4
5use crate::source::Format;
6
7/// All errors that can occur while loading or watching configuration.
8#[derive(Debug, thiserror::Error)]
9#[non_exhaustive]
10pub enum Error {
11    /// A required configuration file does not exist.
12    #[error("configuration file not found: {}", .path.display())]
13    NotFound {
14        /// Path of the missing file.
15        path: PathBuf,
16    },
17
18    /// Reading a configuration file failed for I/O reasons other than absence.
19    #[error("failed to read configuration file {}: {source}", .path.display())]
20    Io {
21        /// Path of the file that could not be read.
22        path: PathBuf,
23        /// Underlying I/O error.
24        #[source]
25        source: std::io::Error,
26    },
27
28    /// The file extension does not map to any known configuration format.
29    #[error("cannot detect configuration format of `{}` (expected .toml, .yaml, .yml or .json)", .path.display())]
30    UnknownExtension {
31        /// Path whose extension could not be recognized.
32        path: PathBuf,
33    },
34
35    /// The format is recognized but its cargo feature is not enabled.
36    #[error("{format} support is disabled; enable the `{feature}` feature of confy-rs")]
37    FeatureDisabled {
38        /// The requested format.
39        format: Format,
40        /// Name of the cargo feature that must be enabled.
41        feature: &'static str,
42    },
43
44    /// The content is not valid for its format.
45    #[error("failed to parse {} as {format}: {message}", display_origin(.origin))]
46    Parse {
47        /// Path of the offending file, or `None` when parsing from a string.
48        origin: Option<PathBuf>,
49        /// The format that was being parsed.
50        format: Format,
51        /// Parser error message, including position info when available.
52        message: String,
53    },
54
55    /// The merged configuration does not match the target type.
56    #[error("failed to deserialize configuration: {0}")]
57    Deserialize(#[source] serde_json::Error),
58
59    /// Serializing the defaults layer failed.
60    #[error("failed to serialize defaults layer: {0}")]
61    Defaults(#[source] serde_json::Error),
62
63    /// Setting up the file watcher failed.
64    #[cfg(feature = "watch")]
65    #[error("failed to watch configuration files: {0}")]
66    Watch(#[from] notify::Error),
67}
68
69fn display_origin(origin: &Option<PathBuf>) -> String {
70    match origin {
71        Some(path) => path.display().to_string(),
72        None => "inline string".to_string(),
73    }
74}