use crate::config::locking::{HierarchicalLockManager, audit_log};
use crate::{Error, Result};
pub struct ConfigValidator;
impl ConfigValidator {
#[allow(clippy::collapsible_if)]
pub async fn validate_change(key: &str, new_value: &str) -> Result<()> {
let lock_manager = HierarchicalLockManager::load().await?;
if let Some((level, entry)) = lock_manager.is_locked(key) {
if entry.value != new_value {
audit_log::log_blocked_attempt(
key,
new_value,
level,
"Attempted change while locked",
)
.await?;
return Err(Error::config(format!(
"Cannot change '{}' - it is locked at {} level\nCurrent value: {}\nAttempted value: {}\nLock reason: {}\n\nTo unlock, run:\n ferrous-forge config unlock {} --level={} --reason=\"...\"",
key,
level.display_name(),
entry.value,
new_value,
entry.reason,
key,
level.display_name().to_lowercase()
)));
}
}
Ok(())
}
pub async fn can_modify(key: &str, new_value: &str) -> bool {
matches!(Self::validate_change(key, new_value).await, Ok(()))
}
}