Skip to content

Commit

Permalink
Remove message field from ExecResult
Browse files Browse the repository at this point in the history
Various bug fixes
  • Loading branch information
Jonxslays committed Nov 28, 2021
1 parent 66a0741 commit b78ffc7
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 35 deletions.
19 changes: 11 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
},
}
}
}
```
Expand Down
17 changes: 9 additions & 8 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,20 +220,21 @@ impl Client {
Ok(data) => match data.status() {
reqwest::StatusCode::OK => Ok(data.json::<ExecResponse>().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)
Expand Down
26 changes: 15 additions & 11 deletions src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand All @@ -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<ExecResult>,
/// The error message returned by Piston, if any.
pub message: Option<String>,
}

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(),
}
}
}

Expand Down Expand Up @@ -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());
Expand Down
19 changes: 11 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
//! },
//! }
//! }
//! # }
//! ```
Expand Down

0 comments on commit b78ffc7

Please sign in to comment.