I originally wrote this trait as part of the zebra-test^1 crate to abstract over the act of adding context from an external command to an error report in our various helper methods. I particularly like this pattern am curious to see if it applies cleanly to many other types during error reporting and want to add it to color-eyre proper so users of this crate can potentially implement it on their types.
TODO
[ ] add tests
[ ] Add more detailed documentation to the ContextFrom trait, including an example of implementing it for a user's type
[ ] Add an example to the lib.rs docs and readme showing the usage with std::process::Command.
Output
The following example:
fn visit_the_shell() -> eyre::Result<()> {
let mut cmd = std::process::Command::new("bash");
cmd.arg("-c")
// uh oh, there's an extra ' in that string and bash isn't gonna like it!
.arg("echo 'Hello bash, I hope you're doing well!'");
let output = cmd.output().context_from(&cmd)?;
if !output.status.success() {
Err(eyre::eyre!("invalid bash command"))
.context_from(&cmd)
.context_from(&output)
} else {
// I know the command is invalid ;)
Ok(())
}
}
I originally wrote this trait as part of the
zebra-test
^1 crate to abstract over the act of adding context from an external command to an error report in our various helper methods. I particularly like this pattern am curious to see if it applies cleanly to many other types during error reporting and want to add it tocolor-eyre
proper so users of this crate can potentially implement it on their types.TODO
std::process::Command
.Output
The following example:
Produces the following output: