use serde::{Deserialize, Serialize};
use std::fs;
use std::path::PathBuf;
#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct Config {
pub db_path: Option<PathBuf>,
pub default_export_dir: Option<PathBuf>,
}
impl Default for Config {
fn default() -> Self {
let default_db_path = dirs::data_dir()
.unwrap_or_else(|| {
dirs::home_dir()
.unwrap_or_else(|| PathBuf::from("."))
.join(".medi")
})
.join("medi_db");
let default_export_dir = dirs::document_dir().or_else(|| {
dirs::home_dir().map(|mut path| {
path.push("medi_exports");
path
})
});
Config {
db_path: Option::from(default_db_path),
default_export_dir: Option::from(default_export_dir),
}
}
}
pub fn load() -> Result<Config, std::io::Error> {
let config_dir = dirs::config_dir()
.expect("Could not find config directory")
.join("medi");
fs::create_dir_all(&config_dir)?;
let templates_dir = config_dir.join("templates");
fs::create_dir_all(&templates_dir)?;
let example_template_path = templates_dir.join("meeting.md");
if !example_template_path.exists() {
let template_content = r#"
# Meeting: {{ MEETING TITLE }}
**Date:** {{ YYYY-MM-DD }}
**Location:**
**Facilitator:**
**Notetaker:**
---
## Attendees
-
---
## Agenda
1.
2.
---
## Decisions Made
-
---
## Action Items
| Task | Owner | Due Date |
| ---- | ----- | -------- |
| | | |
| | | |
---
## Additional Notes
-
"#;
fs::write(example_template_path, template_content.trim())?;
}
let config_path = config_dir.join("config.toml");
if !config_path.exists() {
let default_config = Config::default();
let toml_string =
toml::to_string_pretty(&default_config).expect("Could not serialize default config");
fs::write(&config_path, toml_string)?;
}
let toml_content = fs::read_to_string(config_path)?;
let config: Config = toml::from_str(&toml_content).expect("Could not deserialize config file");
Ok(config)
}