use owo_colors::*;
use std::convert::Infallible;
use std::sync::Arc;
#[allow(unused)]
use crate::LoadLocation;
#[derive(Debug, Clone, thiserror::Error)]
pub enum Error {
#[error("Network error loading config from {}", url.yellow())]
#[cfg_attr(docsrs, doc(cfg(any(feature = "url-blocking", feature = "url-async"))))]
#[cfg(any(feature = "url-blocking", feature = "url-async"))]
Network {
url: String,
#[source]
source: Arc<reqwest::Error>,
},
#[error("I/O error loading config from {}", path.yellow())]
Io {
path: String,
#[source]
source: Arc<std::io::Error>,
},
#[cfg(any(feature = "json", feature = "jsonc"))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "json", feature = "jsonc"))))]
#[error("JSON error loading config from {}", location)]
Json {
#[source]
source: Arc<serde_json::Error>,
location: LoadLocation,
},
#[cfg(feature = "toml")]
#[cfg_attr(docsrs, doc(cfg(feature = "toml")))]
#[error("TOML error loading config from {}", location)]
Toml {
#[source]
source: toml::de::Error,
location: LoadLocation,
},
#[error("YAML error loading config from {}", location)]
#[cfg(feature = "yaml")]
#[cfg_attr(docsrs, doc(cfg(feature = "yaml")))]
Yaml {
#[source]
source: Arc<serde_yaml::Error>,
location: LoadLocation,
},
#[cfg(feature = "env")]
#[cfg_attr(docsrs, doc(cfg(feature = "env")))]
#[error(transparent)]
FromEnv(#[from] FromEnvError),
#[error(transparent)]
Merge(#[from] MergeError),
#[error(transparent)]
FromPartial(#[from] FromPartialError),
}
#[derive(Debug, Clone, thiserror::Error)]
#[error("error merging config field {}: {}", field.yellow(), message)]
pub struct MergeError {
pub field: String,
pub message: String,
}
#[cfg(feature = "env")]
#[cfg_attr(docsrs, doc(cfg(feature = "env")))]
#[derive(Debug, Clone, thiserror::Error)]
#[error("error parsing var {} from env for field: {}: {}", key.yellow(), field.yellow(), message)]
pub struct FromEnvError {
pub key: String,
pub field: String,
pub message: String,
}
#[derive(Debug, Clone, thiserror::Error)]
#[error("missing properties {} in finished config", missing_properties.iter().map(|name| name.yellow().to_string()).collect::<Vec<_>>().join(", ") )]
pub struct FromPartialError {
pub missing_properties: Vec<String>,
}
macro_rules! impl_from_infallible {
($($ty:ty)*) => {
$(
impl From<Infallible> for $ty {
fn from(e: Infallible) -> Self {
match e {}
}
}
)*
}
}
impl_from_infallible!(
Error
MergeError
FromPartialError
);
#[cfg(feature = "env")]
impl_from_infallible!(
FromEnvError
);