use std::path::PathBuf;
use serde::{Deserialize, Serialize};
use crate::io::{FileWriterConfig, RotationPeriod};
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct FileOutputConfig {
pub enabled: bool,
pub path: PathBuf,
pub filename: String,
pub rotation: RotationPeriod,
pub max_age_days: u32,
pub compress_rotated: bool,
}
impl Default for FileOutputConfig {
fn default() -> Self {
Self {
enabled: false,
path: PathBuf::from("/var/spool/dfe/output"),
filename: "events.ndjson".to_string(),
rotation: RotationPeriod::Hourly,
max_age_days: 7,
compress_rotated: true,
}
}
}
impl FileOutputConfig {
#[must_use]
pub fn to_writer_config(&self) -> FileWriterConfig {
FileWriterConfig {
path: self.path.clone(),
rotation: self.rotation,
max_age_days: self.max_age_days,
compress_rotated: self.compress_rotated,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_defaults() {
let config = FileOutputConfig::default();
assert!(!config.enabled);
assert_eq!(config.path, PathBuf::from("/var/spool/dfe/output"));
assert_eq!(config.filename, "events.ndjson");
assert_eq!(config.rotation, RotationPeriod::Hourly);
assert_eq!(config.max_age_days, 7);
assert!(config.compress_rotated);
}
#[test]
fn test_serde_roundtrip() {
let config = FileOutputConfig {
enabled: true,
path: "/tmp/test-output".into(),
filename: "data.ndjson".into(),
rotation: RotationPeriod::Daily,
max_age_days: 30,
compress_rotated: false,
};
let json = serde_json::to_string(&config).expect("serialise");
let parsed: FileOutputConfig = serde_json::from_str(&json).expect("deserialise");
assert!(parsed.enabled);
assert_eq!(parsed.filename, "data.ndjson");
assert_eq!(parsed.rotation, RotationPeriod::Daily);
}
}