pub struct CommandRunner { /* private fields */ }Expand description
Runs external commands and captures their output.
CommandRunner runs one Command synchronously on the caller thread and
returns captured process output. The runner always preserves raw output
bytes. Its lossy-output option controls whether CommandOutput::stdout
and CommandOutput::stderr reject invalid UTF-8 or return replacement
characters.
§Author
Haixing Hu
Implementations§
Source§impl CommandRunner
impl CommandRunner
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a command runner with default settings.
§Returns
A runner with a 10-second timeout, inherited working directory, success
exit code 0, and strict UTF-8 output text accessors.
Sourcepub const fn without_timeout(self) -> Self
pub const fn without_timeout(self) -> Self
Sourcepub fn working_directory<P>(self, working_directory: P) -> Self
pub fn working_directory<P>(self, working_directory: P) -> Self
Sourcepub fn success_exit_code(self, exit_code: i32) -> Self
pub fn success_exit_code(self, exit_code: i32) -> Self
Sourcepub fn success_exit_codes(self, exit_codes: &[i32]) -> Self
pub fn success_exit_codes(self, exit_codes: &[i32]) -> Self
Sourcepub const fn disable_logging(self, disable_logging: bool) -> Self
pub const fn disable_logging(self, disable_logging: bool) -> Self
Sourcepub const fn lossy_output(self, lossy_output: bool) -> Self
pub const fn lossy_output(self, lossy_output: bool) -> Self
Configures whether output text accessors use lossy UTF-8 conversion.
§Parameters
lossy_output-trueto replace invalid UTF-8 bytes with the Unicode replacement character whenCommandOutput::stdoutorCommandOutput::stderris called.
§Returns
The updated command runner.
Sourcepub const fn configured_timeout(&self) -> Option<Duration>
pub const fn configured_timeout(&self) -> Option<Duration>
Returns the configured timeout.
§Returns
Some(duration) when timeout handling is enabled, otherwise None.
Sourcepub fn configured_working_directory(&self) -> Option<&Path>
pub fn configured_working_directory(&self) -> Option<&Path>
Returns the default working directory.
§Returns
Some(path) when a default working directory is configured, otherwise
None to inherit the current process working directory.
Sourcepub fn configured_success_exit_codes(&self) -> &[i32]
pub fn configured_success_exit_codes(&self) -> &[i32]
Returns the configured successful exit codes.
§Returns
Borrowed list of exit codes treated as successful.
Sourcepub const fn is_logging_disabled(&self) -> bool
pub const fn is_logging_disabled(&self) -> bool
Sourcepub const fn is_lossy_output_enabled(&self) -> bool
pub const fn is_lossy_output_enabled(&self) -> bool
Returns whether output text accessors use lossy UTF-8 conversion.
§Returns
true when invalid UTF-8 bytes are replaced before output is returned
by CommandOutput::stdout or CommandOutput::stderr.
Sourcepub fn run(&self, command: Command) -> Result<CommandOutput, CommandError>
pub fn run(&self, command: Command) -> Result<CommandOutput, CommandError>
Runs a command and captures stdout and stderr.
This method blocks the caller thread until the child process exits or
the configured timeout is reached. Captured output is always retained
as raw bytes. If lossy output mode is enabled, invalid UTF-8 is replaced
only for CommandOutput::stdout and CommandOutput::stderr; byte
accessors still return the original process output.
§Parameters
command- Structured command to run.
§Returns
Captured output when the process exits with a configured success code.
§Errors
Returns CommandError if the process cannot be spawned, cannot be
waited on, times out, cannot be killed after timing out, emits output
that cannot be read, or exits with a code not configured as successful.
Trait Implementations§
Source§impl Clone for CommandRunner
impl Clone for CommandRunner
Source§fn clone(&self) -> CommandRunner
fn clone(&self) -> CommandRunner
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more