char_coal/app/
config.rs

1use super::cli::{QueryArgs, Toggle};
2use serde::{Deserialize, Serialize};
3use std::{fs, io, path::PathBuf};
4
5#[derive(Serialize, Deserialize)]
6pub struct Config {
7    #[serde(skip)]
8    pub path: PathBuf,
9    pub main_mode: MainMode,
10    pub speak: bool,
11    #[serde(rename = "Normal")]
12    pub normal: Normal,
13}
14
15#[derive(Serialize, Deserialize)]
16pub enum MainMode {
17    Normal,
18    // Collins,
19    // Both,
20}
21
22#[derive(Serialize, Deserialize)]
23pub struct Normal {
24    pub with_pronunciation: bool,
25    pub with_variants: bool,
26    pub with_sentence: bool,
27}
28
29impl Config {
30    pub fn new(path: PathBuf) -> Self {
31        Config {
32            path,
33            main_mode: MainMode::Normal,
34            normal: Normal {
35                with_pronunciation: true,
36                with_variants: true,
37                with_sentence: true,
38            },
39            speak: false,
40        }
41    }
42    pub fn of_file(path: PathBuf) -> io::Result<Self> {
43        let content = fs::read_to_string(&path)?;
44        let config = toml::from_str(&content)?;
45        Ok(Self { path, ..config })
46    }
47    pub fn to_file(&self) -> anyhow::Result<()> {
48        let s = toml::to_string_pretty(&self)?;
49        fs::write(&self.path, s)?;
50        Ok(())
51    }
52    pub fn apply(&mut self, args: &mut QueryArgs) {
53        if args.speak {
54            args.speak_as = Some(Toggle::True);
55        }
56        if args.mute {
57            args.speak_as = Some(Toggle::False);
58        }
59        if let Some(speak_as) = &args.speak_as {
60            speak_as.twitch(&mut self.speak);
61        }
62
63        if args.concise {
64            args.concise_as = Some(Toggle::True);
65        }
66        if let Some(concise_as) = &args.concise_as {
67            concise_as.counter_twitch(&mut self.normal.with_sentence);
68            concise_as.counter_twitch(&mut self.normal.with_variants);
69        }
70    }
71}