Skip to main content

caixa_resolver/
config.rs

1use serde::{Deserialize, Serialize};
2use std::path::PathBuf;
3
4/// Resolver configuration — lives at `~/.config/caixa/config.yaml`.
5///
6/// The whole file is optional; defaults work out of the box. When a user
7/// wants to point `:nome` shorthand at a non-default org, they edit this.
8#[derive(Debug, Clone, Serialize, Deserialize)]
9pub struct ResolverConfig {
10    /// How to expand a bare `:nome "x"` when `:fonte` is omitted.
11    /// Default: `github:pleme-io`.
12    #[serde(default = "default_host")]
13    pub default_host: String,
14
15    /// Where to cache cloned repos. Default: `$XDG_CACHE_HOME/caixa` or
16    /// `~/.cache/caixa`.
17    #[serde(default, skip_serializing_if = "Option::is_none")]
18    pub cache_dir: Option<PathBuf>,
19
20    /// Include `:deps-dev` when resolving.
21    #[serde(default)]
22    pub include_dev: bool,
23
24    /// Extra hosts the resolver recognizes as shorthand prefixes.
25    /// E.g. `["codeberg:my-org"]` lets users write `(:nome "x" :fonte
26    /// (:tipo git :repo "codeberg:my-org/x"))`.
27    #[serde(default)]
28    pub additional_hosts: Vec<String>,
29}
30
31impl Default for ResolverConfig {
32    fn default() -> Self {
33        Self {
34            default_host: default_host(),
35            cache_dir: None,
36            include_dev: false,
37            additional_hosts: Vec::new(),
38        }
39    }
40}
41
42fn default_host() -> String {
43    "github:pleme-io".to_string()
44}
45
46impl ResolverConfig {
47    /// Load from `~/.config/caixa/config.yaml`, falling back to defaults.
48    pub fn load_or_default() -> Self {
49        let Some(base) = dirs::config_dir() else {
50            return Self::default();
51        };
52        let path = base.join("caixa").join("config.yaml");
53        match std::fs::read_to_string(&path) {
54            Ok(src) => serde_yaml::from_str(&src).unwrap_or_default(),
55            Err(_) => Self::default(),
56        }
57    }
58}