use anyhow::Result;
use serde::{Deserialize, Serialize};
use std::fs;
use std::path::Path;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SnapshotConfig {
pub baseline: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ServerConfig {
pub command: Option<String>,
pub timeout_ms: Option<u64>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DiffConfig {
pub treat_additive_as_warning: Option<bool>,
pub treat_non_breaking_as_warning: Option<bool>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TripwireConfig {
pub snapshot: Option<SnapshotConfig>,
pub server: Option<ServerConfig>,
pub diff: Option<DiffConfig>,
}
impl TripwireConfig {
pub fn load(path: &str) -> Result<Self> {
let content = fs::read_to_string(path)?;
let config: TripwireConfig = toml::from_str(&content)?;
Ok(config)
}
pub fn load_or_default(path: Option<&str>) -> Result<Self> {
if let Some(p) = path {
Self::load(p)
} else if Path::new("trip-test.toml").exists() {
Self::load("trip-test.toml")
} else {
Ok(TripwireConfig {
snapshot: None,
server: None,
diff: None,
})
}
}
pub fn get_baseline(&self) -> Option<String> {
self.snapshot.as_ref().and_then(|s| s.baseline.clone())
}
pub fn get_server_command(&self) -> Option<String> {
self.server.as_ref().and_then(|s| s.command.clone())
}
#[allow(dead_code)]
pub fn get_timeout_ms(&self) -> u64 {
self.server
.as_ref()
.and_then(|s| s.timeout_ms)
.unwrap_or(30000)
}
}