|
1 | 1 | use crate::file_parser::codefile::CodeFile; |
2 | | -use crate::leetcode_api::utils::{ExecutionResult, SubmissionResult}; |
| 2 | +use crate::utils::{execute_testcases, submit}; |
3 | 3 | use clap::{Parser, Subcommand}; |
4 | 4 | use colored::Colorize; |
5 | | -use leetcode_api::leetcode::{Authorized, LeetCode}; |
| 5 | +use leetcode_api::leetcode::LeetCode; |
6 | 6 | use std::process::ExitCode; |
7 | 7 |
|
8 | 8 | mod file_parser; |
9 | 9 | mod leetcode_api; |
| 10 | +mod utils; |
10 | 11 |
|
11 | 12 | #[derive(Parser)] |
12 | 13 | #[command(version)] |
@@ -137,7 +138,15 @@ fn main() -> ExitCode { |
137 | 138 | } |
138 | 139 | } |
139 | 140 | Commands::Question { question_name } => { |
140 | | - let question = lc.question_content(&question_name); |
| 141 | + let question_name = if let Some(idx) = question_name.find("leetcode.com/problems/") |
| 142 | + { |
| 143 | + let problem = (&question_name[idx..]).split_whitespace().next().unwrap(); |
| 144 | + let problem = problem.split('/').skip(2).next().unwrap(); |
| 145 | + problem |
| 146 | + } else { |
| 147 | + &question_name |
| 148 | + }; |
| 149 | + let question = lc.question_content(question_name); |
141 | 150 | if question.is_ok() { |
142 | 151 | let question = question.unwrap(); |
143 | 152 | let filename = format!("{}.html", question_name); |
@@ -197,141 +206,3 @@ fn main() -> ExitCode { |
197 | 206 | } |
198 | 207 | } |
199 | 208 | } |
200 | | - |
201 | | -fn execute_testcases( |
202 | | - filename: Option<String>, |
203 | | - testcases: Option<String>, |
204 | | - lc: &LeetCode<Authorized>, |
205 | | -) -> (bool, CodeFile) { |
206 | | - let is_correct; |
207 | | - let code_file: CodeFile; |
208 | | - if let Some(filename) = filename { |
209 | | - code_file = CodeFile::from_file(&filename); |
210 | | - } else { |
211 | | - code_file = CodeFile::from_dir(); |
212 | | - } |
213 | | - if let Some(testcases) = testcases { |
214 | | - let Ok(data_input) = std::fs::read_to_string(&testcases) else{ |
215 | | - eprintln!("Error opening testcases file!"); |
216 | | - return (false, code_file); |
217 | | - }; |
218 | | - match lc.execute(&code_file, data_input) { |
219 | | - Ok(result) => { |
220 | | - println!(); |
221 | | - is_correct = match result { |
222 | | - ExecutionResult::Success(result) => { |
223 | | - result.display(); |
224 | | - result.is_correct() |
225 | | - } |
226 | | - ExecutionResult::LimitExceeded(limit_exceeded) => { |
227 | | - limit_exceeded.display(); |
228 | | - false |
229 | | - } |
230 | | - ExecutionResult::CompileError(compile_error) => { |
231 | | - compile_error.display(); |
232 | | - false |
233 | | - } |
234 | | - ExecutionResult::RuntimeError(runtime_error) => { |
235 | | - runtime_error.display(); |
236 | | - false |
237 | | - } |
238 | | - ExecutionResult::PendingResult(state) => { |
239 | | - println!("{}", state); |
240 | | - false |
241 | | - } |
242 | | - ExecutionResult::Unknown(_) => { |
243 | | - eprintln!("Unknown Error!"); |
244 | | - false |
245 | | - } |
246 | | - } |
247 | | - } |
248 | | - Err(e) => { |
249 | | - eprintln!("Some error occured! {e}"); |
250 | | - is_correct = false; |
251 | | - } |
252 | | - } |
253 | | - } else { |
254 | | - let result = lc.execute_default(&code_file); |
255 | | - println!(); |
256 | | - match result { |
257 | | - Ok(result) => { |
258 | | - is_correct = match result { |
259 | | - ExecutionResult::Success(result) => { |
260 | | - result.display(); |
261 | | - if !result.is_correct() { |
262 | | - println!( |
263 | | - "{}", |
264 | | - "Testcases can be found in testcase.txt".yellow().italic() |
265 | | - ); |
266 | | - } |
267 | | - result.is_correct() |
268 | | - } |
269 | | - ExecutionResult::LimitExceeded(limit_exceeded) => { |
270 | | - limit_exceeded.display(); |
271 | | - false |
272 | | - } |
273 | | - ExecutionResult::CompileError(compile_error) => { |
274 | | - compile_error.display(); |
275 | | - false |
276 | | - } |
277 | | - ExecutionResult::RuntimeError(runtime_error) => { |
278 | | - runtime_error.display(); |
279 | | - false |
280 | | - } |
281 | | - ExecutionResult::PendingResult(state) => { |
282 | | - println!("{}", state); |
283 | | - false |
284 | | - } |
285 | | - ExecutionResult::Unknown(_) => { |
286 | | - eprintln!("Unknown Error!"); |
287 | | - false |
288 | | - } |
289 | | - } |
290 | | - } |
291 | | - Err(e) => { |
292 | | - eprintln!("Some error occured! {e}"); |
293 | | - is_correct = false; |
294 | | - } |
295 | | - } |
296 | | - } |
297 | | - (is_correct, code_file) |
298 | | -} |
299 | | - |
300 | | -fn submit(lc: &LeetCode<Authorized>, code_file: CodeFile) -> ExitCode { |
301 | | - match lc.submit(&code_file) { |
302 | | - Ok(result) => match result { |
303 | | - SubmissionResult::Success(success) => { |
304 | | - success.display(); |
305 | | - ExitCode::SUCCESS |
306 | | - } |
307 | | - SubmissionResult::LimitExceeded(wrong) => { |
308 | | - wrong.display(); |
309 | | - ExitCode::FAILURE |
310 | | - } |
311 | | - SubmissionResult::PendingResult(state) => { |
312 | | - println!("{}", state); |
313 | | - ExitCode::FAILURE |
314 | | - } |
315 | | - SubmissionResult::CompileError(compile_err) => { |
316 | | - compile_err.display(); |
317 | | - ExitCode::FAILURE |
318 | | - } |
319 | | - SubmissionResult::RuntimeError(runtime_error) => { |
320 | | - runtime_error.display(); |
321 | | - ExitCode::FAILURE |
322 | | - } |
323 | | - SubmissionResult::Wrong(wrong) => { |
324 | | - wrong.display(); |
325 | | - ExitCode::FAILURE |
326 | | - } |
327 | | - SubmissionResult::Unknown(_) => { |
328 | | - eprintln!("Unknown Error!"); |
329 | | - ExitCode::FAILURE |
330 | | - } |
331 | | - }, |
332 | | - Err(e) => { |
333 | | - eprintln!("Some error occured! {e}"); |
334 | | - return ExitCode::FAILURE; |
335 | | - } |
336 | | - } |
337 | | -} |
0 commit comments