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