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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
pub use self::digit::Digit;
pub use self::file::{code_path, load_script, test_cases_path};
pub use self::filter::{filter, squash};
pub use self::html::HTML;
mod digit {
pub trait Digit<T> {
fn digit(self, d: T) -> String;
}
impl Digit<i32> for i32 {
fn digit(self, d: i32) -> String {
let mut s = self.to_string();
let space = " ".repeat((d as usize) - s.len());
s.push_str(&space);
s
}
}
impl Digit<i32> for String {
fn digit(self, d: i32) -> String {
let mut s = self.clone();
let space = " ".repeat((d as usize) - self.len());
s.push_str(&space);
s
}
}
impl Digit<i32> for &'static str {
fn digit(self, d: i32) -> String {
let mut s = self.to_string();
let space = " ".repeat((d as usize) - self.len());
s.push_str(&space);
s
}
}
}
mod filter {
use crate::cache::models::Problem;
pub fn filter(ps: &mut Vec<Problem>, query: String) {
for p in query.chars() {
match p {
'l' => ps.retain(|x| x.locked),
'L' => ps.retain(|x| !x.locked),
's' => ps.retain(|x| x.starred),
'S' => ps.retain(|x| !x.starred),
'e' => ps.retain(|x| x.level == 1),
'E' => ps.retain(|x| x.level != 1),
'm' => ps.retain(|x| x.level == 2),
'M' => ps.retain(|x| x.level != 2),
'h' => ps.retain(|x| x.level == 3),
'H' => ps.retain(|x| x.level != 3),
'd' => ps.retain(|x| x.status == "ac"),
'D' => ps.retain(|x| x.status != "ac"),
_ => {}
}
}
}
pub fn squash(ps: &mut Vec<Problem>, ids: Vec<String>) -> Result<(), crate::Error> {
use std::collections::HashMap;
let mut map: HashMap<String, bool> = HashMap::new();
ids.iter().for_each(|x| {
map.insert(x.to_string(), true).unwrap_or_default();
});
ps.retain(|x| map.get(&x.id.to_string()).is_some());
Ok(())
}
}
mod html {
use scraper::Html;
pub trait HTML {
fn render(&self) -> String;
}
impl HTML for String {
fn render(&self) -> String {
let rep = self
.replace(r#"</sup>"#, "")
.replace(r#"<sup>"#, "^")
.replace(r#"</sub>"#, "")
.replace(r#"<sub>"#, "_");
let frag = Html::parse_fragment(rep.as_str());
let res = frag
.root_element()
.text()
.fold(String::new(), |acc, e| acc + e);
res
}
}
}
mod file {
pub fn suffix(l: &str) -> Result<&'static str, crate::Error> {
match l {
"bash" => Ok("sh"),
"c" => Ok("c"),
"cpp" => Ok("cpp"),
"csharp" => Ok("cs"),
"golang" => Ok("go"),
"java" => Ok("java"),
"javascript" => Ok("js"),
"kotlin" => Ok("kt"),
"mysql" => Ok("sql"),
"php" => Ok("php"),
"python" => Ok("py"),
"python3" => Ok("py"),
"ruby" => Ok("rb"),
"rust" => Ok("rs"),
"scala" => Ok("scala"),
"swift" => Ok("swift"),
_ => Ok("c"),
}
}
use crate::{cache::models::Problem, Error};
pub fn test_cases_path(problem: &Problem) -> Result<String, Error> {
let conf = crate::cfg::locate()?;
let mut path = format!("{}/{}.tests.dat", conf.storage.code()?, conf.code.pick);
path = path.replace("${fid}", &problem.fid.to_string());
path = path.replace("${slug}", &problem.slug.to_string());
Ok(path)
}
pub fn code_path(problem: &Problem, l: Option<String>) -> Result<String, Error> {
let conf = crate::cfg::locate()?;
let mut lang = conf.code.lang;
if l.is_some() {
lang = l.ok_or(Error::NoneError)?;
}
let mut path = format!(
"{}/{}.{}",
conf.storage.code()?,
conf.code.pick,
suffix(&lang)?,
);
path = path.replace("${fid}", &problem.fid.to_string());
path = path.replace("${slug}", &problem.slug.to_string());
Ok(path)
}
pub fn load_script(module: &str) -> Result<String, crate::Error> {
use std::fs::File;
use std::io::Read;
let conf = crate::cfg::locate()?;
let mut script = "".to_string();
File::open(format!("{}/{}.py", conf.storage.scripts()?, module))?
.read_to_string(&mut script)?;
Ok(script)
}
}