use std::ffi::OsString;
use std::io;
use std::path::PathBuf;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Invocation {
program: OsString,
args: Vec<OsString>,
current_dir: Option<PathBuf>,
}
impl Invocation {
pub fn new(program: impl Into<OsString>) -> Self {
Self {
program: program.into(),
args: Vec::new(),
current_dir: None,
}
}
#[must_use]
pub fn arg(mut self, arg: impl Into<OsString>) -> Self {
self.args.push(arg.into());
self
}
#[must_use]
pub fn current_dir(mut self, dir: impl Into<PathBuf>) -> Self {
self.current_dir = Some(dir.into());
self
}
pub fn program(&self) -> &OsString {
&self.program
}
pub fn args(&self) -> &[OsString] {
&self.args
}
pub fn working_dir(&self) -> Option<&PathBuf> {
self.current_dir.as_ref()
}
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct Output {
pub status: Option<i32>,
pub stdout: Vec<u8>,
pub stderr: Vec<u8>,
}
impl Output {
pub fn success(&self) -> bool {
self.status == Some(0)
}
}
pub trait ProcessRunner: Send + Sync {
fn run(&self, invocation: &Invocation) -> io::Result<Output>;
}
#[cfg(test)]
mod tests {
use super::Invocation;
use std::path::PathBuf;
#[test]
fn invocation_builder_keeps_order_and_dir() {
let inv = Invocation::new("uv")
.arg("lock")
.arg("--upgrade-package")
.current_dir("/work");
assert_eq!(inv.args(), ["lock", "--upgrade-package"]);
assert_eq!(inv.working_dir(), Some(&PathBuf::from("/work")));
assert_eq!(Invocation::new("git").working_dir(), None);
}
}