use futures::future::BoxFuture;
use futures::stream::BoxStream;
use std::path::PathBuf;
use tokio_util::sync::CancellationToken;
use super::ExecError;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ProcessSpec {
pub program: String,
pub args: Vec<String>,
pub cwd: PathBuf,
pub env: Vec<(String, String)>,
pub env_scope: super::env::EnvScope,
pub stdin: Option<Vec<u8>>,
}
impl ProcessSpec {
pub fn new(program: impl Into<String>, cwd: impl Into<PathBuf>) -> Self {
Self {
program: program.into(),
args: Vec::new(),
cwd: cwd.into(),
env: Vec::new(),
env_scope: super::env::EnvScope::Session,
stdin: None,
}
}
pub fn arg(mut self, a: impl Into<String>) -> Self {
self.args.push(a.into());
self
}
pub fn args<I: IntoIterator<Item = S>, S: Into<String>>(mut self, it: I) -> Self {
self.args.extend(it.into_iter().map(Into::into));
self
}
}
pub const OUTPUT_GRACE_AFTER_EXIT: std::time::Duration = std::time::Duration::from_secs(1);
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OutputStream {
Stdout,
Stderr,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct OutputChunk {
pub stream: OutputStream,
pub bytes: Vec<u8>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ExitStatus {
pub code: Option<i32>,
pub success: bool,
}
pub trait ProcessHandle: Send {
fn output(&mut self) -> Option<BoxStream<'static, Result<OutputChunk, ExecError>>>;
fn kill(&mut self) -> BoxFuture<'_, ()>;
fn wait(&mut self) -> BoxFuture<'_, Result<ExitStatus, ExecError>>;
}
#[async_trait::async_trait]
pub trait Process: Send + Sync {
async fn spawn(
&self,
spec: ProcessSpec,
cancel: CancellationToken,
) -> Result<Box<dyn ProcessHandle>, ExecError>;
}