Skip to main content

cageforge_command/
stdio.rs

1// SPDX-License-Identifier: Apache-2.0
2
3//! Portable standard-stream routing for [`crate::CommandRequest`].
4//!
5//! The values describe intent; the process adapter maps them to pipes, null
6//! devices, or inherited handles on its target platform.
7
8/// Routing for one standard process stream.
9#[derive(Debug, Clone, Copy, PartialEq, Eq)]
10pub enum StdioMode {
11    /// Connect the child stream to the launcher process's corresponding
12    /// standard stream.
13    Inherit,
14    /// Connect the child stream to the platform's null device.
15    Null,
16    /// Ask the backend to create a pipe for the caller.
17    Pipe,
18}
19
20/// Portable routing choices for stdin, stdout, and stderr.
21#[derive(Debug, Clone, Copy, PartialEq, Eq)]
22pub struct StdioSpec {
23    stdin: StdioMode,
24    stdout: StdioMode,
25    stderr: StdioMode,
26}
27
28impl StdioSpec {
29    /// Creates explicit routing choices for all three standard streams.
30    pub const fn new(stdin: StdioMode, stdout: StdioMode, stderr: StdioMode) -> Self {
31        Self {
32            stdin,
33            stdout,
34            stderr,
35        }
36    }
37
38    /// Creates the non-interactive default: closed stdin and captured output.
39    pub const fn captured() -> Self {
40        Self::new(StdioMode::Null, StdioMode::Pipe, StdioMode::Pipe)
41    }
42
43    /// Creates a request that inherits all three standard streams.
44    pub const fn inherited() -> Self {
45        Self::new(StdioMode::Inherit, StdioMode::Inherit, StdioMode::Inherit)
46    }
47
48    /// Replaces stdin routing.
49    pub const fn with_stdin(mut self, mode: StdioMode) -> Self {
50        self.stdin = mode;
51        self
52    }
53
54    /// Replaces stdout routing.
55    pub const fn with_stdout(mut self, mode: StdioMode) -> Self {
56        self.stdout = mode;
57        self
58    }
59
60    /// Replaces stderr routing.
61    pub const fn with_stderr(mut self, mode: StdioMode) -> Self {
62        self.stderr = mode;
63        self
64    }
65
66    /// Returns stdin routing.
67    pub const fn stdin(&self) -> StdioMode {
68        self.stdin
69    }
70
71    /// Returns stdout routing.
72    pub const fn stdout(&self) -> StdioMode {
73        self.stdout
74    }
75
76    /// Returns stderr routing.
77    pub const fn stderr(&self) -> StdioMode {
78        self.stderr
79    }
80}
81
82impl Default for StdioSpec {
83    fn default() -> Self {
84        Self::captured()
85    }
86}