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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
use toml;
use std::{fs, collections::HashMap};
use serde::{Deserialize, Serialize};
const DEFAULT_CONFIG: &'static str = r#"
# usually you don't wanna change those
[sys]
categories = [
"algorithms",
"database",
"shell"
]
langs = [
"bash",
"c",
"cpp",
"csharp",
"golang",
"java",
"javascript",
"kotlin",
"mysql",
"php",
"python",
"python3",
"ruby",
"rust",
"scala",
"swift"
]
[sys.urls]
base = "https://leetcode.com"
graphql = "https://leetcode.com/graphql"
login = "https://leetcode.com/accounts/login/"
problems = "https://leetcode.com/api/problems/$category/"
problem = "https://leetcode.com/problems/$slug/description/"
test = "https://leetcode.com/problems/$slug/interpret_solution/"
session = "https://leetcode.com/session/"
submit = "https://leetcode.com/problems/$slug/submit/"
submissions = "https://leetcode.com/api/submissions/$slug"
submission = "https://leetcode.com/submissions/detail/$id/"
verify = "https://leetcode.com/submissions/detail/$id/check/"
favorites = "https://leetcode.com/list/api/questions"
favorite_delete = "https://leetcode.com/list/api/questions/$hash/$id"
# but you will want change these
[autologin]
enable = false
retry = 2
[code]
editor = "vim"
lang = "rust"
[file]
show = "${fid}.${slug}"
submission = "${fid}.${slug}.${sid}.${ac}"
[color]
enable = true
theme = "default"
[network]
concurrency = 10
delay = 1
"#;
#[derive(Debug, Deserialize, Serialize)]
pub struct Config {
pub sys: Sys,
pub autologin: AutoLogin,
pub code: Code,
pub file: File,
pub color: Color,
pub network: Network
}
impl Config {
pub fn sync(&self) {
let home = dirs::home_dir().unwrap();
let conf = home.join(".leetcode/conf.toml");
fs::write(conf, toml::ser::to_string_pretty(&self).unwrap()).unwrap();
}
}
#[derive(Debug, Deserialize, Serialize)]
pub struct Sys {
pub categories: [String; 3],
pub langs: [String; 16],
pub urls: HashMap<String, String>,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct Urls {
pub base: String,
pub graphql: String,
pub login: String,
pub problems: String,
pub problem: String,
pub test: String,
pub session: String,
pub submit: String,
pub submissions: String,
pub submission: String,
pub verify: String,
pub favorites: String,
pub favorite_delete: String,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct AutoLogin {
pub enable: bool,
pub retry: i32
}
#[derive(Debug, Deserialize, Serialize)]
pub struct Code {
pub editor: String,
pub lang: String
}
#[derive(Debug, Deserialize, Serialize)]
pub struct File {
pub show: String,
pub submission: String
}
#[derive(Debug, Deserialize, Serialize)]
pub struct Color {
pub enable: bool,
pub theme: String
}
#[derive(Debug, Deserialize, Serialize)]
pub struct Network {
pub concurrency: i32,
pub delay: i32
}
pub fn locate() -> Config {
let conf = root().join("conf.toml");
if !conf.is_file() {
fs::write(&conf, &DEFAULT_CONFIG[1..]).unwrap();
}
let s = fs::read_to_string(&conf).unwrap();
toml::from_str(&s).unwrap()
}
pub fn root() -> std::path::PathBuf {
let dir = dirs::home_dir().unwrap().join(".leetcode");
if !dir.is_dir() {
info!("Generate root dir at {:?}.", &dir);
fs::DirBuilder::new()
.recursive(true)
.create(&dir)
.unwrap();
}
dir
}