personal activity index (bluesky, leaflet, substack)
pai.desertthunder.dev
rss
bluesky
1use pai_core::{PaiError, Result};
2use std::path::PathBuf;
3
4/// Resolves the database file path, with XDG fallback
5///
6/// Priority order:
7/// 1. Explicit path provided via `-d` flag
8/// 2. $XDG_DATA_HOME/pai/pai.db
9/// 3. $HOME/.local/share/pai/pai.db
10pub fn resolve_db_path(explicit_path: Option<PathBuf>) -> Result<PathBuf> {
11 if let Some(path) = explicit_path {
12 return Ok(path);
13 }
14
15 if let Some(data_home) = dirs::data_dir() {
16 return Ok(data_home.join("pai").join("pai.db"));
17 }
18
19 Err(PaiError::Config(
20 "Unable to determine database path: no XDG_DATA_HOME or HOME set".to_string(),
21 ))
22}
23
24/// Resolves the config directory path, with XDG fallback
25///
26/// Priority order:
27/// 1. Explicit directory provided via `-C` flag
28/// 2. $XDG_CONFIG_HOME/pai
29/// 3. $HOME/.config/pai
30pub fn resolve_config_dir(explicit_dir: Option<PathBuf>) -> Result<PathBuf> {
31 if let Some(dir) = explicit_dir {
32 return Ok(dir);
33 }
34
35 if let Some(config_home) = dirs::config_dir() {
36 return Ok(config_home.join("pai"));
37 }
38
39 Err(PaiError::Config(
40 "Unable to determine config directory: no XDG_CONFIG_HOME or HOME set".to_string(),
41 ))
42}
43
44#[cfg(test)]
45mod tests {
46 use super::*;
47 use std::path::Path;
48
49 #[test]
50 fn resolve_db_path_with_explicit() {
51 let explicit = Some(PathBuf::from("/custom/path/db.sqlite"));
52 let result = resolve_db_path(explicit).unwrap();
53 assert_eq!(result, Path::new("/custom/path/db.sqlite"));
54 }
55
56 #[test]
57 fn resolve_db_path_falls_back() {
58 let result = resolve_db_path(None);
59 assert!(result.is_ok());
60
61 let path = result.unwrap();
62 assert!(path.ends_with("pai/pai.db"));
63 }
64
65 #[test]
66 fn resolve_config_dir_with_explicit() {
67 let explicit = Some(PathBuf::from("/custom/config"));
68 let result = resolve_config_dir(explicit).unwrap();
69 assert_eq!(result, Path::new("/custom/config"));
70 }
71
72 #[test]
73 fn resolve_config_dir_falls_back() {
74 let result = resolve_config_dir(None);
75 assert!(result.is_ok());
76
77 let path = result.unwrap();
78 assert!(path.ends_with("pai"));
79 }
80}