Skip to main content

observer_core/
config.rs

1// SPDX-FileCopyrightText: 2026 Alexander R. Croft
2// SPDX-License-Identifier: GPL-3.0-or-later
3
4use crate::error::{ObserverError, ObserverResult};
5use serde::{Deserialize, Serialize};
6use std::collections::BTreeMap;
7use std::path::{Path, PathBuf};
8
9#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
10pub struct ObserverConfig {
11    pub version: String,
12    #[serde(default)]
13    pub analytics: AnalyticsConfig,
14    #[serde(default)]
15    pub providers: BTreeMap<String, ProviderConfig>,
16}
17
18#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
19pub struct AnalyticsConfig {
20    #[serde(default)]
21    pub enabled: bool,
22}
23
24#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
25pub struct ProviderConfig {
26    pub command: String,
27    #[serde(default)]
28    pub args: Vec<String>,
29    pub cwd: Option<String>,
30    #[serde(default)]
31    pub inherit_env: bool,
32    #[serde(default)]
33    pub env: BTreeMap<String, String>,
34}
35
36impl ObserverConfig {
37    pub fn parse(source: &str) -> ObserverResult<Self> {
38        toml::from_str(source).map_err(|error| ObserverError::Config(error.to_string()))
39    }
40
41    pub fn parse_file(path: &Path) -> ObserverResult<Self> {
42        let source = std::fs::read_to_string(path)
43            .map_err(|error| ObserverError::Config(error.to_string()))?;
44        let config = Self::parse(&source)?;
45        if config.version != "0" {
46            return Err(ObserverError::Config(format!(
47                "unsupported observer.toml version `{}`",
48                config.version
49            )));
50        }
51        Ok(config)
52    }
53
54    pub fn default_config_path(workspace_root: &Path) -> PathBuf {
55        workspace_root.join("observer.toml")
56    }
57}