Trait cradle::output::Output [−][src]
pub trait Output: Sized { }
Expand description
All possible return types of run!
, run_output!
or
run_result!
must implement this trait.
This return-type polymorphism makes cradle very flexible.
For example, if you want to capture what a command writes
to stdout
you can do that using StdoutUntrimmed
:
use cradle::prelude::*; let StdoutUntrimmed(output) = run_output!(%"echo foo"); assert_eq!(output, "foo\n");
But if instead you want to capture the command’s ExitStatus
,
you can use Status
:
use cradle::prelude::*; let Status(exit_status) = run_output!("false"); assert_eq!(exit_status.code(), Some(1));
For documentation on what all the possible return types do,
see the documentation for the individual impls of Output
.
Here’s a non-exhaustive list of the more commonly used return types to get you started:
()
: In case you don’t want to capture anything. See alsorun
.- To capture output streams:
StdoutTrimmed
: To capturestdout
, trimmed of whitespace.StdoutUntrimmed
: To capturestdout
untrimmed.Stderr
: To capturestderr
.
Status
: To capture the command’sExitStatus
.
Also, Output
is implemented for tuples.
You can use this to combine multiple return types that implement Output
.
The following code for example retrieves the command’s ExitStatus
and what it writes to stdout
:
use cradle::prelude::*; let (Status(exit_status), StdoutUntrimmed(stdout)) = run_output!(%"echo foo"); assert!(exit_status.success()); assert_eq!(stdout, "foo\n");
Implementations on Foreign Types
Use this when you don’t need any result from the child process.
use cradle::prelude::*; let () = run_output!(%"touch ./foo");
Since run_output!
(and run_result
) use return type polymorphism,
you have to make sure the compiler can figure out which return type you want to use.
In this example that happens through the let () =
.
So you can’t just omit that.
See also run!
for a more convenient way to use ()
as the return type.
impl<A, B, C, D, E> Output for (A, B, C, D, E) where
A: Output,
B: Output,
C: Output,
D: Output,
E: Output,
impl<A, B, C, D, E, F> Output for (A, B, C, D, E, F) where
A: Output,
B: Output,
C: Output,
D: Output,
E: Output,
F: Output,
Using bool
as the return type for run_output!
will return true
if
the command returned successfully, and false
otherwise:
use cradle::prelude::*; if !run_output!(%"which cargo") { panic!("Cargo is not installed!"); }
Also, when using bool
, non-zero exit codes will not result in a panic
or std::result::Result::Err
:
use cradle::prelude::*; let success: bool = run_output!("false"); assert!(!success); let result: Result<bool, cradle::Error> = run_result!("false"); assert!(result.is_ok()); assert_eq!(result.unwrap(), false);
Also see the section about error handling in the module documentation.