Skip to main content

zeph_config/
defaults.rs

1// SPDX-FileCopyrightText: 2026 Andrei G <bug-ops>
2// SPDX-License-Identifier: MIT OR Apache-2.0
3
4use std::path::PathBuf;
5
6/// Legacy project-relative `SQLite` path (pre-XDG migration).
7///
8/// Used only for detecting and migrating old configs that still reference this path.
9pub const DEFAULT_SQLITE_PATH: &str = ".zeph/data/zeph.db";
10/// Legacy project-relative skills directory (pre-XDG migration).
11pub const DEFAULT_SKILLS_DIR: &str = ".zeph/skills";
12/// Legacy project-relative debug output directory (pre-XDG migration).
13pub const DEFAULT_DEBUG_DIR: &str = ".zeph/debug";
14/// Legacy project-relative log file path (pre-XDG migration).
15pub const DEFAULT_LOG_FILE: &str = ".zeph/logs/zeph.log";
16
17#[cfg(any(target_os = "macos", target_os = "windows"))]
18const PLATFORM_APP_DIR_NAME: &str = "Zeph";
19#[cfg(not(any(target_os = "macos", target_os = "windows")))]
20const PLATFORM_APP_DIR_NAME: &str = "zeph";
21
22/// Platform default writable data root.
23///
24/// Examples:
25/// - Linux: `~/.local/share/zeph`
26/// - macOS: `~/Library/Application Support/Zeph`
27/// - Windows: `%LOCALAPPDATA%\Zeph`
28pub(crate) fn default_runtime_data_root() -> PathBuf {
29    dirs::data_local_dir()
30        .or_else(dirs::data_dir)
31        .or_else(|| dirs::home_dir().map(|home| home.join(".local").join("share")))
32        .unwrap_or_else(|| PathBuf::from("."))
33        .join(PLATFORM_APP_DIR_NAME)
34}
35
36/// Returns the platform-appropriate default path for the `SQLite` database.
37///
38/// # Examples
39///
40/// ```
41/// let path = zeph_config::default_sqlite_path();
42/// assert!(path.ends_with("zeph.db"));
43/// ```
44#[must_use]
45pub fn default_sqlite_path() -> String {
46    default_runtime_data_root()
47        .join("data")
48        .join("zeph.db")
49        .to_string_lossy()
50        .into_owned()
51}
52
53/// Returns the default vault/config directory for skills.
54///
55/// Mirrors the logic in zeph-core's `default_vault_dir` but without depending on that crate.
56#[must_use]
57pub fn default_vault_dir() -> PathBuf {
58    if let Ok(xdg) = std::env::var("XDG_CONFIG_HOME") {
59        return PathBuf::from(xdg).join("zeph");
60    }
61    if let Ok(appdata) = std::env::var("APPDATA") {
62        return PathBuf::from(appdata).join("zeph");
63    }
64    let home = std::env::var("HOME").unwrap_or_else(|_| ".".to_owned());
65    PathBuf::from(home).join(".config").join("zeph")
66}
67
68/// Returns the platform-appropriate default directory for user skills.
69///
70/// # Examples
71///
72/// ```
73/// let dir = zeph_config::default_skills_dir();
74/// assert!(dir.ends_with("skills"));
75/// ```
76#[must_use]
77pub fn default_skills_dir() -> String {
78    default_vault_dir()
79        .join("skills")
80        .to_string_lossy()
81        .into_owned()
82}
83
84/// Returns the platform-appropriate default directory for debug output.
85#[must_use]
86pub fn default_debug_dir() -> PathBuf {
87    default_runtime_data_root().join("debug")
88}
89
90/// Returns the platform-appropriate default path for the log file.
91///
92/// # Examples
93///
94/// ```
95/// let path = zeph_config::default_log_file_path();
96/// assert!(path.ends_with("zeph.log"));
97/// ```
98#[must_use]
99pub fn default_log_file_path() -> String {
100    default_runtime_data_root()
101        .join("logs")
102        .join("zeph.log")
103        .to_string_lossy()
104        .into_owned()
105}
106
107/// Returns the default `skills.paths` vector containing [`default_skills_dir`].
108#[must_use]
109pub fn default_skill_paths() -> Vec<String> {
110    vec![default_skills_dir()]
111}
112
113pub(crate) fn default_log_file() -> String {
114    default_log_file_path()
115}
116
117/// Alias for [`default_sqlite_path`] used as a serde default function.
118#[must_use]
119pub fn default_sqlite_path_field() -> String {
120    default_sqlite_path()
121}
122
123pub(crate) fn default_debug_output_dir() -> PathBuf {
124    default_debug_dir()
125}
126
127/// Returns `true` when `path` is the legacy project-relative `SQLite` path that must be migrated.
128#[must_use]
129pub fn is_legacy_default_sqlite_path(path: &str) -> bool {
130    path == DEFAULT_SQLITE_PATH
131}
132
133/// Returns `true` when `path` is the legacy project-relative skills directory that must be migrated.
134#[must_use]
135pub fn is_legacy_default_skills_path(path: &str) -> bool {
136    path == DEFAULT_SKILLS_DIR
137}
138
139/// Returns `true` when `path` is the legacy project-relative debug directory that must be migrated.
140#[must_use]
141pub fn is_legacy_default_debug_dir(path: &std::path::Path) -> bool {
142    path == std::path::Path::new(DEFAULT_DEBUG_DIR)
143}
144
145/// Returns `true` when `path` is the legacy project-relative log file path that must be migrated.
146#[must_use]
147pub fn is_legacy_default_log_file(path: &str) -> bool {
148    path == DEFAULT_LOG_FILE
149}
150
151pub(crate) fn default_true() -> bool {
152    true
153}