pub struct TaskConfig {
pub command: String,
pub args: Option<Vec<String>>,
pub working_dir: Option<String>,
pub env: Option<HashMap<String, String>>,
pub timeout_ms: Option<u64>,
pub enable_stdin: Option<bool>,
pub ready_indicator: Option<String>,
pub ready_indicator_source: Option<StreamSource>,
}Expand description
Configuration for a task to be executed.
TaskConfig defines all parameters needed to execute a system process securely.
It includes the command, arguments, environment setup, timeouts, and monitoring options.
All fields are validated before execution to prevent:
- Path traversal
- Null byte
§Examples
§Basic Command
use tcrm_task::tasks::config::TaskConfig;
let config = TaskConfig::new("cmd")
.args(["/C", "dir", "C:\\"]);§Complex Configuration
use tcrm_task::tasks::config::{TaskConfig, StreamSource};
use std::collections::HashMap;
let mut env = HashMap::new();
env.insert("PATH".to_string(), "C:\\Windows\\System32".to_string());
let config = TaskConfig::new("cmd")
.args(["/C", "echo", "Server started"])
.working_dir("C:\\")
.env(env)
.timeout_ms(30000)
.enable_stdin(true)
.ready_indicator("Server started")
.ready_indicator_source(StreamSource::Stdout);Fields§
§command: StringThe command or executable to run
args: Option<Vec<String>>Arguments to pass to the command
working_dir: Option<String>Working directory for the command
env: Option<HashMap<String, String>>Environment variables for the command
timeout_ms: Option<u64>Maximum allowed runtime in milliseconds
enable_stdin: Option<bool>Allow providing input to the task via stdin
ready_indicator: Option<String>Optional string to indicate the task is ready (for long-running processes like servers)
ready_indicator_source: Option<StreamSource>Source of the ready indicator string (stdout/stderr)
Implementations§
Source§impl TaskConfig
impl TaskConfig
Sourcepub fn working_dir(self, dir: impl Into<String>) -> Self
pub fn working_dir(self, dir: impl Into<String>) -> Self
Set the working directory for the command
The working directory must exist when the task is executed.
§Arguments
dir- Path to the working directory
§Examples
use tcrm_task::tasks::config::TaskConfig;
let config = TaskConfig::new("ls")
.working_dir("/tmp");
let config2 = TaskConfig::new("cargo")
.working_dir("/path/to/project");Sourcepub fn env<K, V, I>(self, env: I) -> Self
pub fn env<K, V, I>(self, env: I) -> Self
Set environment variables for the command
§Arguments
env- Iterator of (key, value) pairs for environment variables
§Examples
use tcrm_task::tasks::config::TaskConfig;
use std::collections::HashMap;
// Using tuples
let config = TaskConfig::new("node")
.env([("NODE_ENV", "production"), ("PORT", "3000")]);
// Using HashMap
let mut env = HashMap::new();
env.insert("RUST_LOG".to_string(), "debug".to_string());
let config2 = TaskConfig::new("cargo")
.env(env);Sourcepub fn timeout_ms(self, timeout: u64) -> Self
pub fn timeout_ms(self, timeout: u64) -> Self
Set the maximum allowed runtime in milliseconds
If the task runs longer than this timeout, it will be terminated.
§Arguments
timeout- Timeout in milliseconds (must be > 0)
§Examples
use tcrm_task::tasks::config::TaskConfig;
// 30 second timeout
let config = TaskConfig::new("long-running-task")
.timeout_ms(30000);
// 5 minute timeout
let config2 = TaskConfig::new("build-script")
.timeout_ms(300000);Sourcepub fn enable_stdin(self, b: bool) -> Self
pub fn enable_stdin(self, b: bool) -> Self
Enable or disable stdin for the task
When enabled, you can send input to the process via the stdin channel.
§Arguments
b- Whether to enable stdin input
§Examples
use tcrm_task::tasks::config::TaskConfig;
// Interactive command that needs input
let config = TaskConfig::new("python")
.args(["-i"])
.enable_stdin(true);Sourcepub fn ready_indicator(self, indicator: impl Into<String>) -> Self
pub fn ready_indicator(self, indicator: impl Into<String>) -> Self
Set the ready indicator for the task
For long-running processes (like servers), this string indicates when the process is ready to accept requests. When this string appears in the process output, a Ready event will be emitted.
§Arguments
indicator- String to look for in process output
§Examples
use tcrm_task::tasks::config::TaskConfig;
let config = TaskConfig::new("my-server")
.ready_indicator("Server listening on port");
let config2 = TaskConfig::new("database")
.ready_indicator("Database ready for connections");Sourcepub fn ready_indicator_source(self, source: StreamSource) -> Self
pub fn ready_indicator_source(self, source: StreamSource) -> Self
Set the source of the ready indicator
Specifies whether to look for the ready indicator in stdout or stderr.
§Arguments
source- Stream source (Stdout or Stderr)
§Examples
use tcrm_task::tasks::config::{TaskConfig, StreamSource};
let config = TaskConfig::new("my-server")
.ready_indicator("Ready")
.ready_indicator_source(StreamSource::Stderr);Sourcepub fn validate(&self) -> Result<(), TaskError>
pub fn validate(&self) -> Result<(), TaskError>
Validate the configuration
Validates all configuration parameters for security and correctness. This method should be called before executing the task to ensure safe operation.
§Validation Checks
- Command: Must not be empty, contain shell injection patterns, or exceed length limits
- Arguments: Must not contain null bytes or shell injection patterns
- Working Directory: Must exist and be a valid directory
- Environment Variables: Keys must not contain spaces, ‘=’, or null bytes
- Timeout: Must be greater than 0 if specified
- Ready Indicator: Must not be empty if specified
§Returns
Ok(())if the configuration is validErr(TaskError::InvalidConfiguration)with details if validation fails
§Errors
Returns a TaskError if any validation check fails:
TaskError::InvalidConfigurationfor configuration errorsTaskError::IOfor working directory validation failures
§Examples
use tcrm_task::tasks::config::TaskConfig;
// Valid config
let config = TaskConfig::new("echo")
.args(["hello", "world"]);
assert!(config.validate().is_ok());
// Invalid config (empty command)
let config = TaskConfig::new("");
assert!(config.validate().is_err());
// Invalid config (zero timeout)
let config = TaskConfig::new("sleep")
.timeout_ms(0);
assert!(config.validate().is_err());Trait Implementations§
Source§impl Clone for TaskConfig
impl Clone for TaskConfig
Source§fn clone(&self) -> TaskConfig
fn clone(&self) -> TaskConfig
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more