1use super::{FieldReference, Located};
4use crate::source::SourceSpan;
5
6#[derive(Debug, Clone, PartialEq, Eq)]
8pub enum Build {
9 Context(Located<String>),
11 Definition(BuildDefinition),
13}
14
15#[derive(Debug, Clone, PartialEq, Eq)]
17pub struct BuildDefinition {
18 span: SourceSpan,
19 fields: Vec<BuildField>,
20 extension_fields: Vec<FieldReference>,
21 unknown_fields: Vec<FieldReference>,
22}
23
24impl BuildDefinition {
25 pub(super) const fn new(span: SourceSpan) -> Self {
26 Self {
27 span,
28 fields: Vec::new(),
29 extension_fields: Vec::new(),
30 unknown_fields: Vec::new(),
31 }
32 }
33
34 pub(super) fn push_field(&mut self, field: BuildField) {
35 self.fields.push(field);
36 }
37
38 pub(super) fn push_extension(&mut self, field: FieldReference) {
39 self.extension_fields.push(field);
40 }
41
42 pub(super) fn push_unknown(&mut self, field: FieldReference) {
43 self.unknown_fields.push(field);
44 }
45
46 #[must_use]
48 pub const fn span(&self) -> SourceSpan {
49 self.span
50 }
51
52 #[must_use]
54 pub fn fields(&self) -> &[BuildField] {
55 &self.fields
56 }
57
58 #[must_use]
60 pub fn field(&self, kind: BuildFieldKind) -> Option<&BuildField> {
61 self.fields.iter().find(|field| field.kind == kind)
62 }
63
64 #[must_use]
66 pub fn extension_fields(&self) -> &[FieldReference] {
67 &self.extension_fields
68 }
69
70 #[must_use]
72 pub fn unknown_fields(&self) -> &[FieldReference] {
73 &self.unknown_fields
74 }
75}
76
77#[derive(Debug, Clone, PartialEq, Eq)]
79pub struct BuildField {
80 kind: BuildFieldKind,
81 reference: FieldReference,
82}
83
84impl BuildField {
85 pub(super) const fn new(kind: BuildFieldKind, reference: FieldReference) -> Self {
86 Self { kind, reference }
87 }
88
89 #[must_use]
91 pub const fn kind(&self) -> BuildFieldKind {
92 self.kind
93 }
94
95 #[must_use]
97 pub const fn reference(&self) -> &FieldReference {
98 &self.reference
99 }
100}
101
102#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
104#[non_exhaustive]
105pub enum BuildFieldKind {
106 AdditionalContexts,
108 Args,
110 CacheFrom,
112 CacheTo,
114 Context,
116 Dockerfile,
118 DockerfileInline,
120 Entitlements,
122 ExtraHosts,
124 Isolation,
126 Labels,
128 Network,
130 NoCache,
132 Platforms,
134 Privileged,
136 Provenance,
138 Pull,
140 Sbom,
142 Secrets,
144 Ssh,
146 ShmSize,
148 Tags,
150 Target,
152 Ulimits,
154}
155
156impl BuildFieldKind {
157 pub(super) fn from_name(name: &str) -> Option<Self> {
158 Some(match name {
159 "additional_contexts" => Self::AdditionalContexts,
160 "args" => Self::Args,
161 "cache_from" => Self::CacheFrom,
162 "cache_to" => Self::CacheTo,
163 "context" => Self::Context,
164 "dockerfile" => Self::Dockerfile,
165 "dockerfile_inline" => Self::DockerfileInline,
166 "entitlements" => Self::Entitlements,
167 "extra_hosts" => Self::ExtraHosts,
168 "isolation" => Self::Isolation,
169 "labels" => Self::Labels,
170 "network" => Self::Network,
171 "no_cache" => Self::NoCache,
172 "platforms" => Self::Platforms,
173 "privileged" => Self::Privileged,
174 "provenance" => Self::Provenance,
175 "pull" => Self::Pull,
176 "sbom" => Self::Sbom,
177 "secrets" => Self::Secrets,
178 "ssh" => Self::Ssh,
179 "shm_size" => Self::ShmSize,
180 "tags" => Self::Tags,
181 "target" => Self::Target,
182 "ulimits" => Self::Ulimits,
183 _ => return None,
184 })
185 }
186}
187
188#[derive(Debug, Clone, PartialEq, Eq)]
190pub struct DeployDefinition {
191 span: SourceSpan,
192 fields: Vec<DeployField>,
193 extension_fields: Vec<FieldReference>,
194 unknown_fields: Vec<FieldReference>,
195}
196
197impl DeployDefinition {
198 pub(super) const fn new(span: SourceSpan) -> Self {
199 Self {
200 span,
201 fields: Vec::new(),
202 extension_fields: Vec::new(),
203 unknown_fields: Vec::new(),
204 }
205 }
206
207 pub(super) fn push_field(&mut self, field: DeployField) {
208 self.fields.push(field);
209 }
210
211 pub(super) fn push_extension(&mut self, field: FieldReference) {
212 self.extension_fields.push(field);
213 }
214
215 pub(super) fn push_unknown(&mut self, field: FieldReference) {
216 self.unknown_fields.push(field);
217 }
218
219 #[must_use]
221 pub const fn span(&self) -> SourceSpan {
222 self.span
223 }
224
225 #[must_use]
227 pub fn fields(&self) -> &[DeployField] {
228 &self.fields
229 }
230
231 #[must_use]
233 pub fn field(&self, kind: DeployFieldKind) -> Option<&DeployField> {
234 self.fields.iter().find(|field| field.kind == kind)
235 }
236
237 #[must_use]
239 pub fn extension_fields(&self) -> &[FieldReference] {
240 &self.extension_fields
241 }
242
243 #[must_use]
245 pub fn unknown_fields(&self) -> &[FieldReference] {
246 &self.unknown_fields
247 }
248}
249
250#[derive(Debug, Clone, PartialEq, Eq)]
252pub struct DeployField {
253 kind: DeployFieldKind,
254 reference: FieldReference,
255}
256
257impl DeployField {
258 pub(super) const fn new(kind: DeployFieldKind, reference: FieldReference) -> Self {
259 Self { kind, reference }
260 }
261
262 #[must_use]
264 pub const fn kind(&self) -> DeployFieldKind {
265 self.kind
266 }
267
268 #[must_use]
270 pub const fn reference(&self) -> &FieldReference {
271 &self.reference
272 }
273}
274
275#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
277#[non_exhaustive]
278pub enum DeployFieldKind {
279 EndpointMode,
281 Labels,
283 Mode,
285 Placement,
287 Replicas,
289 Resources,
291 RestartPolicy,
293 RollbackConfig,
295 UpdateConfig,
297}
298
299impl DeployFieldKind {
300 pub(super) fn from_name(name: &str) -> Option<Self> {
301 Some(match name {
302 "endpoint_mode" => Self::EndpointMode,
303 "labels" => Self::Labels,
304 "mode" => Self::Mode,
305 "placement" => Self::Placement,
306 "replicas" => Self::Replicas,
307 "resources" => Self::Resources,
308 "restart_policy" => Self::RestartPolicy,
309 "rollback_config" => Self::RollbackConfig,
310 "update_config" => Self::UpdateConfig,
311 _ => return None,
312 })
313 }
314}