#[derive(Debug, Clone, Default)]
pub struct SaltConfig {
pub current: Option<String>,
pub previous: Option<String>,
}
impl SaltConfig {
#[must_use]
pub const fn new(current: Option<String>) -> Self {
Self {
current,
previous: None,
}
}
#[must_use]
pub const fn with_rotation(current: Option<String>, previous: Option<String>) -> Self {
Self { current, previous }
}
#[must_use]
pub const fn has_salt(&self) -> bool {
self.current.is_some() || self.previous.is_some()
}
#[must_use]
pub fn write_salt(&self) -> Option<&str> {
self.current.as_deref().or(self.previous.as_deref())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_salt_config_new() {
let config = SaltConfig::new(Some("current".to_string()));
assert_eq!(config.current, Some("current".to_string()));
assert_eq!(config.previous, None);
assert!(config.has_salt());
assert_eq!(config.write_salt(), Some("current"));
}
#[test]
fn test_salt_config_with_rotation() {
let config =
SaltConfig::with_rotation(Some("new-salt".to_string()), Some("old-salt".to_string()));
assert_eq!(config.current, Some("new-salt".to_string()));
assert_eq!(config.previous, Some("old-salt".to_string()));
assert!(config.has_salt());
assert_eq!(config.write_salt(), Some("new-salt"));
}
#[test]
fn test_salt_config_no_current_uses_previous() {
let config = SaltConfig::with_rotation(None, Some("old-salt".to_string()));
assert!(config.has_salt());
assert_eq!(config.write_salt(), Some("old-salt"));
}
#[test]
fn test_salt_config_empty() {
let config = SaltConfig::default();
assert!(!config.has_salt());
assert_eq!(config.write_salt(), None);
}
}