ZIm/crates/extension_api/src/process.rs
Marshall Bowers 4d106a4b0b
zed_extension_api: Add simple process API (#25399)
This PR adds a simple API for working with processes to the extension
API.

The API is designed to mirror Rust's
[`std::process::Command`](https://doc.rust-lang.org/std/process/struct.Command.html).

Release Notes:

- N/A
2025-02-22 18:05:36 +00:00

44 lines
1.1 KiB
Rust

//! A module for working with processes.
use crate::wit::zed::extension::process;
pub use crate::wit::zed::extension::process::{Command, Output};
impl Command {
pub fn new(program: impl Into<String>) -> Self {
Self {
command: program.into(),
args: Vec::new(),
env: Vec::new(),
}
}
pub fn arg(mut self, arg: impl Into<String>) -> Self {
self.args.push(arg.into());
self
}
pub fn args(mut self, args: impl IntoIterator<Item = impl Into<String>>) -> Self {
self.args.extend(args.into_iter().map(Into::into));
self
}
pub fn env(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
self.env.push((key.into(), value.into()));
self
}
pub fn envs(
mut self,
envs: impl IntoIterator<Item = (impl Into<String>, impl Into<String>)>,
) -> Self {
self.env.extend(
envs.into_iter()
.map(|(key, value)| (key.into(), value.into())),
);
self
}
pub fn output(&mut self) -> Result<Output, String> {
process::run_command(self)
}
}