Skip to content

Commit

Permalink
Display error results more nicely
Browse files Browse the repository at this point in the history
  • Loading branch information
coolreader18 committed Apr 12, 2021
1 parent 63235c4 commit 22e8cc9
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 13 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ log = "0.4"
env_logger = { version = "0.8", default-features = false }

termcolor = "1.1"
textwrap = { version = "0.11", default-features = false }

[features]
default = ["build-cranelift"]
Expand Down
30 changes: 29 additions & 1 deletion src/display.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use logic::{CallbackInput, Coords, GridMap, ObjDetails, Team, GRID_SIZE};
use logic::{CallbackInput, Coords, GridMap, ObjDetails, ProgramError, Team, GRID_SIZE};
use std::io::{self, Write};
use termcolor::{BufferedStandardStream, Color, ColorSpec, WriteColor};

Expand Down Expand Up @@ -74,3 +74,31 @@ fn team_color(team: Team) -> Color {
Team::Blue => Color::Blue,
}
}

pub fn display_output(output: logic::MainOutput) {
if let Some(w) = output.winner {
println!("Done! {:?} won", w);
} else {
println!("Done! it was a tie");
}
if !output.errors.is_empty() {
println!("Some errors occurred:");
for (team, error) in output.errors {
println!(" {:?}:", team);
display_error(error)
}
}
}

fn display_error(err: ProgramError) {
match err {
ProgramError::InitError(error) => {
let indent = |s| textwrap::indent(s, " ");
println!("{}", indent(&error.summary));
if let Some(details) = error.details {
println!("{}", indent(&details));
}
}
_ => println!(" {:?}", err),
}
}
14 changes: 2 additions & 12 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ async fn try_main() -> anyhow::Result<()> {
runners,
|turn_state| {
if !raw {
turn_cb(turn_state)
display::display_turn(turn_state).expect("printing failed");
}
},
turn_num,
Expand All @@ -360,13 +360,7 @@ async fn try_main() -> anyhow::Result<()> {
let stdout = std::io::stdout();
serde_json::to_writer(stdout.lock(), &output).unwrap();
} else {
if !output.errors.is_empty() {
println!("Errors: {:?}", output.errors)
} else if let Some(w) = output.winner {
println!("Done! {:?} won", w);
} else {
println!("Done! nobody won");
}
display::display_output(output)
}
}
Run::Web {
Expand Down Expand Up @@ -724,7 +718,3 @@ fn parse_published_slug(s: &str) -> Option<(Option<&str>, &str)> {
};
Some(ret)
}

fn turn_cb(turn_state: &logic::CallbackInput) {
display::display_turn(turn_state).expect("printing failed");
}

0 comments on commit 22e8cc9

Please sign in to comment.