Skip to content

Commit

Permalink
Use derive_more to reduce manually implemented code
Browse files Browse the repository at this point in the history
Signed-off-by: Otavio Salvador <otavio@ossystems.com.br>
  • Loading branch information
otavio committed Apr 14, 2020
1 parent fbc78dd commit 11832d5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 27 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Expand Up @@ -17,3 +17,4 @@ travis-ci = { repository = "otavio/easy-process-rs" }
[dependencies]
checked_command = "0.2.2"
cmdline_words_parser = "0.2.0"
derive_more = { version = "0.99.5", default-features = false, features = ["display", "from", "error"] }
37 changes: 10 additions & 27 deletions src/lib.rs
Expand Up @@ -47,7 +47,8 @@
//! [`std::process::Output`]: https://doc.rust-lang.org/std/process/struct.Output.html

use cmdline_words_parser::parse_posix;
use std::{error, fmt, io, process::ExitStatus};
use derive_more::{Display, Error, From};
use std::{io, process::ExitStatus};

#[derive(Debug, Default)]
/// Holds the output for a giving `easy_process::run`
Expand All @@ -58,13 +59,20 @@ pub struct Output {
pub stderr: String,
}

#[derive(Debug)]
/// Error variant for `easy_process::run`.
#[derive(Display, Error, From, Debug)]
pub enum Error {
/// I/O error
#[display(fmt = "unexpected I/O Error: {}", _0)]
Io(io::Error),
/// Process error. It holds two parts: first argument is the exit
/// code and the second is the output (stdout and stderr).
#[display(
fmt = "status: {:?} stdout: {:?} stderr: {:?}",
"_0.code()",
"_1.stdout",
"_1.stderr"
)]
Failure(ExitStatus, Output),
}

Expand All @@ -86,31 +94,6 @@ impl From<checked_command::Error> for Error {
}
}

impl error::Error for Error {
fn description(&self) -> &str {
"Process error"
}

fn cause(&self) -> Option<&dyn error::Error> {
Some(self)
}
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::Io(e) => write!(f, "unexpected I/O Error: {}", e),
Error::Failure(ex, output) => write!(
f,
"status: {:?} stdout: {:?} stderr: {:?}",
ex.code(),
output.stdout,
output.stderr
),
}
}
}

/// Runs the given command
///
/// # Arguments
Expand Down

0 comments on commit 11832d5

Please sign in to comment.