use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[non_exhaustive]
pub struct FileLoggingConfig {
#[serde(default)]
pub enabled: bool,
#[serde(default)]
pub file_path: String,
#[serde(default)]
pub rotation: RotationConfig,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[non_exhaustive]
pub struct RotationConfig {
#[serde(default)]
pub strategy: RotationStrategy,
#[serde(default = "default_retention_days")]
pub retention_days: u32,
}
const fn default_retention_days() -> u32 {
7
}
impl Default for RotationConfig {
fn default() -> Self {
Self {
strategy: RotationStrategy::default(),
retention_days: default_retention_days(),
}
}
}
impl RotationConfig {
pub const fn daily() -> Self {
Self {
strategy: RotationStrategy::Daily,
retention_days: default_retention_days(),
}
}
pub const fn hourly() -> Self {
Self {
strategy: RotationStrategy::Hourly,
retention_days: default_retention_days(),
}
}
pub const fn never() -> Self {
Self {
strategy: RotationStrategy::Never,
retention_days: default_retention_days(),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
#[derive(Default)]
pub enum RotationStrategy {
#[default]
Daily,
Hourly,
Never,
}
impl RotationStrategy {
pub const fn as_str(self) -> &'static str {
match self {
Self::Daily => "daily",
Self::Hourly => "hourly",
Self::Never => "never",
}
}
}
impl FileLoggingConfig {
pub fn builder() -> FileLoggingConfigBuilder {
FileLoggingConfigBuilder::default()
}
pub fn with_env_overrides(mut self) -> Self {
if let Ok(val) = std::env::var("SIGNAL_KIT_FILE_ENABLED") {
self.enabled = parse_bool(&val).unwrap_or(self.enabled);
}
if let Ok(val) = std::env::var("SIGNAL_KIT_FILE_PATH")
&& !val.trim().is_empty()
{
self.file_path = val;
}
if let Ok(val) = std::env::var("SIGNAL_KIT_FILE_ROTATION") {
self.rotation.strategy = match val.to_lowercase().as_str() {
"daily" => RotationStrategy::Daily,
"hourly" => RotationStrategy::Hourly,
"never" => RotationStrategy::Never,
_ => self.rotation.strategy, };
}
if let Ok(val) = std::env::var("SIGNAL_KIT_FILE_RETENTION_DAYS") {
self.rotation.retention_days = val.parse().unwrap_or(self.rotation.retention_days);
}
self
}
}
#[derive(Debug, Default)]
pub struct FileLoggingConfigBuilder {
enabled: bool,
file_path: Option<String>,
rotation: RotationConfig,
}
impl FileLoggingConfigBuilder {
pub fn enabled(mut self, enabled: bool) -> Self {
self.enabled = enabled;
self
}
pub fn file_path(mut self, file_path: impl Into<String>) -> Self {
self.file_path = Some(file_path.into());
self
}
pub fn rotation(mut self, rotation: RotationConfig) -> Self {
self.rotation = rotation;
self
}
pub fn build(self) -> Result<FileLoggingConfig, crate::file_logging::FileLoggingError> {
let file_path = self.file_path.unwrap_or_default();
if self.enabled && file_path.trim().is_empty() {
return Err(crate::file_logging::FileLoggingError::InvalidPath(
"file_path must be set when file logging is enabled".to_string(),
));
}
Ok(FileLoggingConfig {
enabled: self.enabled,
file_path,
rotation: self.rotation,
})
}
}
fn parse_bool(s: &str) -> Option<bool> {
match s.to_lowercase().as_str() {
"true" | "1" => Some(true),
"false" | "0" => Some(false),
_ => None,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_default_file_logging_config() {
let config = FileLoggingConfig::default();
assert!(!config.enabled);
assert!(config.file_path.is_empty());
assert_eq!(config.rotation.strategy, RotationStrategy::Daily);
assert_eq!(config.rotation.retention_days, 7);
}
#[test]
fn test_default_rotation_config() {
let config = RotationConfig::default();
assert_eq!(config.strategy, RotationStrategy::Daily);
assert_eq!(config.retention_days, 7);
}
#[test]
fn test_rotation_strategy_as_str() {
assert_eq!(RotationStrategy::Daily.as_str(), "daily");
assert_eq!(RotationStrategy::Hourly.as_str(), "hourly");
assert_eq!(RotationStrategy::Never.as_str(), "never");
}
#[test]
fn test_parse_bool() {
assert_eq!(parse_bool("true"), Some(true));
assert_eq!(parse_bool("TRUE"), Some(true));
assert_eq!(parse_bool("1"), Some(true));
assert_eq!(parse_bool("false"), Some(false));
assert_eq!(parse_bool("FALSE"), Some(false));
assert_eq!(parse_bool("0"), Some(false));
assert_eq!(parse_bool("invalid"), None);
assert_eq!(parse_bool("2"), None);
}
#[test]
fn test_file_logging_config_with_env_overrides() {
let config = FileLoggingConfig::default();
let overridden = config.with_env_overrides();
assert!(!overridden.enabled);
assert!(overridden.file_path.is_empty());
}
#[test]
fn test_rotation_strategy_serde_roundtrip() {
let strategies = [
RotationStrategy::Daily,
RotationStrategy::Hourly,
RotationStrategy::Never,
];
for strategy in strategies {
let serialized = serde_json::to_string(&strategy).unwrap();
let deserialized: RotationStrategy = serde_json::from_str(&serialized).unwrap();
assert_eq!(strategy, deserialized);
}
}
#[test]
fn test_file_logging_config_serde_roundtrip() {
let config = FileLoggingConfig {
enabled: true,
file_path: "/var/log/test.log".to_string(),
rotation: RotationConfig {
strategy: RotationStrategy::Hourly,
retention_days: 14,
},
};
let serialized = serde_json::to_string(&config).unwrap();
let deserialized: FileLoggingConfig = serde_json::from_str(&serialized).unwrap();
assert_eq!(deserialized.enabled, config.enabled);
assert_eq!(deserialized.file_path, config.file_path);
assert_eq!(deserialized.rotation.strategy, config.rotation.strategy);
assert_eq!(
deserialized.rotation.retention_days,
config.rotation.retention_days
);
}
}