Skip to main content

compose_lens/model/
environment.rs

1//! Service environment forms.
2
3use super::{ComposeScalar, Located};
4use crate::source::SourceSpan;
5
6/// One array-syntax environment entry.
7#[derive(Debug, Clone, PartialEq, Eq)]
8pub struct EnvironmentListEntry {
9    raw: Located<String>,
10    name: String,
11    value: Option<String>,
12}
13
14impl EnvironmentListEntry {
15    pub(super) fn parse(raw: Located<String>) -> Self {
16        let (name, value) = raw.value().split_once('=').map_or_else(
17            || (raw.value().clone(), None),
18            |(name, value)| (name.to_owned(), Some(value.to_owned())),
19        );
20        Self { raw, name, value }
21    }
22
23    /// Returns the complete semantic entry and its source span.
24    #[must_use]
25    pub const fn raw(&self) -> &Located<String> {
26        &self.raw
27    }
28
29    /// Returns the variable name.
30    #[must_use]
31    pub fn name(&self) -> &str {
32        &self.name
33    }
34
35    /// Returns the value after the first equals sign.
36    ///
37    /// `None` means no equals sign was authored; `Some("")` means an explicitly empty value.
38    #[must_use]
39    pub fn value(&self) -> Option<&str> {
40        self.value.as_deref()
41    }
42}
43
44/// One mapping-syntax environment entry.
45#[derive(Debug, Clone, PartialEq, Eq)]
46pub struct EnvironmentMapEntry {
47    name: Located<String>,
48    value: Located<ComposeScalar>,
49    span: SourceSpan,
50}
51
52impl EnvironmentMapEntry {
53    pub(super) const fn new(name: Located<String>, value: Located<ComposeScalar>, span: SourceSpan) -> Self {
54        Self { name, value, span }
55    }
56
57    /// Returns the environment-variable name.
58    #[must_use]
59    pub const fn name(&self) -> &Located<String> {
60        &self.name
61    }
62
63    /// Returns the unprocessed scalar value; null remains distinct from an empty string.
64    #[must_use]
65    pub const fn value(&self) -> &Located<ComposeScalar> {
66        &self.value
67    }
68
69    /// Returns the complete mapping-entry span.
70    #[must_use]
71    pub const fn span(&self) -> SourceSpan {
72        self.span
73    }
74}
75
76/// A service environment with array or mapping syntax retained.
77#[derive(Debug, Clone, PartialEq, Eq)]
78pub enum Environment {
79    /// Array syntax.
80    List {
81        /// The complete sequence span.
82        span: SourceSpan,
83        /// Entries in authored order.
84        entries: Vec<EnvironmentListEntry>,
85    },
86    /// Mapping syntax.
87    Map {
88        /// The complete mapping span.
89        span: SourceSpan,
90        /// Entries in authored order.
91        entries: Vec<EnvironmentMapEntry>,
92    },
93}
94
95impl Environment {
96    /// Returns the complete environment value span.
97    #[must_use]
98    pub const fn span(&self) -> SourceSpan {
99        match self {
100            Self::List { span, .. } | Self::Map { span, .. } => *span,
101        }
102    }
103}