use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
use std::path::Path;
use tracing::{debug, trace, warn};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum ConfigLoadFailureKind {
Missing,
LoadError,
}
#[derive(Clone, Debug)]
pub(super) struct ConfigLoadWarning {
path: BoundedConfigPath,
failure_kind: ConfigLoadFailureKind,
}
impl ConfigLoadWarning {
pub(super) fn new(path: &Path, failure_kind: ConfigLoadFailureKind) -> Self {
Self {
path: BoundedConfigPath::from_path(Some(path)),
failure_kind,
}
}
pub(super) fn emit(&self) {
warn_explicit_config_load_failed_from_fields(&self.path, self.failure_kind);
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub(super) struct BoundedConfigPath {
pub(super) hash: Option<String>,
pub(super) is_present: bool,
}
impl BoundedConfigPath {
pub(super) fn from_path(path: Option<&Path>) -> Self {
Self {
hash: path.map(path_hash),
is_present: path.is_some(),
}
}
}
pub(super) fn trace_config_path_variable_from_fields(var_name: &str, path: &BoundedConfigPath) {
trace!(
var_name,
found = path.is_present,
path_hash = path.hash.as_deref(),
"read config path variable"
);
}
pub(super) fn warn_explicit_config_load_failed_from_fields(
path: &BoundedConfigPath,
failure_kind: ConfigLoadFailureKind,
) {
let path_hash = path.hash.as_deref().unwrap_or_default();
warn!(
path_hash = %path_hash,
failure_kind = ?failure_kind,
"explicit config load failed"
);
}
pub(super) fn debug_config_path_from_fields(message: &'static str, path: &BoundedConfigPath) {
let path_hash = path.hash.as_deref().unwrap_or_default();
debug!(
path_hash = %path_hash,
message
);
}
pub(super) fn debug_optional_config_path_from_fields(
message: &'static str,
path: &BoundedConfigPath,
) {
debug!(
path_hash = path.hash.as_deref(),
path_present = path.is_present,
message
);
}
pub(super) fn short_hash(value: &[u8]) -> String {
let mut hasher = DefaultHasher::new();
value.hash(&mut hasher);
format!("{:016x}", hasher.finish())
}
pub(super) fn path_hash(path: &Path) -> String {
short_hash(path.to_string_lossy().as_bytes())
}