rskit_process/command/
spec.rs1use std::collections::HashMap;
4use std::ffi::OsString;
5use std::path::PathBuf;
6
7#[derive(Debug, Clone, Copy, Eq, PartialEq)]
9#[non_exhaustive]
10pub enum EnvPolicy {
11 Inherit,
13 Empty,
15}
16
17#[derive(Debug, Clone, Eq, PartialEq)]
19pub struct ProcessSpec {
20 pub program: PathBuf,
22 pub args: Vec<OsString>,
24 pub dir: Option<PathBuf>,
26 pub env: HashMap<String, String>,
28 pub env_policy: EnvPolicy,
30}
31
32impl ProcessSpec {
33 #[must_use]
35 pub fn new<P: Into<PathBuf>>(program: P) -> Self {
36 Self {
37 program: program.into(),
38 args: Vec::new(),
39 dir: None,
40 env: HashMap::new(),
41 env_policy: EnvPolicy::Inherit,
42 }
43 }
44
45 #[must_use]
47 pub fn arg<S: Into<OsString>>(mut self, arg: S) -> Self {
48 self.args.push(arg.into());
49 self
50 }
51
52 #[must_use]
54 pub fn args<I>(mut self, args: I) -> Self
55 where
56 I: IntoIterator,
57 I::Item: Into<OsString>,
58 {
59 self.args.extend(args.into_iter().map(Into::into));
60 self
61 }
62
63 #[must_use]
65 pub fn dir<P: Into<PathBuf>>(mut self, dir: P) -> Self {
66 self.dir = Some(dir.into());
67 self
68 }
69
70 #[must_use]
72 pub fn env<K: Into<String>, V: Into<String>>(mut self, key: K, value: V) -> Self {
73 self.env.insert(key.into(), value.into());
74 self
75 }
76
77 #[must_use]
79 pub fn envs<K: Into<String>, V: Into<String>, I: IntoIterator<Item = (K, V)>>(
80 mut self,
81 vars: I,
82 ) -> Self {
83 for (k, v) in vars {
84 self.env.insert(k.into(), v.into());
85 }
86 self
87 }
88
89 #[must_use]
91 pub fn env_policy(mut self, policy: EnvPolicy) -> Self {
92 self.env_policy = policy;
93 self
94 }
95
96 #[must_use]
98 pub fn empty_env(mut self) -> Self {
99 self.env_policy = EnvPolicy::Empty;
100 self
101 }
102}
103
104#[must_use]
106pub fn command<P: Into<PathBuf>>(program: P) -> ProcessSpec {
107 ProcessSpec::new(program)
108}