Skip to main content

shipd_core/
config.rs

1use serde::Deserialize;
2use std::fs;
3use std::path::PathBuf;
4
5#[derive(Debug, Default, Deserialize)]
6pub struct Config {
7    #[serde(default)]
8    pub database: DatabaseConfig,
9
10    #[serde(default)]
11    pub theme: String,
12}
13
14#[derive(Debug, Deserialize)]
15pub struct DatabaseConfig {
16    #[serde(default = "default_db_path")]
17    pub path: String,
18}
19
20fn default_db_path() -> String {
21    let mut path = dirs::data_dir().unwrap_or_else(|| PathBuf::from("."));
22    path.push("shipd");
23    path.push("shipd.db");
24    path.to_string_lossy().to_string()
25}
26
27impl Default for DatabaseConfig {
28    fn default() -> Self {
29        DatabaseConfig {
30            path: default_db_path(),
31        }
32    }
33}
34
35pub fn load_config() -> Config {
36    let config_path = dirs::config_dir()
37        .unwrap_or_else(|| PathBuf::from("."))
38        .join("shipd")
39        .join("config.toml");
40
41    if let Ok(content) = fs::read_to_string(&config_path) {
42        toml::from_str(&content).unwrap_or_else(|_| Config::default())
43    } else {
44        Config::default()
45    }
46}