lcode_config/config/
read_config.rs

1use std::fs::{self, OpenOptions, write};
2
3use miette::{Context, IntoDiagnostic, Result};
4
5use super::{LcodeConfig, global::*, user_nested::Suffix};
6use crate::{
7    config::{Config, user_nested::Urls},
8    keymap::TuiKeyMap,
9};
10
11impl LcodeConfig {
12    /// generate config
13    pub fn gen_config(suffix: Suffix) -> Result<()> {
14        let user = Self::new(suffix);
15
16        /// the `$ident` need `global_$ident_path` and `user.$ident`
17        macro_rules! the_configs {
18        ($($conf:ident), *) => {
19            paste::paste!{
20                $(
21                    if ![<G_ $conf:upper _PATH>].exists() {
22                        OpenOptions::new()
23                            .create(true)
24                            .write(true)
25                            .open(&*[<G_ $conf:upper _PATH>])
26                            .into_diagnostic()?;
27                        let toml = toml::to_string(&user.$conf).into_diagnostic()?;
28                        write(&*[<G_ $conf:upper _PATH>], toml).into_diagnostic()?;
29                    }
30                )*
31            }
32        };
33    }
34        the_configs!(config, cookies, langs, keymap);
35
36        Ok(())
37    }
38
39    /// get the user's config
40    /// please first use `global_user_config()` for get config
41    pub fn get_user_conf() -> Result<Self> {
42        let config = fs::read_to_string(&*G_CONFIG_PATH).unwrap_or_else(|err| {
43            tracing::info!("no config.toml: {err}");
44            String::new()
45        });
46        let mut config: Config = toml::from_str(&config).into_diagnostic()?;
47        let urls = Urls::new(config.url_suffix);
48
49        if config.code_dir.starts_with("~") {
50            let mut path = config
51                .code_dir
52                .to_string_lossy()
53                .to_string();
54            let path = path.split_off(2);
55            let mut code_dir = dirs::home_dir().expect("get home_dir failed");
56            code_dir.push(path);
57            config.code_dir = code_dir;
58        }
59        let langs = fs::read_to_string(&*G_LANGS_PATH).unwrap_or_else(|err| {
60            tracing::info!("no langs.toml: {err}");
61            String::new()
62        });
63        let langs = toml::from_str(&langs).into_diagnostic()?;
64
65        let cookies = fs::read_to_string(&*G_COOKIES_PATH).unwrap_or_else(|err| {
66            tracing::info!("no config.toml: {err}");
67            String::new()
68        });
69        let cookies = toml::from_str(&cookies).unwrap_or_default();
70
71        let mut user = Self {
72            urls,
73            config,
74            cookies,
75            langs,
76            keymap: TuiKeyMap::default(),
77        };
78
79        let key = fs::read_to_string(&*G_KEYMAP_PATH).unwrap_or_else(|err| {
80            tracing::info!("no keymap.toml: {err}");
81            String::new()
82        });
83        let key: TuiKeyMap = toml::from_str(&key)
84            .into_diagnostic()
85            .context("get keymap failed")?;
86        user.keymap.add_keymap(key.map_set);
87
88        Ok(user)
89    }
90}