gglib_core/paths/
database.rs

1//! Database path resolution.
2//!
3//! Provides the canonical path to the gglib `SQLite` database file.
4
5use std::fs;
6use std::path::PathBuf;
7
8use super::error::PathError;
9use super::platform::data_root;
10
11/// Get the path to the gglib database file.
12///
13/// Returns the path to `gglib.db` in the user data directory.
14/// This is shared between dev and release builds.
15///
16/// The `data/` subdirectory is created if it doesn't exist.
17pub fn database_path() -> Result<PathBuf, PathError> {
18    let data_dir = data_root()?.join("data");
19
20    fs::create_dir_all(&data_dir).map_err(|e| PathError::CreateFailed {
21        path: data_dir.clone(),
22        reason: e.to_string(),
23    })?;
24
25    Ok(data_dir.join("gglib.db"))
26}
27
28#[cfg(test)]
29mod tests {
30    use super::*;
31
32    #[test]
33    fn test_database_path_ends_with_gglib_db() {
34        let result = database_path();
35        assert!(result.is_ok());
36        let path = result.unwrap();
37        assert!(path.to_string_lossy().ends_with("gglib.db"));
38    }
39}