From b78ffc749ca180e8bc7b57d1fbab4c6d631e4a35 Mon Sep 17 00:00:00 2001 From: Jonxslays <51417989+Jonxslays@users.noreply.github.com> Date: Sat, 27 Nov 2021 20:04:26 -0700 Subject: [PATCH] Remove message field from ExecResult Various bug fixes --- README.md | 19 +++++++++++-------- src/client.rs | 17 +++++++++-------- src/executor.rs | 26 +++++++++++++++----------- src/lib.rs | 19 +++++++++++-------- 4 files changed, 46 insertions(+), 35 deletions(-) diff --git a/README.md b/README.md index e402b47..985f1b7 100644 --- a/README.md +++ b/README.md @@ -43,23 +43,26 @@ async fn main() { .add_file( piston_rs::File::default() .set_name("main.rs") - .set_content("fn main() { println!(\"42\"); }"), + .set_content("fn main() { println!(\"42\"); }") ); match client.execute(&executor).await { Ok(response) => { + println!("Language: {}", response.language); + println!("Version: {}", response.version); + if response.is_err() { - println!("{}", response.message.unwrap()); - } else { - println!("Language: {}", response.language); - println!("Version: {}", response.version); - println!("Output:\n{}", response.run.output); + if let Some(c) = response.compile { + println!("Compilation: {}", c.output); + } } - }, + + println!("Output: {}", response.run.output); + } Err(e) => { println!("Something went wrong contacting Piston."); println!("{}", e); - }, + } } } ``` diff --git a/src/client.rs b/src/client.rs index 8db3adb..ab4f89f 100644 --- a/src/client.rs +++ b/src/client.rs @@ -220,20 +220,21 @@ impl Client { Ok(data) => match data.status() { reqwest::StatusCode::OK => Ok(data.json::().await?), _ => { + let text = format!("{}: {}", data.status(), data.text().await?); + let exec_result = ExecResult { - stdout: String::new(), - stderr: String::new(), - output: String::new(), - code: 0, + stdout: text.clone(), + stderr: text.clone(), + output: text, + code: 1, signal: None, }; let exec_response = ExecResponse { - language: String::new(), - version: String::new(), - run: exec_result.clone(), + language: executor.language.clone(), + version: executor.version.clone(), + run: exec_result, compile: None, - message: Some(format!("{}: {}", data.status(), data.text().await?)), }; Ok(exec_response) diff --git a/src/executor.rs b/src/executor.rs index be9ceed..cfed873 100644 --- a/src/executor.rs +++ b/src/executor.rs @@ -18,16 +18,16 @@ pub struct ExecResult { } impl ExecResult { - /// [`true`] if there was no error in the execution result returned - /// by Piston. + /// [`true`] if there was a zero status code returned from + /// execution. pub fn is_ok(&self) -> bool { - self.stderr.is_empty() + self.code == 0 } - /// [`true`] if there was an error in the execution result returned - /// by Piston. + /// [`true`] if there was a non zero status code returned from + /// execution. pub fn is_err(&self) -> bool { - !self.stderr.is_empty() + self.code != 0 } } @@ -43,21 +43,25 @@ pub struct ExecResponse { /// The optional result Piston sends detailing compilation. This /// will be [`None`] for non-compiled languages. pub compile: Option, - /// The error message returned by Piston, if any. - pub message: Option, } impl ExecResponse { /// [`true`] if the response from Piston did not contain an error /// message. pub fn is_ok(&self) -> bool { - self.message.is_none() + match &self.compile { + Some(c) => c.is_ok() && self.run.is_ok(), + None => self.run.is_ok(), + } } /// [`true`] if the response from Piston did contain an error /// message. pub fn is_err(&self) -> bool { - self.message.is_some() + match &self.compile { + Some(c) => c.is_err() || self.run.is_err(), + None => self.run.is_err(), + } } } @@ -515,7 +519,7 @@ mod test_execution_result { #[test] fn test_is_err_with_stdout() { - let result = generate_result("Hello, world", "Error!", 0); + let result = generate_result("Hello, world", "Error!", 1); assert!(!result.is_ok()); assert!(result.is_err()); diff --git a/src/lib.rs b/src/lib.rs index b71c585..3e947d5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -20,23 +20,26 @@ //! .add_file( //! piston_rs::File::default() //! .set_name("main.rs") -//! .set_content("fn main() { println!(\"42\"); }"), +//! .set_content("fn main() { println!(\"42\"); }") //! ); //! //! match client.execute(&executor).await { //! Ok(response) => { +//! println!("Language: {}", response.language); +//! println!("Version: {}", response.version); +//! //! if response.is_err() { -//! println!("{}", response.message.unwrap()); -//! } else { -//! println!("Language: {}", response.language); -//! println!("Version: {}", response.version); -//! println!("Output:\n{}", response.run.output); +//! if let Some(c) = response.compile { +//! println!("Compilation: {}", c.output); +//! } //! } -//! }, +//! +//! println!("Output: {}", response.run.output); +//! } //! Err(e) => { //! println!("Something went wrong contacting Piston."); //! println!("{}", e); -//! }, +//! } //! } //! # } //! ```