Skip to main content

zig_core/
config.rs

1//! Global zig configuration loaded from `~/.zig/config.toml`.
2//!
3//! Every field is optional so partial files are valid. Missing or unreadable
4//! files fall back to built-in defaults.
5
6use std::path::PathBuf;
7
8use serde::{Deserialize, Serialize};
9
10/// Top-level config structure backing `~/.zig/config.toml`.
11#[derive(Debug, Clone, Default, Serialize, Deserialize)]
12pub struct ZigConfig {
13    #[serde(default)]
14    pub memory: MemorySection,
15}
16
17/// `[memory]` section of the global config file.
18#[derive(Debug, Clone, Serialize, Deserialize)]
19pub struct MemorySection {
20    /// Whether project-local memory (`<git-root>/.zig/memory/`) is enabled.
21    /// When `false`, only the global tiers are used.
22    #[serde(default = "default_true")]
23    pub local: bool,
24}
25
26fn default_true() -> bool {
27    true
28}
29
30impl Default for MemorySection {
31    fn default() -> Self {
32        Self { local: true }
33    }
34}
35
36impl ZigConfig {
37    /// Returns the absolute path to the global config file (`~/.zig/config.toml`).
38    pub fn config_path() -> PathBuf {
39        crate::paths::global_base_dir()
40            .unwrap_or_else(|| PathBuf::from(".zig"))
41            .join("config.toml")
42    }
43
44    /// Load the global config file. Returns a default (empty) config if the
45    /// file is missing or unreadable, so callers can treat it as opt-in.
46    pub fn load() -> Self {
47        match std::fs::read_to_string(Self::config_path()) {
48            Ok(contents) => toml::from_str(&contents).unwrap_or_default(),
49            Err(_) => Self::default(),
50        }
51    }
52}
53
54#[cfg(test)]
55#[path = "config_tests.rs"]
56mod tests;