use std::collections::HashMap;
use std::ffi::OsString;
use std::path::PathBuf;
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
#[non_exhaustive]
pub enum EnvPolicy {
Inherit,
Empty,
}
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct ProcessSpec {
pub program: PathBuf,
pub args: Vec<OsString>,
pub dir: Option<PathBuf>,
pub env: HashMap<String, String>,
pub env_policy: EnvPolicy,
}
impl ProcessSpec {
#[must_use]
pub fn new<P: Into<PathBuf>>(program: P) -> Self {
Self {
program: program.into(),
args: Vec::new(),
dir: None,
env: HashMap::new(),
env_policy: EnvPolicy::Inherit,
}
}
#[must_use]
pub fn arg<S: Into<OsString>>(mut self, arg: S) -> Self {
self.args.push(arg.into());
self
}
#[must_use]
pub fn args<I>(mut self, args: I) -> Self
where
I: IntoIterator,
I::Item: Into<OsString>,
{
self.args.extend(args.into_iter().map(Into::into));
self
}
#[must_use]
pub fn dir<P: Into<PathBuf>>(mut self, dir: P) -> Self {
self.dir = Some(dir.into());
self
}
#[must_use]
pub fn env<K: Into<String>, V: Into<String>>(mut self, key: K, value: V) -> Self {
self.env.insert(key.into(), value.into());
self
}
#[must_use]
pub fn envs<K: Into<String>, V: Into<String>, I: IntoIterator<Item = (K, V)>>(
mut self,
vars: I,
) -> Self {
for (k, v) in vars {
self.env.insert(k.into(), v.into());
}
self
}
#[must_use]
pub fn env_policy(mut self, policy: EnvPolicy) -> Self {
self.env_policy = policy;
self
}
#[must_use]
pub fn empty_env(mut self) -> Self {
self.env_policy = EnvPolicy::Empty;
self
}
}
#[must_use]
pub fn command<P: Into<PathBuf>>(program: P) -> ProcessSpec {
ProcessSpec::new(program)
}