Skip to main content

compose_lens/model/
build_extra_host.rs

1//! Build-specific `extra_hosts` values.
2
3use super::Located;
4use crate::source::SourceSpan;
5
6/// Build-time host mappings with list and mapping syntax retained separately from service
7/// [`super::ExtraHosts`].
8#[derive(Debug, Clone, PartialEq, Eq)]
9pub enum BuildExtraHosts {
10    /// Ordered raw string entries, including duplicates.
11    List {
12        /// The complete sequence span.
13        span: SourceSpan,
14        /// Raw host-mapping strings in authored order.
15        values: Vec<Located<String>>,
16    },
17    /// Ordered hostname entries with scalar or list addresses.
18    Map {
19        /// The complete mapping span.
20        span: SourceSpan,
21        /// Hostname entries in authored order.
22        entries: Vec<BuildExtraHostEntry>,
23    },
24}
25
26impl BuildExtraHosts {
27    /// Returns the complete collection span.
28    #[must_use]
29    pub const fn span(&self) -> SourceSpan {
30        match self {
31            Self::List { span, .. } | Self::Map { span, .. } => *span,
32        }
33    }
34}
35
36/// One mapping-form build host entry.
37#[derive(Debug, Clone, PartialEq, Eq)]
38pub struct BuildExtraHostEntry {
39    hostname: Located<String>,
40    addresses: BuildExtraHostAddresses,
41    span: SourceSpan,
42}
43
44impl BuildExtraHostEntry {
45    pub(crate) const fn new(hostname: Located<String>, addresses: BuildExtraHostAddresses, span: SourceSpan) -> Self {
46        Self {
47            hostname,
48            addresses,
49            span,
50        }
51    }
52
53    /// Returns the raw hostname key and its source span.
54    #[must_use]
55    pub const fn hostname(&self) -> &Located<String> {
56        &self.hostname
57    }
58
59    /// Returns the scalar or ordered-list address form without parsing it as an IP address.
60    #[must_use]
61    pub const fn addresses(&self) -> &BuildExtraHostAddresses {
62        &self.addresses
63    }
64
65    /// Returns the complete mapping-entry span.
66    #[must_use]
67    pub const fn span(&self) -> SourceSpan {
68        self.span
69    }
70}
71
72/// Addresses authored for one build host mapping key.
73#[derive(Debug, Clone, PartialEq, Eq)]
74pub enum BuildExtraHostAddresses {
75    /// One raw string address.
76    Scalar(Located<String>),
77    /// Ordered raw string addresses.
78    List {
79        /// The complete nested sequence span.
80        span: SourceSpan,
81        /// Raw addresses in authored order.
82        values: Vec<Located<String>>,
83    },
84}
85
86impl BuildExtraHostAddresses {
87    /// Returns the scalar address when that form was authored.
88    #[must_use]
89    pub const fn as_scalar(&self) -> Option<&Located<String>> {
90        match self {
91            Self::Scalar(value) => Some(value),
92            Self::List { .. } => None,
93        }
94    }
95
96    /// Returns ordered addresses when the nested list form was authored.
97    #[must_use]
98    pub fn as_list(&self) -> Option<&[Located<String>]> {
99        let Self::List { values, .. } = self else {
100            return None;
101        };
102        Some(values)
103    }
104
105    /// Returns the complete address-form span.
106    #[must_use]
107    pub const fn span(&self) -> SourceSpan {
108        match self {
109            Self::Scalar(value) => value.span(),
110            Self::List { span, .. } => *span,
111        }
112    }
113}