Skip to main content

dkp_core/
config.rs

1use figment::{
2    providers::{Env, Format, Toml},
3    Figment,
4};
5use serde::{Deserialize, Serialize};
6
7#[derive(Debug, Serialize, Deserialize, Default)]
8pub struct DkpConfig {
9    #[serde(default)]
10    pub defaults: ConfigDefaults,
11    #[serde(default)]
12    pub registry: RegistryConfig,
13    #[serde(default)]
14    pub install: InstallConfig,
15}
16
17#[derive(Debug, Serialize, Deserialize, Default)]
18pub struct ConfigDefaults {
19    pub provider: Option<String>,
20    pub model: Option<String>,
21    pub output: Option<String>,
22}
23
24#[derive(Debug, Serialize, Deserialize, Default)]
25pub struct RegistryConfig {
26    pub url: Option<String>,
27    pub token_env: Option<String>,
28    pub key_path: Option<String>,
29}
30
31#[derive(Debug, Serialize, Deserialize, Default)]
32pub struct InstallConfig {
33    /// Project-local install directory name (default: "dkps")
34    pub local_dir: Option<String>,
35    /// Global install directory (default: ~/.dkp/packs)
36    pub global_dir: Option<String>,
37}
38
39impl DkpConfig {
40    pub fn load() -> Self {
41        let mut figment = Figment::new();
42
43        if let Some(home_cfg) = dirs::home_dir()
44            .map(|h| h.join(".dkp").join("config.toml"))
45            .filter(|p| p.exists())
46        {
47            figment = figment.merge(Toml::file(home_cfg));
48        }
49
50        figment
51            .merge(Toml::file(".dkp.toml"))
52            .merge(Env::prefixed("DKP_").split("_"))
53            .extract()
54            .unwrap_or_default()
55    }
56
57    pub fn registry_url(&self) -> &str {
58        self.registry
59            .url
60            .as_deref()
61            .unwrap_or("https://registry.dkp-standard.com")
62    }
63
64    pub fn local_install_dir(&self) -> &str {
65        self.install.local_dir.as_deref().unwrap_or("dkps")
66    }
67}