Skip to content

Commit

Permalink
refactor(config): rename User to LcodeCofig and move read_config fn t…
Browse files Browse the repository at this point in the history
…o LcodeCofig method
  • Loading branch information
saying121 committed Jul 27, 2024
1 parent bf69008 commit 8dfe34f
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 69 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ bool_to_int_with_if = "warn"

### restriction group
unwrap_used = "warn"
unwrap_in_result = "warn"
# unwrap_in_result = "warn"
semicolon_outside_block = "warn"
verbose_file_reads = "warn"
try_err = "warn"
Expand Down
4 changes: 2 additions & 2 deletions crates/lcode-config/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::{global, keymap::TuiKeyMap};
#[derive(PartialEq, Eq)]
#[derive(Serialize, Deserialize)]
#[derive(Default)]
pub struct User {
pub struct LcodeConfig {
#[serde(skip)]
pub urls: Urls,
#[serde(default)]
Expand Down Expand Up @@ -90,7 +90,7 @@ impl Default for Config {
}
}

impl User {
impl LcodeConfig {
/// "cn" "en"
pub fn new(tongue: Suffix) -> Self {
let config = Config::new(tongue);
Expand Down
112 changes: 56 additions & 56 deletions crates/lcode-config/src/config/read_config.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
use std::fs::{self, write, OpenOptions};

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

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

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

/// the `$ident` need `global_$ident_path` and `user.$ident`
macro_rules! the_configs {
/// the `$ident` need `global_$ident_path` and `user.$ident`
macro_rules! the_configs {
($($conf:ident), *) => {
paste::paste!{
$(
Expand All @@ -31,60 +31,60 @@ pub fn gen_config(suffix: Suffix) -> Result<()> {
}
};
}
the_configs!(config, cookies, langs, keymap);
the_configs!(config, cookies, langs, keymap);

Ok(())
}
Ok(())
}

/// get the user's config
/// please first use `global_user_config()` for get config
#[instrument]
pub fn get_user_conf() -> Result<User> {
let config = fs::read_to_string(&*G_CONFIG_PATH).unwrap_or_else(|err| {
tracing::info!("no config.toml: {err}");
String::new()
});
let mut config: Config = toml::from_str(&config).into_diagnostic()?;
let urls = Urls::new(config.url_suffix);
/// get the user's config
/// please first use `global_user_config()` for get config
pub fn get_user_conf() -> Result<Self> {
let config = fs::read_to_string(&*G_CONFIG_PATH).unwrap_or_else(|err| {
tracing::info!("no config.toml: {err}");
String::new()
});
let mut config: Config = toml::from_str(&config).into_diagnostic()?;
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).unwrap_or_else(|err| {
tracing::info!("no langs.toml: {err}");
String::new()
});
let langs = toml::from_str(&langs).into_diagnostic()?;
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).unwrap_or_else(|err| {
tracing::info!("no langs.toml: {err}");
String::new()
});
let langs = toml::from_str(&langs).into_diagnostic()?;

let cookies = fs::read_to_string(&*G_COOKIES_PATH).unwrap_or_else(|err| {
tracing::info!("no config.toml: {err}");
String::new()
});
let cookies = toml::from_str(&cookies).unwrap_or_default();
let cookies = fs::read_to_string(&*G_COOKIES_PATH).unwrap_or_else(|err| {
tracing::info!("no config.toml: {err}");
String::new()
});
let cookies = toml::from_str(&cookies).unwrap_or_default();

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

let key = fs::read_to_string(&*G_KEYMAP_PATH).unwrap_or_else(|err| {
tracing::info!("no keymap.toml: {err}");
String::new()
});
let key: TuiKeyMap = toml::from_str(&key)
.into_diagnostic()
.context("get keymap failed")?;
user.keymap.add_keymap(key.map_set);
let key = fs::read_to_string(&*G_KEYMAP_PATH).unwrap_or_else(|err| {
tracing::info!("no keymap.toml: {err}");
String::new()
});
let key: TuiKeyMap = toml::from_str(&key)
.into_diagnostic()
.context("get keymap failed")?;
user.keymap.add_keymap(key.map_set);

Ok(user)
Ok(user)
}
}
9 changes: 3 additions & 6 deletions crates/lcode-config/src/global.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use std::{collections::HashMap, fs::create_dir_all, path::PathBuf, sync::LazyLock};

use crate::{
config::{read_config::get_user_conf, User},
theme::Theme,
};
use crate::{config::LcodeConfig, theme::Theme};

pub const G_APP_NAME: &str = "lcode";
pub const LOG_FILE: &str = "lcode.log";
Expand All @@ -27,8 +24,8 @@ pub static G_LOG_PATH: LazyLock<PathBuf> = LazyLock::new(|| {
});

/// global user config
pub static G_USER_CONFIG: LazyLock<User> =
LazyLock::new(|| get_user_conf().expect("get G_USER_CONFIG falied"));
pub static G_USER_CONFIG: LazyLock<LcodeConfig> =
LazyLock::new(|| LcodeConfig::get_user_conf().expect("get G_USER_CONFIG falied"));

/// "~/.cache/lcode/leetcode-<cn/com>.db"
pub static G_DATABASE_PATH: LazyLock<PathBuf> = LazyLock::new(|| {
Expand Down
4 changes: 2 additions & 2 deletions crates/lcode-config/tests/serde_config_work.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use lcode_config::{
config::{read_config, user_nested::Suffix},
config::{user_nested::Suffix, LcodeConfig},
global::G_USER_CONFIG,
};
use miette::Result;

#[test]
fn serde_conf_work() -> Result<()> {
read_config::gen_config(Suffix::Cn)?;
LcodeConfig::gen_config(Suffix::Cn)?;
// let a = read_config::get_user_conf()?;
// println!(r##"(| a |) -> {:#?}"##, a);
// let a = &USER_CONFIG.get_suffix();
Expand Down
4 changes: 2 additions & 2 deletions crates/lcode/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use clap::{Args, Command, CommandFactory, Parser, Subcommand};
use clap_complete::{generate, Generator, Shell};
use colored::Colorize;
use lcode_config::{
config::{read_config, user_nested::Suffix},
config::{user_nested::Suffix, LcodeConfig},
global::G_DATABASE_PATH,
};
use leetcode_api::{leetcode::IdSlug, render::Render};
Expand Down Expand Up @@ -168,7 +168,7 @@ pub async fn run() -> Result<()> {
println!("{}", res);
},
Commands::Gencon(args) => {
read_config::gen_config(if args.cn { Suffix::Cn } else { Suffix::Com })?;
LcodeConfig::gen_config(if args.cn { Suffix::Cn } else { Suffix::Com })?;
},
Commands::Submit(args) => {
let (_, res) = glob_leetcode()
Expand Down

0 comments on commit 8dfe34f

Please sign in to comment.