Skip to content

Commit

Permalink
Implement Display for Error and use that for errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Hiram van Paassen authored and hmvp committed Jun 15, 2019
1 parent 846e178 commit 61490c1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
22 changes: 22 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use json;
use std::fmt;
use std::io;

#[derive(Debug)]
Expand All @@ -11,3 +12,24 @@ pub enum Error {
NotACargoFolder,
Syntax(String),
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Error::CargoExecutionFailed(error) => {
write!(f, "Failed to run `cargo` command.\n{:?}", error)
}
Error::InvalidManifestJson(error) => {
write!(f, "Failed to parse JSON response.\n{:?}", error)
}
Error::NoLibraryTargetFound => write!(f, "No library target found."),
Error::NoMatchingBinaryTargetFound => write!(f, "No matching binary target found."),
Error::NoTargetProvided => write!(f, "Please specify a target to process."),
Error::NotACargoFolder => write!(
f,
"could not find `Cargo.toml` in `/home/hiram/git` or any parent directory"
),
Error::Syntax(error) => write!(f, "Failed to parse: {}", error),
}
}
}
20 changes: 1 addition & 19 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,24 +224,6 @@ fn main() {
let arguments = Arguments::from_args();

if let Err(error) = run(&arguments) {
let error_string = match error {
Error::CargoExecutionFailed(error) => {
format!("Error: Failed to run `cargo` command.\n{:?}", error)
}
Error::InvalidManifestJson(error) => {
format!("Error: Failed to parse JSON response.\n{:?}", error)
}
Error::NoLibraryTargetFound => "Error: No library target found.".to_string(),
Error::NoMatchingBinaryTargetFound => {
"Error: No matching binary target found.".to_string()
}
Error::NoTargetProvided => "Error: Please specify a target to process.".to_string(),
Error::NotACargoFolder => {
"Error: could not find `Cargo.toml` in `/home/hiram/git` or any parent directory"
.to_string()
}
Error::Syntax(error) => format!("Error: Failed to parse: {}", error),
};
println!("{}", error_string.red());
println!("{} {}", "error:".red(), error);
}
}

0 comments on commit 61490c1

Please sign in to comment.