Skip to main content

hardy_sqlite_storage/
config.rs

1/// Configuration for the SQLite metadata storage backend.
2///
3/// All fields have sensible defaults and can be overridden via TOML config
4/// or environment variables (kebab-case field names).
5#[derive(Debug, serde::Serialize, serde::Deserialize)]
6#[serde(default, rename_all = "kebab-case")]
7pub struct Config {
8    /// Directory in which the database file is stored.
9    ///
10    /// Defaults to the platform-specific cache directory for the project
11    /// (e.g. `~/.cache/sqlite-storage` on Linux), or `/var/spool/<pkg>` on
12    /// Unix when no project directory can be determined.
13    pub db_dir: std::path::PathBuf,
14    /// Filename of the SQLite database. Defaults to `"metadata.db"`.
15    pub db_name: String,
16}
17
18impl Default for Config {
19    fn default() -> Self {
20        Self {
21            db_dir:  directories::ProjectDirs::from("dtn", "Hardy", env!("CARGO_PKG_NAME"))
22            .map_or_else(
23                || {
24                    #[cfg(unix)]
25                    return std::path::Path::new("/var/spool").join(env!("CARGO_PKG_NAME"));
26
27                    #[cfg(windows)]
28                    return std::env::current_exe().expect("Failed to get current executable path").join(env!("CARGO_PKG_NAME"));
29
30                    #[cfg(not(any(unix,windows)))]
31                    compile_error!("No idea how to determine default sqlite metadata store directory for target platform");
32                },
33                |project_dirs| {
34                    project_dirs.cache_dir().into()
35                    // Lin: /home/alice/.store/barapp
36                    // Win: C:\Users\Alice\AppData\Local\Foo Corp\Bar App\store
37                    // Mac: /Users/Alice/Library/stores/com.Foo-Corp.Bar-App
38                },
39            ),
40            db_name: String::from("metadata.db"),
41        }
42    }
43}