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