Skip to main content

trip_test/
config.rs

1//! Configuration file parsing (TOML format).
2//!
3//! Reads `trip-test.toml` for settings like baseline snapshot path and server command.
4
5use anyhow::Result;
6use serde::{Deserialize, Serialize};
7use std::fs;
8use std::path::Path;
9
10/// Snapshot configuration section.
11#[derive(Debug, Clone, Serialize, Deserialize)]
12pub struct SnapshotConfig {
13    pub baseline: Option<String>,
14}
15
16#[derive(Debug, Clone, Serialize, Deserialize)]
17pub struct ServerConfig {
18    pub command: Option<String>,
19    pub timeout_ms: Option<u64>,
20}
21
22#[derive(Debug, Clone, Serialize, Deserialize)]
23pub struct DiffConfig {
24    pub treat_additive_as_warning: Option<bool>,
25    pub treat_non_breaking_as_warning: Option<bool>,
26}
27
28#[derive(Debug, Clone, Serialize, Deserialize)]
29pub struct TripTestConfig {
30    pub snapshot: Option<SnapshotConfig>,
31    pub server: Option<ServerConfig>,
32    pub diff: Option<DiffConfig>,
33}
34
35impl TripTestConfig {
36    pub fn load(path: &str) -> Result<Self> {
37        let content = fs::read_to_string(path)?;
38        let config: TripTestConfig = toml::from_str(&content)?;
39        Ok(config)
40    }
41
42    pub fn load_or_default(path: Option<&str>) -> Result<Self> {
43        if let Some(p) = path {
44            Self::load(p)
45        } else if Path::new("trip-test.toml").exists() {
46            Self::load("trip-test.toml")
47        } else {
48            Ok(TripTestConfig {
49                snapshot: None,
50                server: None,
51                diff: None,
52            })
53        }
54    }
55
56    pub fn get_baseline(&self) -> Option<String> {
57        self.snapshot.as_ref().and_then(|s| s.baseline.clone())
58    }
59
60    pub fn get_server_command(&self) -> Option<String> {
61        self.server.as_ref().and_then(|s| s.command.clone())
62    }
63
64    #[allow(dead_code)]
65    pub fn get_timeout_ms(&self) -> u64 {
66        self.server
67            .as_ref()
68            .and_then(|s| s.timeout_ms)
69            .unwrap_or(30000)
70    }
71}