systemd_run/
ioredirect.rs

1#[allow(dead_code)]
2pub enum Priv {
3    Inherit,
4    Null,
5    Journal,
6    File(String),
7    Truncate(String),
8    Append(String),
9}
10
11impl Priv {
12    fn marshal(self) -> (&'static str, String) {
13        use Priv::*;
14        match self {
15            Inherit => ("", "inherit".to_string()),
16            Null => ("", "null".to_string()),
17            Journal => ("", "journal".to_string()),
18            File(x) => ("File", x),
19            Truncate(x) => ("FileToTruncate", x),
20            Append(x) => ("FileToAppend", x),
21        }
22    }
23}
24
25/// The description of a input.
26///
27/// Read `StandardInput` in [systemd.exec(5)](man:systemd.exec(5)) for
28/// details.
29pub struct InputSpec(Priv);
30
31impl InputSpec {
32    /// Connect `/dev/null` to the input.
33    pub fn null() -> Self {
34        Self(Priv::Null)
35    }
36
37    /// Specify the a path where the file is connected to the input.
38    ///
39    /// This setting will be unavailable if the feature `systemd_236` is
40    /// disabled.
41    #[cfg(feature = "systemd_236")]
42    pub fn file<T: AsRef<str>>(path: T) -> Self {
43        Self(Priv::File(path.as_ref().to_owned()))
44    }
45}
46
47pub fn marshal_input(spec: InputSpec) -> (&'static str, String) {
48    spec.0.marshal()
49}
50
51/// The description of a output.
52///
53/// Read `StandardOutput` and `StandardError` in
54/// [systemd.exec(5)](man:systemd.exec(5)) for details.
55pub struct OutputSpec(Priv);
56
57impl OutputSpec {
58    /// For `stdout`, use the same file for `stdin`.
59    /// For `stderr`, use the same file for `stdout`.
60    pub fn inherit() -> Self {
61        Self(Priv::Inherit)
62    }
63
64    /// Connect `/dev/null` to the output.
65    pub fn null() -> Self {
66        Self(Priv::Null)
67    }
68
69    /// Log the output into system journal.
70    pub fn journal() -> Self {
71        Self(Priv::Journal)
72    }
73
74    /// Specify a path where the file will be overwritten from offset 0.
75    /// If the file does not exist, it will be created.
76    ///
77    /// This setting will be unavailable if the feature `systemd_236` is
78    /// disabled.
79    #[cfg(feature = "systemd_236")]
80    pub fn file<T: AsRef<str>>(path: T) -> Self {
81        Self(Priv::File(path.as_ref().to_owned()))
82    }
83
84    /// Like [OutputSpec::file], but the file will be truncated before
85    /// written.
86    ///
87    /// This setting will be unavailable if the feature `systemd_248` is
88    /// disabled.
89    #[cfg(feature = "systemd_248")]
90    pub fn truncate<T: AsRef<str>>(path: T) -> Self {
91        Self(Priv::Truncate(path.as_ref().to_owned()))
92    }
93
94    /// Like [OutputSpec::file], but the file will be written from its end,
95    /// instead of offset 0.
96    ///
97    /// This setting will be unavailable if the feature `systemd_240` is
98    /// disabled.
99    #[cfg(feature = "systemd_240")]
100    pub fn append<T: AsRef<str>>(path: T) -> Self {
101        Self(Priv::Append(path.as_ref().to_owned()))
102    }
103}
104
105pub fn marshal_output(spec: OutputSpec) -> (&'static str, String) {
106    spec.0.marshal()
107}