use std::collections::BTreeMap;
use bon::Builder;
use indexmap::IndexMap;
#[derive(Builder, Clone, Debug)]
#[builder(builder_type = Builder)]
pub struct Execution {
#[builder(into)]
pub(crate) image: String,
#[builder(into)]
pub(crate) program: String,
#[builder(into, default)]
pub(crate) args: Vec<String>,
#[builder(into)]
pub(crate) work_dir: Option<String>,
#[builder(into)]
pub(crate) stdin: Option<String>,
#[builder(into)]
pub(crate) stdout: Option<String>,
#[builder(into)]
pub(crate) stderr: Option<String>,
#[builder(into, default)]
pub(crate) env: IndexMap<String, String>,
}
impl Execution {
pub fn image(&self) -> &str {
&self.image
}
pub fn program(&self) -> &str {
&self.program
}
pub fn args(&self) -> &[String] {
&self.args
}
pub fn work_dir(&self) -> Option<&str> {
self.work_dir.as_deref()
}
pub fn stdin(&self) -> Option<&str> {
self.stdin.as_deref()
}
pub fn stdout(&self) -> Option<&str> {
self.stdout.as_deref()
}
pub fn stderr(&self) -> Option<&str> {
self.stderr.as_deref()
}
pub fn env(&self) -> &IndexMap<String, String> {
&self.env
}
}
impl From<Execution> for tes::v1::types::task::Executor {
fn from(execution: Execution) -> Self {
let env = execution
.env
.into_iter()
.collect::<BTreeMap<String, String>>();
let env = if env.is_empty() { None } else { Some(env) };
let mut command = Vec::with_capacity(execution.args.len() + 1);
command.push(execution.program);
command.extend(execution.args);
tes::v1::types::task::Executor {
image: execution.image.to_owned(),
command,
workdir: execution.work_dir,
stdin: execution.stdin,
stdout: execution.stdout,
stderr: execution.stderr,
env,
ignore_error: Some(true),
}
}
}