Skip to content

Commit

Permalink
Implement custom Debug for cli::Error
Browse files Browse the repository at this point in the history
These errors are pretty much user facing only, and are expected to be
propagated to main(). Implementing a custom Debug trait takes advantage
of the runtime to print the error as a user friendly message when the
program exits on error.
  • Loading branch information
eyeplum committed Oct 13, 2020
1 parent 33d3043 commit a783a3a
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,25 @@ pub const FLAG_NAME_CODE_POINT_INPUT_MODE: &str = "code_point_input_mode";

pub type Result<T> = std::result::Result<T, Box<dyn error::Error>>;

#[derive(Debug)]
pub enum Error {
UnrecognizedInputType(String),
UnrecognizedOutputFormat(String),
}

impl fmt::Debug for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self)
}
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::UnrecognizedInputType(input_type) => {
write!(f, "Unrecognized input type: '{}'", input_type)
write!(f, "Unrecognized input type '{}'", input_type)
}
Error::UnrecognizedOutputFormat(output_format) => {
write!(f, "Unrecognized output format: '{}'", output_format)
write!(f, "Unrecognized output format '{}'", output_format)
}
}
}
Expand Down

0 comments on commit a783a3a

Please sign in to comment.