pub trait ProcessRunner {
// Required method
fn run(
&self,
cmd: &CommandSpec,
timeout: Duration,
) -> Result<ProcessOutput, RunnerError>;
}Expand description
Trait for process execution.
Implementations MUST use argv-style APIs only (no shell string evaluation). This trait provides a synchronous interface for process execution.
§Security
All implementations MUST:
- Use
Command::new().args()style APIs only - NOT use shell string evaluation (
sh -c,cmd /C) - Pass arguments as discrete elements, not concatenated strings
§Threading
ProcessRunner is a synchronous interface. Implementations MAY internally
drive an async runtime (e.g., Tokio for timeouts) but MUST NOT expose async
in the public API. This aligns with NFR-ASYNC.
§Example
use xchecker_utils::runner::{ProcessRunner, CommandSpec, ProcessOutput};
use xchecker_utils::error::RunnerError;
use std::time::Duration;
struct SimpleRunner;
impl ProcessRunner for SimpleRunner {
fn run(&self, cmd: &CommandSpec, _timeout: Duration) -> Result<ProcessOutput, RunnerError> {
// Use argv-style execution via CommandSpec::to_command()
let output = cmd.to_command()
.output()
.map_err(|e| RunnerError::NativeExecutionFailed {
reason: e.to_string(),
})?;
Ok(ProcessOutput::new(
output.stdout,
output.stderr,
output.status.code(),
false,
))
}
}
// Usage example
let runner = SimpleRunner;
let cmd = CommandSpec::new("echo").arg("hello");
let result = runner.run(&cmd, Duration::from_secs(30));
assert!(result.is_ok());Required Methods§
Sourcefn run(
&self,
cmd: &CommandSpec,
timeout: Duration,
) -> Result<ProcessOutput, RunnerError>
fn run( &self, cmd: &CommandSpec, timeout: Duration, ) -> Result<ProcessOutput, RunnerError>
Execute a command with the given timeout.
§Arguments
cmd- The command specification to executetimeout- Maximum duration to wait for the process to complete
§Returns
Ok(ProcessOutput)- The process completed (possibly with non-zero exit code)Err(RunnerError::Timeout)- The process timed outErr(RunnerError::*)- Other execution errors
§Security
Implementations MUST use argv-style APIs only. The CommandSpec ensures
arguments are passed as discrete elements, preventing shell injection.