use std::path::PathBuf;
use thiserror::Error;
#[derive(Debug, Error)]
pub enum ConfError {
#[error("conf: failed to read configuration file '{path}': {source}")]
Io {
path: PathBuf,
#[source]
source: std::io::Error,
},
#[error("conf: configuration document is empty")]
EmptyDocument,
#[error("conf: configuration must contain exactly one pool, found {0}")]
TooManyPools(usize),
#[error("conf: pool name must not be empty")]
EmptyPoolName,
#[error("conf: directive '{name}' is unknown")]
UnknownKey {
name: String,
},
#[error("conf: directive '{0}' is missing")]
MissingRequired(&'static str),
#[error("conf: '{field}' has an invalid address '{value}': {reason}")]
BadAddr {
field: &'static str,
value: String,
reason: String,
},
#[error("conf: token list '{value}' is not valid: {reason}")]
BadToken {
value: String,
reason: String,
},
#[error("conf: directive '{field}' must be one of 'DC_ONE', 'DC_QUORUM', 'DC_SAFE_QUORUM', 'DC_EACH_SAFE_QUORUM', got '{value}'")]
BadConsistency {
field: &'static str,
value: String,
},
#[error("conf: directive 'secure_server_option' must be one of 'none', 'rack', 'datacenter', 'all', got '{0}'")]
BadSecure(String),
#[error("conf: directive 'data_store' must be 0 (redis), 1 (memcache), or 2 (noxu), got {0}")]
BadDataStore(i64),
#[error("conf: {0}")]
BadNoxuConfig(&'static str),
#[error("conf: directive 'hash' is not a valid hash function, got '{0}'")]
BadHash(String),
#[error("conf: directive 'distribution' is not a valid distribution, got '{0}'")]
BadDistribution(String),
#[error("conf: directive '{field}' value {value} is out of range: {reason}")]
OutOfRange {
field: &'static str,
value: i64,
reason: &'static str,
},
#[error("conf: '{field}' entry '{value}' is invalid: {reason}")]
BadServer {
field: &'static str,
value: String,
reason: String,
},
#[error("conf: directive 'hash_tag' must be a string of exactly 2 characters, got '{0}'")]
BadHashTag(String),
#[error("conf: yaml parse error: {message}")]
Yaml {
message: String,
},
}
impl ConfError {
pub(crate) fn from_yaml(err: &serde_yaml::Error) -> Self {
let message = err.to_string();
if let Some(start) = message.find("unknown field `") {
let after = &message[start + "unknown field `".len()..];
if let Some(end) = after.find('`') {
return ConfError::UnknownKey {
name: after[..end].to_string(),
};
}
}
ConfError::Yaml { message }
}
}