Skip to main content

oxicode_vtui/tui/config/
loader.rs

1use std::path::{Path, PathBuf};
2
3use anyhow::{Context, Result};
4use serde::{Deserialize, Serialize};
5
6pub use super::SyntaxHighlightingConfig;
7use super::{
8    AcpConfig, AgentConfig, AutomationConfig, ContextConfig, McpConfig, PromptCacheConfig,
9    PtyConfig, SecurityConfig, ToolsConfig, UiConfig,
10};
11
12#[derive(Debug, Clone, Deserialize, Serialize, Default)]
13pub struct VTCodeConfig {
14    pub agent: AgentConfig,
15    pub ui: UiConfig,
16    pub prompt_cache: PromptCacheConfig,
17    pub mcp: McpConfig,
18    pub acp: AcpConfig,
19    pub automation: AutomationConfig,
20    pub tools: ToolsConfig,
21    pub security: SecurityConfig,
22    pub context: ContextConfig,
23    pub syntax_highlighting: SyntaxHighlightingConfig,
24    pub pty: PtyConfig,
25}
26
27pub struct ConfigManager {
28    path: PathBuf,
29    config: VTCodeConfig,
30}
31
32impl ConfigManager {
33    pub fn load() -> Result<Self> {
34        let cwd = std::env::current_dir().context("failed to read current directory")?;
35        Self::load_from_workspace(cwd)
36    }
37
38    pub fn load_from_workspace(workspace_root: impl AsRef<Path>) -> Result<Self> {
39        let path = workspace_root.as_ref().join("vtcode.toml");
40
41        let config = if path.exists() {
42            let raw = std::fs::read_to_string(&path)
43                .with_context(|| format!("failed to read {}", path.display()))?;
44            toml::from_str::<VTCodeConfig>(&raw)
45                .with_context(|| format!("failed to parse {}", path.display()))?
46        } else {
47            VTCodeConfig::default()
48        };
49
50        Ok(Self { path, config })
51    }
52
53    pub fn config(&self) -> &VTCodeConfig {
54        &self.config
55    }
56
57    pub fn save_config(&self, config: &VTCodeConfig) -> Result<()> {
58        let rendered = toml::to_string_pretty(config).context("failed to serialize config")?;
59        std::fs::write(&self.path, rendered)
60            .with_context(|| format!("failed to write {}", self.path.display()))
61    }
62}