mi6_core/config/storage.rs
1//! Configuration types for storage initialization.
2//!
3//! This module provides config structs that allow components to be initialized
4//! with sensible defaults while still allowing customization when needed.
5
6use std::path::PathBuf;
7
8use super::dir;
9
10/// Configuration for storage backends.
11///
12/// This struct allows components to configure storage without needing to know
13/// implementation details like SQLite. Users provide *what* they want (path, options),
14/// not *how* it's implemented.
15///
16/// # Example
17///
18/// ```
19/// use mi6_core::StorageConfig;
20///
21/// // Use defaults (reads from MI6_DIR_PATH or ~/.mi6)
22/// let config = StorageConfig::default();
23///
24/// // Custom database path
25/// let config = StorageConfig {
26/// db_path: Some("/tmp/test.db".into()),
27/// ..Default::default()
28/// };
29/// ```
30#[derive(Debug, Clone, Default)]
31pub struct StorageConfig {
32 /// Database path. If None, uses the default location.
33 ///
34 /// Resolution order (when calling `resolve_db_path()`):
35 /// 1. This field if set
36 /// 2. Default: `<mi6_dir>/mi6.db`
37 pub db_path: Option<PathBuf>,
38}
39
40impl StorageConfig {
41 /// Create a new storage config with defaults.
42 pub fn new() -> Self {
43 Self::default()
44 }
45
46 /// Create a storage config with a specific database path.
47 pub fn with_path(path: impl Into<PathBuf>) -> Self {
48 Self {
49 db_path: Some(path.into()),
50 }
51 }
52
53 /// Get the effective database path, resolving defaults if needed.
54 ///
55 /// Resolution order:
56 /// 1. `db_path` field if set
57 /// 2. Default: `<mi6_dir>/mi6.db`
58 pub fn resolve_db_path(&self) -> Result<PathBuf, crate::ConfigError> {
59 if let Some(ref path) = self.db_path {
60 return Ok(path.clone());
61 }
62 dir::db_path()
63 }
64}
65
66#[cfg(test)]
67mod tests {
68 use super::*;
69
70 #[test]
71 fn test_storage_config_default() {
72 let config = StorageConfig::default();
73 assert!(config.db_path.is_none());
74 }
75
76 #[test]
77 fn test_storage_config_with_path() {
78 let config = StorageConfig::with_path("/tmp/test.db");
79 assert_eq!(config.db_path, Some(PathBuf::from("/tmp/test.db")));
80 }
81}