Skip to content

Commit 478ebdd

Browse files
authored
Improve printer for execution results (#78)
* Refactor result printer * Move printer files to a printer mod * Refactor * Add decorator * Implement submit result printer
1 parent 48fc49a commit 478ebdd

File tree

10 files changed

+771
-219
lines changed

10 files changed

+771
-219
lines changed

src/config.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,17 @@ pub enum Either {
6868
String(String),
6969
}
7070

71+
impl From<String> for Either {
72+
fn from(s: String) -> Self {
73+
let split: Vec<String> = s.trim().split("\n").map(|st| st.to_owned()).collect();
74+
if split.is_empty() {
75+
Either::String(s.to_owned())
76+
} else {
77+
Either::Sequence(split)
78+
}
79+
}
80+
}
81+
7182
impl ToString for Either {
7283
fn to_string(&self) -> String {
7384
match self {

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ pub use error::{LeetUpError, Result};
44
pub mod cmd;
55
mod config;
66
mod error;
7+
mod printer;
78

89
pub(crate) mod client;
910
pub(crate) mod icon;

src/model/mod.rs

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -206,10 +206,18 @@ pub struct CodeDefinition {
206206
}
207207

208208
#[derive(Deserialize, Debug)]
209-
pub struct SubmissionResult {
209+
pub struct SubmissionResponse {
210+
pub state: Option<String>,
211+
pub input: Option<Either>,
212+
pub input_formatted: Option<Either>,
210213
pub code_output: Option<Either>,
214+
pub std_output: Option<Either>,
215+
pub last_test_case: Option<Either>,
216+
pub correct_answer: Option<bool>,
211217
pub code_answer: Option<Either>,
218+
pub expected_output: Option<Either>,
212219
pub expected_code_output: Option<Either>,
220+
pub expected_answer: Option<Either>,
213221
pub expected_code_answer: Option<Either>,
214222
pub compare_result: Option<String>,
215223
pub compile_error: Option<String>,
@@ -218,7 +226,6 @@ pub struct SubmissionResult {
218226
pub memory: Option<u32>,
219227
pub memory_percentile: Option<f32>,
220228
pub pretty_lang: String,
221-
pub question_id: Option<u32>,
222229
pub run_success: bool,
223230
pub runtime_percentile: Option<f32>,
224231
pub expected_status_code: Option<u32>,
@@ -230,16 +237,31 @@ pub struct SubmissionResult {
230237
pub total_testcases: Option<u32>,
231238
}
232239

233-
impl SubmissionResult {
234-
pub fn has_compile_error(&self) -> bool {
240+
pub trait ExecutionErrorResponse {
241+
fn has_compile_error(&self) -> bool;
242+
243+
fn has_runtime_error(&self) -> bool;
244+
245+
fn has_error(&self) -> bool;
246+
247+
fn is_error(&self) -> bool {
248+
self.has_compile_error() || self.has_runtime_error() || self.has_error()
249+
}
250+
}
251+
252+
impl ExecutionErrorResponse for SubmissionResponse {
253+
fn has_compile_error(&self) -> bool {
235254
self.compile_error.is_some() || self.full_compile_error.is_some()
236255
}
237256

238-
pub fn has_runtime_error(&self) -> bool {
239-
self.status_msg.to_lowercase().contains("error")
257+
fn has_runtime_error(&self) -> bool {
258+
let tle = "time limit exceeded";
259+
let msg = self.status_msg.to_lowercase();
260+
261+
msg.eq(tle) || msg.contains("error")
240262
}
241263

242-
pub fn has_error(&self) -> bool {
264+
fn has_error(&self) -> bool {
243265
self.total_correct.lt(&self.total_testcases)
244266
}
245267
}

src/printer/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
mod printer;
2+
mod submit_execution_printer;
3+
mod test_execution_printer;
4+
5+
pub use printer::*;
6+
pub use submit_execution_printer::SubmitExecutionResult;
7+
pub use test_execution_printer::TestExecutionResult;

src/printer/printer.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
use crate::model::SubmissionResponse;
2+
3+
pub(crate) const NEW_LINE: &'static str = "\n";
4+
pub(crate) const TEXT_BOLD_ON: &'static str = "\x1b[1m";
5+
pub(crate) const TEXT_BOLD_OFF: &'static str = "\x1b[m";
6+
7+
pub trait Printer {
8+
fn print(&self) {
9+
print!("{}", self.buffer());
10+
}
11+
12+
fn is_error(&self) -> bool;
13+
14+
fn buffer(&self) -> String;
15+
16+
fn total_cases_ratio_buffer(&self, response: &SubmissionResponse) -> String {
17+
format!(
18+
"{}/{}",
19+
response.total_correct.unwrap_or(0),
20+
response.total_testcases.unwrap_or(0)
21+
)
22+
}
23+
}
24+
25+
pub mod decorator {
26+
use super::*;
27+
28+
pub fn bold_text(s: &str) -> String {
29+
format!("{}{}{}", s, TEXT_BOLD_ON, TEXT_BOLD_OFF)
30+
}
31+
}

0 commit comments

Comments
 (0)