use std::fs;
use anyhow::Result;
use email_address::EmailAddress;
use log::info;
use serde_derive::{Deserialize, Serialize};
use toml;
use crate::utils::EmailAddressList;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Cfg {
pub imap: ImapCfg,
pub archive: ArchiveCfg,
pub runtime: RuntimeCfg,
}
impl Cfg {
pub fn load(path: &str) -> Result<Cfg> {
info!("loading configuration from {}...", path);
let cfg_data = fs::read_to_string(path)?;
let cfg: Cfg = toml::from_str(&cfg_data)?;
info!("loaded");
Ok(cfg)
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ImapCfg {
pub host: String,
pub port: u16,
pub username: EmailAddress,
pub password: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ArchiveCfg {
pub letter_dir: String, pub rstdoc_dir: String, pub create_dirs: Option<bool>,
pub git_no_push: Option<bool>,
pub allowed_from_addrs: EmailAddressList,
pub allowed_to_addrs: EmailAddressList,
pub overwrite: Option<bool>, }
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RuntimeCfg {
pub interval: Option<u64>, }
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_cfg_load() {
let _ = Cfg::load("./test_data/config.toml").unwrap();
}
}