Skip to main content

compose_lens/model/
value.rs

1//! Shared scalar and collection values used by the typed Compose model.
2
3use super::Located;
4use crate::source::SourceSpan;
5
6/// A Compose boolean before optional interpolation is evaluated.
7#[derive(Debug, Clone, PartialEq, Eq)]
8pub enum BooleanValue {
9    /// A YAML boolean literal.
10    Literal(bool),
11    /// A scalar expression whose result must later be validated as a boolean.
12    Expression(String),
13}
14
15/// A YAML scalar retained without applying Compose interpolation or coercion.
16#[derive(Debug, Clone, PartialEq, Eq)]
17pub enum ComposeScalar {
18    /// An explicit YAML null.
19    Null,
20    /// A YAML boolean.
21    Boolean(bool),
22    /// A numeric scalar with its authored semantic spelling retained.
23    Number(String),
24    /// A string scalar, which may still contain interpolation expressions.
25    String(String),
26}
27
28/// One source-aware key/value entry from a Compose mapping.
29#[derive(Debug, Clone, PartialEq, Eq)]
30pub struct KeyValueEntry {
31    key: Located<String>,
32    value: Located<ComposeScalar>,
33    span: SourceSpan,
34}
35
36impl KeyValueEntry {
37    pub(crate) const fn new(key: Located<String>, value: Located<ComposeScalar>, span: SourceSpan) -> Self {
38        Self { key, value, span }
39    }
40
41    /// Returns the entry key.
42    #[must_use]
43    pub const fn key(&self) -> &Located<String> {
44        &self.key
45    }
46
47    /// Returns the scalar entry value.
48    #[must_use]
49    pub const fn value(&self) -> &Located<ComposeScalar> {
50        &self.value
51    }
52
53    /// Returns the complete key/value span.
54    #[must_use]
55    pub const fn span(&self) -> SourceSpan {
56        self.span
57    }
58}
59
60/// A Compose labels value with its list or mapping form retained.
61#[derive(Debug, Clone, PartialEq, Eq)]
62pub enum Labels {
63    /// List syntax such as `com.example.role=database`.
64    List {
65        /// The complete sequence span.
66        span: SourceSpan,
67        /// Label strings in authored order.
68        values: Vec<Located<String>>,
69    },
70    /// Mapping syntax with scalar values.
71    Map {
72        /// The complete mapping span.
73        span: SourceSpan,
74        /// Label entries in authored order.
75        entries: Vec<KeyValueEntry>,
76    },
77}
78
79impl Labels {
80    /// Returns the authored collection span.
81    #[must_use]
82    pub const fn span(&self) -> SourceSpan {
83        match self {
84            Self::List { span, .. } | Self::Map { span, .. } => *span,
85        }
86    }
87}