linsight_core/paths.rs
1// SPDX-FileCopyrightText: 2026 VisorCraft LLC
2// SPDX-License-Identifier: GPL-3.0-only
3
4//! Shared filesystem-path helpers.
5
6use std::path::PathBuf;
7
8/// Resolve the history DB path.
9///
10/// Resolution order (mirrors XDG Base Directory spec):
11/// 1. `$XDG_DATA_HOME/linsight/history.db`
12/// 2. `$HOME/.local/share/linsight/history.db`
13/// 3. `/tmp/linsight-history.db` (last resort — no home dir)
14pub fn history_db_path() -> PathBuf {
15 if let Some(d) = std::env::var_os("XDG_DATA_HOME") {
16 PathBuf::from(d).join("linsight/history.db")
17 } else if let Some(h) = std::env::var_os("HOME") {
18 PathBuf::from(h).join(".local/share/linsight/history.db")
19 } else {
20 PathBuf::from("/tmp/linsight-history.db")
21 }
22}
23
24#[cfg(test)]
25mod tests {
26 use super::*;
27
28 #[test]
29 fn history_db_path_xdg_data_home_wins() {
30 // Temporarily override env vars is not safe in multi-threaded tests,
31 // so just verify the function returns a PathBuf ending in history.db.
32 let p = history_db_path();
33 assert!(p.ends_with("linsight/history.db") || p.to_str().unwrap().ends_with("history.db"));
34 }
35}