use super::{FieldReference, Located};
use crate::source::SourceSpan;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Build {
Context(Located<String>),
Definition(BuildDefinition),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BuildDefinition {
span: SourceSpan,
fields: Vec<BuildField>,
extension_fields: Vec<FieldReference>,
unknown_fields: Vec<FieldReference>,
}
impl BuildDefinition {
pub(super) const fn new(span: SourceSpan) -> Self {
Self {
span,
fields: Vec::new(),
extension_fields: Vec::new(),
unknown_fields: Vec::new(),
}
}
pub(super) fn push_field(&mut self, field: BuildField) {
self.fields.push(field);
}
pub(super) fn push_extension(&mut self, field: FieldReference) {
self.extension_fields.push(field);
}
pub(super) fn push_unknown(&mut self, field: FieldReference) {
self.unknown_fields.push(field);
}
#[must_use]
pub const fn span(&self) -> SourceSpan {
self.span
}
#[must_use]
pub fn fields(&self) -> &[BuildField] {
&self.fields
}
#[must_use]
pub fn field(&self, kind: BuildFieldKind) -> Option<&BuildField> {
self.fields.iter().find(|field| field.kind == kind)
}
#[must_use]
pub fn extension_fields(&self) -> &[FieldReference] {
&self.extension_fields
}
#[must_use]
pub fn unknown_fields(&self) -> &[FieldReference] {
&self.unknown_fields
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BuildField {
kind: BuildFieldKind,
reference: FieldReference,
}
impl BuildField {
pub(super) const fn new(kind: BuildFieldKind, reference: FieldReference) -> Self {
Self { kind, reference }
}
#[must_use]
pub const fn kind(&self) -> BuildFieldKind {
self.kind
}
#[must_use]
pub const fn reference(&self) -> &FieldReference {
&self.reference
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum BuildFieldKind {
AdditionalContexts,
Args,
CacheFrom,
CacheTo,
Context,
Dockerfile,
DockerfileInline,
Entitlements,
ExtraHosts,
Isolation,
Labels,
Network,
NoCache,
Platforms,
Privileged,
Provenance,
Pull,
Sbom,
Secrets,
Ssh,
ShmSize,
Tags,
Target,
Ulimits,
}
impl BuildFieldKind {
pub(super) fn from_name(name: &str) -> Option<Self> {
Some(match name {
"additional_contexts" => Self::AdditionalContexts,
"args" => Self::Args,
"cache_from" => Self::CacheFrom,
"cache_to" => Self::CacheTo,
"context" => Self::Context,
"dockerfile" => Self::Dockerfile,
"dockerfile_inline" => Self::DockerfileInline,
"entitlements" => Self::Entitlements,
"extra_hosts" => Self::ExtraHosts,
"isolation" => Self::Isolation,
"labels" => Self::Labels,
"network" => Self::Network,
"no_cache" => Self::NoCache,
"platforms" => Self::Platforms,
"privileged" => Self::Privileged,
"provenance" => Self::Provenance,
"pull" => Self::Pull,
"sbom" => Self::Sbom,
"secrets" => Self::Secrets,
"ssh" => Self::Ssh,
"shm_size" => Self::ShmSize,
"tags" => Self::Tags,
"target" => Self::Target,
"ulimits" => Self::Ulimits,
_ => return None,
})
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DeployDefinition {
span: SourceSpan,
fields: Vec<DeployField>,
extension_fields: Vec<FieldReference>,
unknown_fields: Vec<FieldReference>,
}
impl DeployDefinition {
pub(super) const fn new(span: SourceSpan) -> Self {
Self {
span,
fields: Vec::new(),
extension_fields: Vec::new(),
unknown_fields: Vec::new(),
}
}
pub(super) fn push_field(&mut self, field: DeployField) {
self.fields.push(field);
}
pub(super) fn push_extension(&mut self, field: FieldReference) {
self.extension_fields.push(field);
}
pub(super) fn push_unknown(&mut self, field: FieldReference) {
self.unknown_fields.push(field);
}
#[must_use]
pub const fn span(&self) -> SourceSpan {
self.span
}
#[must_use]
pub fn fields(&self) -> &[DeployField] {
&self.fields
}
#[must_use]
pub fn field(&self, kind: DeployFieldKind) -> Option<&DeployField> {
self.fields.iter().find(|field| field.kind == kind)
}
#[must_use]
pub fn extension_fields(&self) -> &[FieldReference] {
&self.extension_fields
}
#[must_use]
pub fn unknown_fields(&self) -> &[FieldReference] {
&self.unknown_fields
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DeployField {
kind: DeployFieldKind,
reference: FieldReference,
}
impl DeployField {
pub(super) const fn new(kind: DeployFieldKind, reference: FieldReference) -> Self {
Self { kind, reference }
}
#[must_use]
pub const fn kind(&self) -> DeployFieldKind {
self.kind
}
#[must_use]
pub const fn reference(&self) -> &FieldReference {
&self.reference
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum DeployFieldKind {
EndpointMode,
Labels,
Mode,
Placement,
Replicas,
Resources,
RestartPolicy,
RollbackConfig,
UpdateConfig,
}
impl DeployFieldKind {
pub(super) fn from_name(name: &str) -> Option<Self> {
Some(match name {
"endpoint_mode" => Self::EndpointMode,
"labels" => Self::Labels,
"mode" => Self::Mode,
"placement" => Self::Placement,
"replicas" => Self::Replicas,
"resources" => Self::Resources,
"restart_policy" => Self::RestartPolicy,
"rollback_config" => Self::RollbackConfig,
"update_config" => Self::UpdateConfig,
_ => return None,
})
}
}