-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetcode_api_runner.rs
More file actions
226 lines (196 loc) · 7.71 KB
/
leetcode_api_runner.rs
File metadata and controls
226 lines (196 loc) · 7.71 KB
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
use std::{
io,
path::{
Path,
PathBuf,
},
};
use colored::*;
use leetcoderustapi::{
problem_actions::Problem,
ProgrammingLanguage,
UserApi,
};
use nanohtml2text::html2text;
use crate::{
config::RuntimeConfigSetup,
local_config::LocalConfig,
readme_parser::LeetcodeReadmeParser,
result_formatter::format_test_result,
test_generator::TestGenerator,
utils::*,
};
pub struct LeetcodeApiRunner {
rcs: RuntimeConfigSetup,
api: UserApi,
}
impl LeetcodeApiRunner {
pub async fn new(rcs: &RuntimeConfigSetup) -> Result<Self, io::Error> {
Ok(LeetcodeApiRunner {
rcs: rcs.clone(),
api: UserApi::new(&rcs.config.leetcode_token).await.map_err(
|e| {
io::Error::new(
io::ErrorKind::NotConnected,
format!(
"An error occurred while creating the API client. \
Check your token in your configuration file: {}: \
{}",
rcs.config_file.display(),
e,
),
)
},
)?,
})
}
pub async fn get_problem_info(&self, id: u32) -> io::Result<String> {
let pb = self.api.set_problem_by_id(id).await?;
let title = pb.description()?.name.bold().cyan();
let difficulty = difficulty_color(&pb.difficulty());
let description = html2text(&pb.description()?.content);
Ok(format!("\n#{id} - {difficulty} - {title}\n\n{description}"))
}
/// Fetches the problem name by its ID.
pub async fn get_problem_name(&self, id: u32) -> io::Result<String> {
let pb = self.api.set_problem_by_id(id).await.unwrap();
Ok(pb.description().unwrap().name.clone())
}
/// Fetches the available languages for a given problem ID.
pub async fn get_available_languages(
&self, id: &u32,
) -> io::Result<Vec<String>> {
let problem = self.api.set_problem_by_id(*id).await?;
Ok(problem
.code_snippets()
.expect("No code snippets found.")
.iter()
.map(|snippet| snippet.langSlug.clone())
.collect::<Vec<_>>())
}
pub async fn start_problem(
&self, id: u32, lang: ProgrammingLanguage,
) -> io::Result<(String, PathBuf, Option<String>)> {
let pb = self.api.set_problem_by_id(id).await?;
let pb_desc = pb.description()?;
let pb_name = pb_desc.name.replace(" ", "_");
let md_desc = html2md::parse_html(&pb_desc.content);
let (pb_dir, src_dir, warning) =
self.prepare_problem_dir(id, &pb_name, &lang)?;
let mut starter_code = self.get_starter_code(&lang, &pb)?;
starter_code = inject_default_return_value(&starter_code, &lang);
let test_data = LeetcodeReadmeParser::new(&md_desc).parse()?;
let tests = TestGenerator::new(&starter_code, test_data).run(&lang)?;
let mut file_content = format!("{starter_code}\n\n{tests}");
file_content = prefix_code(&file_content, &lang);
file_content = postfix_code(&file_content, &lang);
write_readme(&pb_dir, id, &pb_name, &md_desc)?;
write_to_file(&src_dir, &get_file_name(&lang), &file_content)?;
// Create local config file
let local_config =
LocalConfig::new(id, pb_name.clone(), language_to_string(&lang));
local_config.write_to_dir(&pb_dir)?;
let success_message = format!(
"{}: {} created at \n{}\nin {}.",
id,
pb_name.green().bold(),
pb_dir.display(),
language_to_string(&lang)
);
Ok((success_message, pb_dir, warning))
}
/// Prepares the problem directory.
fn prepare_problem_dir(
&self, id: u32, pb_name: &str, language: &ProgrammingLanguage,
) -> io::Result<(PathBuf, PathBuf, Option<String>)> {
let leetcode_dir = self.rcs.resolve_leetcode_dir()?;
let problem_dir = leetcode_dir.join(format!("{id}_{pb_name}"));
let src_dir = problem_dir.join("src");
ensure_directory_exists(&problem_dir)?;
ensure_directory_exists(&src_dir)?;
let warning =
self.initialize_language_project(&problem_dir, pb_name, language)?;
Ok((problem_dir, src_dir, warning))
}
/// Generates starter code for the specified programming language.
fn get_starter_code(
&self, language: &ProgrammingLanguage, pb: &Problem,
) -> io::Result<String> {
let str_language = language_to_string(language);
let code_snippets = pb.code_snippets().ok_or_else(|| {
io::Error::new(io::ErrorKind::NotFound, "No code snippets found")
})?;
let starter_code = code_snippets
.iter()
.find(|snippet| snippet.langSlug == str_language)
.map(|snippet| snippet.code.clone())
.ok_or_else(|| {
io::Error::new(
io::ErrorKind::NotFound,
format!(
"No starter code found for language: {str_language}"
),
)
})?;
Ok(starter_code)
}
pub async fn test_response(
&self, id: u32, path_to_file: &String,
) -> io::Result<String> {
let problem_info = self.api.set_problem_by_id(id).await?;
let name = problem_info.description()?.name;
let file_content = std::fs::read_to_string(path_to_file)
.expect("Unable to read the file");
let language = get_language_from_extension(path_to_file);
let _ = run_local_check(path_to_file, &language).await?;
let processed_code = preprocess_code(&file_content, &language);
let test_res =
problem_info.send_test(language, &processed_code).await?;
Ok(format_test_result(id, &name, &test_res))
}
pub async fn submit_response(
&self, id: u32, path_to_file: &String,
) -> io::Result<()> {
let pb = self.api.set_problem_by_id(id).await?;
let file_content = std::fs::read_to_string(path_to_file)
.expect("Unable to read the file");
let language = get_language_from_extension(path_to_file);
let sub_res = pb.send_subm(language, &file_content).await?;
println!("{id}: submit result {sub_res:?}");
Ok(())
}
/// Initializes language-specific project structure.
fn initialize_language_project(
&self, problem_dir: &Path, pb_name: &str,
language: &ProgrammingLanguage,
) -> io::Result<Option<String>> {
use std::process::Command;
let result = match language {
ProgrammingLanguage::Rust => Command::new("cargo")
.args(["init", "--name", pb_name, "--vcs", "none", "--bin"])
.current_dir(problem_dir)
.output(),
ProgrammingLanguage::JavaScript
| ProgrammingLanguage::TypeScript => Command::new("npm")
.args(["init", "-y"])
.current_dir(problem_dir)
.output(),
ProgrammingLanguage::Go => {
let module_name =
format!("leetcode-{}", pb_name.replace("_", "-"));
Command::new("go")
.args(["mod", "init", &module_name])
.current_dir(problem_dir)
.output()
},
_ => return Ok(None),
};
match result {
Ok(output) if !output.status.success() => {
Ok(Some(String::from_utf8(output.stderr).unwrap()))
},
Err(e) => Ok(Some(e.to_string())),
_ => Ok(None),
}
}
}