Skip to main content

CommandRunner

Struct CommandRunner 

Source
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

Source

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.

Source

pub const fn timeout(self, timeout: Duration) -> Self

Sets the command timeout.

§Parameters
  • timeout - Maximum duration allowed for each command.
§Returns

The updated command runner.

Source

pub const fn without_timeout(self) -> Self

Disables timeout handling.

§Returns

The updated command runner.

Source

pub fn working_directory<P>(self, working_directory: P) -> Self
where P: Into<PathBuf>,

Sets the default working directory.

§Parameters
  • working_directory - Directory used when a command has no per-command working directory override.
§Returns

The updated command runner.

Source

pub fn success_exit_code(self, exit_code: i32) -> Self

Sets the only exit code treated as successful.

§Parameters
  • exit_code - Exit code considered successful.
§Returns

The updated command runner.

Source

pub fn success_exit_codes(self, exit_codes: &[i32]) -> Self

Sets all exit codes treated as successful.

§Parameters
  • exit_codes - Exit codes considered successful.
§Returns

The updated command runner.

Source

pub const fn disable_logging(self, disable_logging: bool) -> Self

Enables or disables command execution logs.

§Parameters
  • disable_logging - true to suppress runner logs.
§Returns

The updated command runner.

Source

pub const fn lossy_output(self, lossy_output: bool) -> Self

Configures whether output text accessors use lossy UTF-8 conversion.

§Parameters
§Returns

The updated command runner.

Source

pub const fn configured_timeout(&self) -> Option<Duration>

Returns the configured timeout.

§Returns

Some(duration) when timeout handling is enabled, otherwise None.

Source

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.

Source

pub fn configured_success_exit_codes(&self) -> &[i32]

Returns the configured successful exit codes.

§Returns

Borrowed list of exit codes treated as successful.

Source

pub const fn is_logging_disabled(&self) -> bool

Returns whether logging is disabled.

§Returns

true when runner logs are disabled.

Source

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.

Source

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

Source§

fn clone(&self) -> CommandRunner

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for CommandRunner

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for CommandRunner

Source§

fn default() -> Self

Creates a command runner with the default timeout and exit-code policy.

§Returns

A runner with a 10-second timeout, inherited working directory, success exit code 0, and strict UTF-8 output text accessors.

Source§

impl PartialEq for CommandRunner

Source§

fn eq(&self, other: &CommandRunner) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for CommandRunner

Source§

impl StructuralPartialEq for CommandRunner

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.