crankshaft_engine/task/
execution.rs1use std::collections::BTreeMap;
4
5use bon::Builder;
6use indexmap::IndexMap;
7
8#[derive(Builder, Clone, Debug)]
10#[builder(builder_type = Builder)]
11pub struct Execution {
12 #[builder(into)]
14 pub(crate) image: String,
15
16 #[builder(into)]
18 pub(crate) program: String,
19
20 #[builder(into, default)]
22 pub(crate) args: Vec<String>,
23
24 #[builder(into)]
26 pub(crate) work_dir: Option<String>,
27
28 #[builder(into)]
31 pub(crate) stdin: Option<String>,
32
33 #[builder(into)]
36 pub(crate) stdout: Option<String>,
37
38 #[builder(into)]
41 pub(crate) stderr: Option<String>,
42
43 #[builder(into, default)]
45 pub(crate) env: IndexMap<String, String>,
46}
47
48impl Execution {
49 pub fn image(&self) -> &str {
51 &self.image
52 }
53
54 pub fn program(&self) -> &str {
56 &self.program
57 }
58
59 pub fn args(&self) -> &[String] {
61 &self.args
62 }
63
64 pub fn work_dir(&self) -> Option<&str> {
66 self.work_dir.as_deref()
67 }
68
69 pub fn stdin(&self) -> Option<&str> {
71 self.stdin.as_deref()
72 }
73
74 pub fn stdout(&self) -> Option<&str> {
76 self.stdout.as_deref()
77 }
78
79 pub fn stderr(&self) -> Option<&str> {
81 self.stderr.as_deref()
82 }
83
84 pub fn env(&self) -> &IndexMap<String, String> {
86 &self.env
87 }
88}
89
90impl From<Execution> for tes::v1::types::task::Executor {
91 fn from(execution: Execution) -> Self {
92 let env = execution
93 .env
94 .into_iter()
95 .collect::<BTreeMap<String, String>>();
96
97 let env = if env.is_empty() { None } else { Some(env) };
98
99 let mut command = Vec::with_capacity(execution.args.len() + 1);
100 command.push(execution.program);
101 command.extend(execution.args);
102
103 tes::v1::types::task::Executor {
104 image: execution.image.to_owned(),
105 command,
106 workdir: execution.work_dir,
107 stdin: execution.stdin,
108 stdout: execution.stdout,
109 stderr: execution.stderr,
110 env,
111 ignore_error: Some(true),
112 }
113 }
114}