Skip to main content

compose_lens/model/
logging.rs

1//! Source-aware service logging configuration.
2
3use crate::source::SourceSpan;
4
5use super::{FieldReference, Located};
6
7/// An authored logging-option scalar with its exact Compose-supported YAML kind retained.
8#[derive(Debug, Clone, PartialEq, Eq)]
9#[non_exhaustive]
10pub enum LoggingOptionValue {
11    /// A YAML string scalar, including interpolation-shaped text.
12    String(String),
13    /// A YAML number scalar with exact semantic spelling retained.
14    Number(String),
15    /// An explicit or empty YAML null.
16    Null,
17}
18
19/// One source-aware logging option entry.
20#[derive(Debug, Clone, PartialEq, Eq)]
21pub struct LoggingOption {
22    name: Located<String>,
23    value: Located<LoggingOptionValue>,
24    span: SourceSpan,
25}
26
27impl LoggingOption {
28    pub(crate) const fn new(name: Located<String>, value: Located<LoggingOptionValue>, span: SourceSpan) -> Self {
29        Self { name, value, span }
30    }
31
32    /// Returns the non-empty option key and its exact source span.
33    #[must_use]
34    pub const fn name(&self) -> &Located<String> {
35        &self.name
36    }
37
38    /// Returns the exact string, number, or null value and its source span.
39    #[must_use]
40    pub const fn value(&self) -> &Located<LoggingOptionValue> {
41        &self.value
42    }
43
44    /// Returns the complete key/value entry span.
45    #[must_use]
46    pub const fn span(&self) -> SourceSpan {
47        self.span
48    }
49}
50
51/// An ordered service logging-options mapping, including an explicitly empty mapping.
52#[derive(Debug, Clone, PartialEq, Eq)]
53pub struct LoggingOptions {
54    span: SourceSpan,
55    entries: Vec<LoggingOption>,
56    unmodeled_entries: Vec<FieldReference>,
57}
58
59impl LoggingOptions {
60    pub(crate) const fn new(
61        span: SourceSpan,
62        entries: Vec<LoggingOption>,
63        unmodeled_entries: Vec<FieldReference>,
64    ) -> Self {
65        Self {
66            span,
67            entries,
68            unmodeled_entries,
69        }
70    }
71
72    /// Returns the exact span of the authored options mapping.
73    #[must_use]
74    pub const fn span(&self) -> SourceSpan {
75        self.span
76    }
77
78    /// Returns valid option entries in authored order.
79    #[must_use]
80    pub fn entries(&self) -> &[LoggingOption] {
81        &self.entries
82    }
83
84    /// Returns malformed option entries retained for source-aware recovery.
85    #[must_use]
86    pub fn unmodeled_entries(&self) -> &[FieldReference] {
87        &self.unmodeled_entries
88    }
89}
90
91/// An explicitly authored service-level Compose `logging` mapping.
92#[derive(Debug, Clone, PartialEq, Eq)]
93pub struct Logging {
94    span: SourceSpan,
95    driver: Option<Located<String>>,
96    options: Option<LoggingOptions>,
97    extension_fields: Vec<FieldReference>,
98    unknown_fields: Vec<FieldReference>,
99}
100
101impl Logging {
102    pub(crate) const fn new(span: SourceSpan) -> Self {
103        Self {
104            span,
105            driver: None,
106            options: None,
107            extension_fields: Vec::new(),
108            unknown_fields: Vec::new(),
109        }
110    }
111
112    pub(crate) fn set_driver(&mut self, driver: Located<String>) {
113        self.driver = Some(driver);
114    }
115
116    pub(crate) fn set_options(&mut self, options: LoggingOptions) {
117        self.options = Some(options);
118    }
119
120    pub(crate) fn push_extension(&mut self, field: FieldReference) {
121        self.extension_fields.push(field);
122    }
123
124    pub(crate) fn push_unknown(&mut self, field: FieldReference) {
125        self.unknown_fields.push(field);
126    }
127
128    /// Returns the exact span of the complete authored logging mapping.
129    #[must_use]
130    pub const fn span(&self) -> SourceSpan {
131        self.span
132    }
133
134    /// Returns the exact uninterpreted string driver when validly authored.
135    #[must_use]
136    pub const fn driver(&self) -> Option<&Located<String>> {
137        self.driver.as_ref()
138    }
139
140    /// Returns the ordered options mapping, including an explicitly empty one.
141    #[must_use]
142    pub const fn options(&self) -> Option<&LoggingOptions> {
143        self.options.as_ref()
144    }
145
146    /// Returns retained `x-*` fields from the logging mapping.
147    #[must_use]
148    pub fn extension_fields(&self) -> &[FieldReference] {
149        &self.extension_fields
150    }
151
152    /// Returns unrecognized fields from the logging mapping.
153    #[must_use]
154    pub fn unknown_fields(&self) -> &[FieldReference] {
155        &self.unknown_fields
156    }
157}