Skip to main content

nms_copilot/
paths.rs

1//! Path resolution for NMS Copilot data files.
2
3use std::path::PathBuf;
4
5/// Base directory for NMS Copilot data: `~/.nms-copilot/`.
6pub fn data_dir() -> PathBuf {
7    dirs::home_dir()
8        .expect("Could not determine home directory")
9        .join(".nms-copilot")
10}
11
12/// Path to the history file: `~/.nms-copilot/history.txt`.
13pub fn history_path() -> PathBuf {
14    data_dir().join("history.txt")
15}
16
17/// Path to the config file: `~/.nms-copilot/config.toml`.
18pub fn config_path() -> PathBuf {
19    data_dir().join("config.toml")
20}
21
22/// Path to the cache file: `~/.nms-copilot/galaxy.rkyv`.
23pub fn cache_path() -> PathBuf {
24    data_dir().join("galaxy.rkyv")
25}
26
27/// Ensure the data directory exists.
28pub fn ensure_data_dir() -> std::io::Result<()> {
29    std::fs::create_dir_all(data_dir())
30}
31
32#[cfg(test)]
33mod tests {
34    use super::*;
35
36    #[test]
37    fn test_data_dir_ends_with_nms_copilot() {
38        let dir = data_dir();
39        assert!(dir.ends_with(".nms-copilot"));
40    }
41
42    #[test]
43    fn test_history_path_under_data_dir() {
44        let path = history_path();
45        assert!(path.starts_with(data_dir()));
46        assert_eq!(path.file_name().unwrap(), "history.txt");
47    }
48
49    #[test]
50    fn test_config_path_under_data_dir() {
51        let path = config_path();
52        assert!(path.starts_with(data_dir()));
53        assert_eq!(path.file_name().unwrap(), "config.toml");
54    }
55
56    #[test]
57    fn test_cache_path_under_data_dir() {
58        let path = cache_path();
59        assert!(path.starts_with(data_dir()));
60        assert_eq!(path.file_name().unwrap(), "galaxy.rkyv");
61    }
62
63    #[test]
64    fn test_ensure_data_dir_creates_directory() {
65        ensure_data_dir().unwrap();
66        assert!(data_dir().is_dir());
67    }
68}