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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
use std::fs::{self, write, OpenOptions};

use miette::{IntoDiagnostic, Result};
use tracing::{instrument, warn};

use super::{global::*, user_nest::Suffix, User};
use crate::{
    config::{user_nest::Urls, Config},
    keymap::TuiKeyMap,
};

/// generate config
pub fn gen_config(suffix: Suffix) -> Result<()> {
    let user = User::new(suffix);

    /// the `$ident` need `global_$ident_path` and `user.$ident`
    macro_rules! the_configs {
        ($($conf:ident), *) => {
            paste::paste!{
                $(
                    if ![<G_ $conf:upper _PATH>].exists() {
                        OpenOptions::new()
                            .create(true)
                            .write(true)
                            .open(&*[<G_ $conf:upper _PATH>])
                            .into_diagnostic()?;
                        let toml = toml::to_string(&user.$conf).into_diagnostic()?;
                        write(&*[<G_ $conf:upper _PATH>], toml).into_diagnostic()?;
                    }
                )*
            }
        };
    }
    the_configs!(config, cookies, langs, keymap);

    Ok(())
}

/// get the user's config
/// please first use `global_user_config()` for get config
#[instrument]
pub fn get_user_conf() -> Result<User> {
    if !(G_CONFIG_PATH.exists()
        && G_COOKIES_PATH.exists()
        && G_LANGS_PATH.exists()
        && G_KEYMAP_PATH.exists())
    {
        gen_config(Suffix::Com)?;
    }

    let config = fs::read_to_string(&*G_CONFIG_PATH).into_diagnostic()?;
    let mut config: Config = toml::from_str(&config)
        .into_diagnostic()
        .expect(
            "something wrong, you can backup of `config.toml` as `config.toml.bak` for auto \
             generate",
        );
    let urls = Urls::new(config.url_suffix);

    if config.code_dir.starts_with("~") {
        let mut path = config
            .code_dir
            .to_string_lossy()
            .to_string();
        let path = path.split_off(2);
        let mut code_dir = dirs::home_dir().expect("get home_dir failed");
        code_dir.push(path);
        config.code_dir = code_dir;
    }
    let langs = fs::read_to_string(&*G_LANGS_PATH)
        .into_diagnostic()
        .expect("read langs.toml failed");
    let langs = toml::from_str(&langs)
        .into_diagnostic()
        .expect(
            "something wrong, you can backup of `langs.toml` as `langs.toml.bak` for auto generate",
        );

    let cookies = fs::read_to_string(&*G_COOKIES_PATH)
        .into_diagnostic()
        .expect("read cookies.toml failed");
    let cookies = toml::from_str(&cookies)
        .into_diagnostic()
        .expect(
            "something wrong, you can backup of `cookies.toml` as `cookies.toml.bak` for auto \
             generate",
        );

    let mut user = User {
        urls,
        config,
        cookies,
        langs,
        keymap: TuiKeyMap::default(),
    };

    let key = fs::read_to_string(&*G_KEYMAP_PATH)
        .into_diagnostic()
        .expect("read keymap.toml failed");
    let key: TuiKeyMap = toml::from_str(&key)
        .into_diagnostic()
        .unwrap_or_default();
    user.keymap.add_keymap(key.keymap);

    Ok(user)
}