use std::error::Error;
use std::fmt;
use qubit_config::ConfigError;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RetryConfigError {
path: String,
message: String,
}
impl RetryConfigError {
#[inline]
pub fn invalid_value(path: impl Into<String>, message: impl Into<String>) -> Self {
Self {
path: path.into(),
message: message.into(),
}
}
#[inline]
pub fn from_config(path: impl Into<String>, source: ConfigError) -> Self {
Self {
path: path.into(),
message: source.to_string(),
}
}
#[inline]
pub fn path(&self) -> &str {
&self.path
}
#[inline]
pub fn message(&self) -> &str {
&self.message
}
}
impl fmt::Display for RetryConfigError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if self.path.is_empty() {
write!(f, "invalid retry configuration: {}", self.message)
} else {
write!(
f,
"invalid retry configuration at '{}': {}",
self.path, self.message
)
}
}
}
impl Error for RetryConfigError {}
impl From<ConfigError> for RetryConfigError {
#[inline]
fn from(source: ConfigError) -> Self {
let path = match &source {
ConfigError::PropertyNotFound(path)
| ConfigError::PropertyHasNoValue(path)
| ConfigError::PropertyIsFinal(path) => path.clone(),
ConfigError::TypeMismatch { key, .. } | ConfigError::ConversionError { key, .. } => {
key.clone()
}
ConfigError::DeserializeError { path, .. } => path.clone(),
ConfigError::IndexOutOfBounds { .. }
| ConfigError::SubstitutionError(_)
| ConfigError::SubstitutionDepthExceeded(_)
| ConfigError::MergeError(_)
| ConfigError::IoError(_)
| ConfigError::ParseError(_)
| ConfigError::Other(_) => String::new(),
};
Self::from_config(path, source)
}
}