Skip to main content

rskit_process/command/
config.rs

1//! Aggregate configuration for subprocess execution behavior.
2
3use std::time::Duration;
4
5use super::io::{InputPolicy, ProcessIo};
6use super::redaction::ArgRedaction;
7use super::signal::SignalPolicy;
8
9/// Configuration for subprocess execution behavior.
10#[derive(Debug, Clone)]
11#[non_exhaustive]
12pub struct ProcessConfig {
13    /// Overall timeout for the process. None means no timeout.
14    pub timeout: Option<Duration>,
15    /// Explicit I/O strategy.
16    pub io: ProcessIo,
17    /// Signal and process-tree termination policy.
18    pub signal: SignalPolicy,
19    /// Redaction policy for command-line arguments emitted in spawn logs.
20    pub arg_redaction: ArgRedaction,
21}
22
23impl Default for ProcessConfig {
24    fn default() -> Self {
25        Self {
26            timeout: Some(Duration::from_secs(30)),
27            io: ProcessIo::default(),
28            signal: SignalPolicy::default(),
29            arg_redaction: ArgRedaction::default(),
30        }
31    }
32}
33
34impl ProcessConfig {
35    /// Set the process timeout.
36    #[must_use]
37    pub fn with_timeout(mut self, timeout: Option<Duration>) -> Self {
38        self.timeout = timeout;
39        self
40    }
41
42    /// Set the I/O strategy.
43    #[must_use]
44    pub fn with_io(mut self, io: ProcessIo) -> Self {
45        self.io = io;
46        self
47    }
48
49    /// Set the signal policy.
50    #[must_use]
51    pub fn with_signal_policy(mut self, signal: SignalPolicy) -> Self {
52        self.signal = signal;
53        self
54    }
55
56    /// Set the command-line argument redaction policy used for spawn logs.
57    #[must_use]
58    pub fn with_arg_redaction(mut self, arg_redaction: ArgRedaction) -> Self {
59        self.arg_redaction = arg_redaction;
60        self
61    }
62
63    /// Add a custom secret-bearing command-line argument name for spawn-log redaction.
64    #[must_use]
65    pub fn with_sensitive_arg_name(mut self, name: impl AsRef<str>) -> Self {
66        self.arg_redaction = self.arg_redaction.with_name(name);
67        self
68    }
69
70    /// Set the maximum retained bytes for captured or observed output.
71    #[must_use]
72    pub fn with_max_output_bytes(mut self, bytes: usize) -> Self {
73        match &mut self.io {
74            ProcessIo::Captured(io) => io.output.max_output_bytes = Some(bytes),
75            ProcessIo::Observed(io) => io.output.max_output_bytes = Some(bytes),
76            #[cfg(unix)]
77            ProcessIo::Pty(io) => io.output.max_output_bytes = Some(bytes),
78            ProcessIo::Inherited(_) => {}
79        }
80        self
81    }
82
83    /// Disable output capture bounds for captured or observed output.
84    #[must_use]
85    pub fn with_unbounded_output(mut self) -> Self {
86        match &mut self.io {
87            ProcessIo::Captured(io) => io.output.max_output_bytes = None,
88            ProcessIo::Observed(io) => io.output.max_output_bytes = None,
89            #[cfg(unix)]
90            ProcessIo::Pty(io) => io.output.max_output_bytes = None,
91            ProcessIo::Inherited(_) => {}
92        }
93        self
94    }
95
96    /// Set stdin for the configured I/O mode.
97    #[must_use]
98    pub fn with_input(mut self, input: InputPolicy) -> Self {
99        match &mut self.io {
100            ProcessIo::Captured(io) => io.input = input,
101            ProcessIo::Observed(io) => io.input = input,
102            #[cfg(unix)]
103            ProcessIo::Pty(io) => io.input = input,
104            ProcessIo::Inherited(io) => io.input = input,
105        }
106        self
107    }
108}