~llndqvst/notmuch-mvrs

ref: ae973f7bd1e1c9aa26e4889458f3da67846cbb3c notmuch-mvrs/src/config.rs -rw-r--r-- 1.3 KiB
ae973f7bLukas Lindqvist Add support for actually renaming emails 9 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
use serde::Deserialize;
use std::fs;
use std::path::PathBuf;
use toml;

pub struct ConfigShim {
    pub mover: String,
    pub db: notmuch::Database,
    pub dry_run: bool,
    pub mover_options: Vec<MoverOption>,
}

#[derive(Deserialize)]
struct Config {
    mover: Option<String>,
    db: PathBuf,
    #[serde(default = "bool::default")]
    dry_run: bool,
    mover_options: Option<Vec<MoverOption>>,
}

#[derive(Deserialize)]
pub struct MoverOption {
    pub target_dir: String,
    pub query: String,
}

impl Config {
    fn new(p: PathBuf) -> Self {
        let pc = p.clone();
        let s1: &str = pc.to_str().unwrap();
        let s = match fs::read_to_string(p) {
            Ok(f) => f,
            Err(e) => panic!("{}, {}", e, s1),
        };
        toml::from_str(s.as_str()).unwrap()
    }

    fn get_mover(&self) -> String {
        match &self.mover {
            Some(x) => String::from(x),
            None => String::from(crate::DEFAULT_MOVER),
        }
    }
}

impl ConfigShim {
    pub fn new(p: PathBuf) -> Self {
        let c = Config::new(p);
        ConfigShim {
            mover: c.get_mover(),
            db: notmuch::Database::open(&c.db, notmuch::DatabaseMode::ReadOnly).unwrap(),
            dry_run: c.dry_run,
            mover_options: c.mover_options.unwrap(),
        }
    }
}