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 no timeout, inherited working directory, success exit code 0, strict UTF-8 output text accessors, unlimited in-memory output capture, and no output tee files.

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 max_stdout_bytes(self, max_bytes: usize) -> Self

Sets the maximum stdout bytes retained in memory.

The reader still drains the complete stdout stream. Bytes beyond this limit are not retained in CommandOutput, but they are still written to a configured stdout tee file.

§Parameters
  • max_bytes - Maximum number of stdout bytes to retain.
§Returns

The updated command runner.

Source

pub const fn max_stderr_bytes(self, max_bytes: usize) -> Self

Sets the maximum stderr bytes retained in memory.

The reader still drains the complete stderr stream. Bytes beyond this limit are not retained in CommandOutput, but they are still written to a configured stderr tee file.

§Parameters
  • max_bytes - Maximum number of stderr bytes to retain.
§Returns

The updated command runner.

Source

pub const fn max_output_bytes(self, max_bytes: usize) -> Self

Sets the same in-memory capture limit for stdout and stderr.

§Parameters
  • max_bytes - Maximum number of bytes retained for each stream.
§Returns

The updated command runner.

Source

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

Streams stdout to a file while still capturing it in memory.

The file is created or truncated before the command is spawned. Combine this with Self::max_stdout_bytes to avoid unbounded memory use for large stdout streams.

§Parameters
  • path - Destination file path for stdout bytes.
§Returns

The updated command runner.

Source

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

Streams stderr to a file while still capturing it in memory.

The file is created or truncated before the command is spawned. Combine this with Self::max_stderr_bytes to avoid unbounded memory use for large stderr streams.

§Parameters
  • path - Destination file path for stderr bytes.
§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 const fn configured_max_stdout_bytes(&self) -> Option<usize>

Returns the configured stdout capture limit.

§Returns

Some(max_bytes) when stdout capture is limited, otherwise None.

Source

pub const fn configured_max_stderr_bytes(&self) -> Option<usize>

Returns the configured stderr capture limit.

§Returns

Some(max_bytes) when stderr capture is limited, otherwise None.

Source

pub fn configured_stdout_file(&self) -> Option<&Path>

Returns the stdout tee file path.

§Returns

Some(path) when stdout is streamed to a file, otherwise None.

Source

pub fn configured_stderr_file(&self) -> Option<&Path>

Returns the stderr tee file path.

§Returns

Some(path) when stderr is streamed to a file, otherwise None.

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. When a timeout is configured, Unix children run as leaders of new process groups and Windows children run in Job Objects. This lets timeout killing target the process tree instead of only the direct child process. Without a configured timeout, commands use the platform’s normal process-spawning behavior.

Captured output is retained as raw bytes up to the configured per-stream limits. Reader threads still drain complete streams so the child is not blocked on full pipes. If lossy output mode is enabled, invalid UTF-8 is replaced only for CommandOutput::stdout and CommandOutput::stderr; byte accessors still return the retained raw 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 written to a tee file, cannot receive configured stdin, 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 exit-code policy.

§Returns

A runner with no timeout, inherited working directory, success exit code 0, strict UTF-8 output text accessors, unlimited in-memory output capture, and no output tee files.

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<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
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.