use std::fmt;
use std::path::Path;
const DEFAULT_SEPERATOR: &str = "__";
pub struct EnvConfig {
pub name: String, pub separator: Option<String>, }
impl EnvConfig {
pub fn new(name: String, separator: Option<String>) -> Self {
Self { name, separator }
}
pub fn get_separator(&self) -> &str {
self.separator.as_deref().unwrap_or(DEFAULT_SEPERATOR)
}
}
pub struct LoadingParam<'a> {
pub file: Option<&'a Path>, pub env_prefix: Option<EnvConfig>, }
#[derive(Debug)]
pub enum ConfigError {
Config(config::ConfigError),
FileNotFound(std::path::PathBuf),
ShowSettingsParseError(String),
InvalidLoadingParam,
InvalidEnvConfig { prefix: String, separator: String },
}
impl fmt::Display for ConfigError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ConfigError::Config(err) => write!(f, "Config error: {}", err),
ConfigError::FileNotFound(path) => {
write!(f, "Configuration file not found: {:?}", path)
}
ConfigError::ShowSettingsParseError(value) => {
write!(
f,
"Cannot parse SHOW_SETTINGS environment variable '{}' as boolean",
value
)
}
ConfigError::InvalidLoadingParam => {
write!(f, "No configuration source provided. Please configure at least one of:\n\
- Configuration file (set the 'file' parameter)\n\
- Environment variables (set the 'env_prefix' parameter with a valid prefix)")
}
ConfigError::InvalidEnvConfig { prefix, separator } => {
write!(f, "Invalid environment configuration: env prefix '{}' contains separator '{}'.\n\
This will cause configuration loading to fail. Please choose a prefix that doesn't contain the separator,\n\
or use a different separator character.", prefix, separator)
}
}
}
}
impl std::error::Error for ConfigError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
ConfigError::Config(err) => Some(err),
_ => None,
}
}
}
impl From<config::ConfigError> for ConfigError {
fn from(err: config::ConfigError) -> Self {
ConfigError::Config(err)
}
}