-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.rs
127 lines (117 loc) · 3.57 KB
/
mod.rs
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
pub mod read_config;
pub mod user_nested;
mod user_serializes;
use std::{collections::VecDeque, path::PathBuf};
use decrypt_cookies::LeetCodeCookies;
use serde::{Deserialize, Serialize};
use user_nested::*;
use self::user_serializes::*;
use crate::{global, keymap::TuiKeyMap};
/// config for user
#[derive(Clone)]
#[derive(Debug)]
#[derive(PartialEq, Eq)]
#[derive(Serialize, Deserialize)]
#[derive(Default)]
pub struct LcodeConfig {
#[serde(skip)]
pub urls: Urls,
#[serde(default)]
pub config: Config,
#[serde(default)]
pub cookies: LeetCodeCookies,
#[serde(default)]
pub langs: SupportLang,
#[serde(default)]
pub keymap: TuiKeyMap,
}
#[derive(Clone)]
#[derive(Debug)]
#[derive(PartialEq, Eq)]
#[derive(Serialize, Deserialize)]
pub struct Config {
#[serde(default)]
pub translate: bool,
#[serde(default, with = "user_serializes")]
pub url_suffix: Suffix,
#[serde(default)]
pub column: usize,
#[serde(default)]
pub num_sublist: u32,
#[serde(default)]
pub page_size: usize,
#[serde(default = "default_editor")]
pub editor: VecDeque<String>,
#[serde(default = "lang_default")]
pub lang: String,
#[serde(default = "default_code_dir")]
pub code_dir: PathBuf,
#[serde(default)]
pub browser: String,
#[serde(default = "cargo_default")]
pub cargo_integr: bool,
#[serde(default = "default_ser_bool")]
pub dir_with_frontend_id: bool, // create qs dir use frontend id
}
impl Config {
pub fn new(suffix: Suffix) -> Self {
let (url_suffix, translate) = match suffix {
Suffix::Cn => (Suffix::Cn, true),
Suffix::Com => (Suffix::Com, false),
};
Self {
translate,
url_suffix,
..Default::default()
}
}
}
impl Default for Config {
fn default() -> Self {
Self {
translate: false,
column: 4,
num_sublist: 16,
page_size: 25,
url_suffix: Suffix::default(),
editor: default_editor(),
lang: "rust".to_owned(),
code_dir: default_code_dir(),
browser: String::new(),
cargo_integr: true,
dir_with_frontend_id: false,
}
}
}
impl LcodeConfig {
/// "cn" "en"
pub fn new(tongue: Suffix) -> Self {
let config = Config::new(tongue);
Self { config, ..Default::default() }
}
/// `start`, `end`, `inject_end`, `inject_end`
pub fn get_lang_info(&self) -> (String, String, String, String) {
macro_rules! return_info_macro {
($($struct_name:ident),*) => {
match self.config.lang.as_str() {
$(
stringify!($struct_name) => self.langs.$struct_name.return_info(),
)*
_ => self.langs.rust.return_info(),
}
};
}
return_info_macro!(
rust, bash, c, cpp, csharp, golang, java, javascript, kotlin, mysql, php, python,
python3, ruby, scala, swift, typescript, racket, erlang, elixir, dart
)
}
/// get code file suffix
pub fn get_suffix(&self) -> &str {
let sp_lang = &global::G_SUPPORT_LANGS;
sp_lang
.get(self.config.lang.as_str())
.copied()
.unwrap_or_default()
}
}