1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
//! Raw-preserving service resource and namespace values.
use super::{Located, MemLimitUnit};
use crate::source::SourceSpan;
/// A malformed item retained from a sequence that requires YAML string scalars.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct InvalidServiceStringItem {
span: SourceSpan,
}
impl InvalidServiceStringItem {
pub(crate) const fn new(span: SourceSpan) -> Self {
Self { span }
}
/// Returns the exact malformed item's source span.
#[must_use]
pub const fn span(self) -> SourceSpan {
self.span
}
}
/// The raw spelling of a service integer setting, with a conservative validity classification.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum ServiceInteger {
/// An integral YAML scalar in the documented range for its field.
Valid(String),
/// An integral YAML scalar retained outside the field's documented range.
OutOfRange(String),
/// A string or scalar expression retained without numeric coercion.
Other(String),
}
impl ServiceInteger {
pub(crate) fn parse(value: String, min: i128, max: i128) -> Self {
match value.parse::<i128>() {
Ok(number) if (min..=max).contains(&number) => Self::Valid(value),
Ok(_) => Self::OutOfRange(value),
Err(_) => Self::Other(value),
}
}
/// Returns whether this is a documented in-range integer spelling.
#[must_use]
pub const fn is_valid(&self) -> bool {
matches!(self, Self::Valid(_))
}
}
/// A service-level Compose `memswap_limit` value with its authored scalar retained.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MemswapLimit {
raw: Located<String>,
scalar_kind: MemswapLimitScalarKind,
kind: MemswapLimitKind,
}
impl MemswapLimit {
pub(crate) fn parse(raw: Located<String>, scalar_kind: MemswapLimitScalarKind) -> Self {
let value = raw.value();
let kind = if value == "-1" {
MemswapLimitKind::Unlimited
} else if matches!(scalar_kind, MemswapLimitScalarKind::String) && value.contains('$') {
MemswapLimitKind::Expression
} else if let Some((amount_raw, unit)) = quantity_parts(value) {
if amount_raw.bytes().all(|byte| byte == b'0') {
MemswapLimitKind::Zero {
amount_raw: amount_raw.to_owned(),
unit,
}
} else {
MemswapLimitKind::Positive {
amount_raw: amount_raw.to_owned(),
unit,
}
}
} else {
MemswapLimitKind::Other(value.to_owned())
};
Self { raw, scalar_kind, kind }
}
/// Returns the complete scalar value and its source span without normalization.
#[must_use]
pub const fn raw(&self) -> &Located<String> {
&self.raw
}
/// Returns whether the authored YAML scalar was a number or string.
#[must_use]
pub const fn scalar_kind(&self) -> MemswapLimitScalarKind {
self.scalar_kind
}
/// Returns the raw-preserving memory-plus-swap classification.
#[must_use]
pub const fn kind(&self) -> &MemswapLimitKind {
&self.kind
}
pub(crate) const fn is_positive(&self) -> bool {
matches!(self.kind, MemswapLimitKind::Positive { .. })
}
}
/// The YAML scalar category of an authored service memory-plus-swap limit.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum MemswapLimitScalarKind {
/// A YAML integer or floating-point scalar.
Number,
/// A YAML string scalar, including quoted numeric spelling.
String,
}
/// The raw-preserving semantic family of a service memory-plus-swap limit.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum MemswapLimitKind {
/// Compose's explicit unlimited `-1` spelling.
Unlimited,
/// An all-zero quantity, kept distinct from omitted and unlimited values.
Zero {
/// Exact amount spelling before the optional documented unit.
amount_raw: String,
/// The documented suffix when present.
unit: Option<MemLimitUnit>,
},
/// A positive decimal quantity with an optional documented unit.
Positive {
/// Exact amount spelling before the optional documented unit.
amount_raw: String,
/// The documented suffix when present.
unit: Option<MemLimitUnit>,
},
/// A dollar-bearing string deferred to Compose interpolation.
Expression,
/// A malformed or provider-specific spelling retained for inspection.
Other(String),
}
fn quantity_parts(value: &str) -> Option<(&str, Option<MemLimitUnit>)> {
let with_unit = [
("kb", MemLimitUnit::Kb),
("mb", MemLimitUnit::Mb),
("gb", MemLimitUnit::Gb),
("b", MemLimitUnit::B),
("k", MemLimitUnit::K),
("m", MemLimitUnit::M),
("g", MemLimitUnit::G),
]
.into_iter()
.find_map(|(suffix, unit)| value.strip_suffix(suffix).map(|amount| (amount, Some(unit))));
let (amount, unit) = with_unit.unwrap_or((value, None));
(!amount.is_empty() && amount.bytes().all(|byte| byte.is_ascii_digit())).then_some((amount, unit))
}
/// A raw decimal CPU allocation spelling.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum Cpus {
/// A decimal spelling, including `0.000`.
Decimal(String),
/// A deferred interpolation expression.
Expression(String),
/// A retained non-decimal spelling.
Other(String),
}
impl Cpus {
pub(crate) fn parse(value: String) -> Self {
if value.contains('$') {
return Self::Expression(value);
}
if !value.is_empty()
&& value.bytes().all(|byte| byte.is_ascii_digit() || byte == b'.')
&& value.bytes().filter(|byte| *byte == b'.').count() <= 1
{
Self::Decimal(value)
} else {
Self::Other(value)
}
}
/// Returns whether the spelling is decimal or deferred.
#[must_use]
pub const fn is_valid(&self) -> bool {
!matches!(self, Self::Other(_))
}
}
/// A service IPC mode retaining recognized portable service references separately.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum IpcMode {
/// A documented shareable IPC mode.
Shareable,
/// A local service namespace reference.
Service(String),
/// Any other scalar spelling retained as evidence.
Raw(String),
}
impl IpcMode {
pub(crate) fn parse(value: String) -> Self {
if value == "shareable" {
Self::Shareable
} else if let Some(name) = value.strip_prefix("service:") {
Self::Service(name.to_owned())
} else {
Self::Raw(value)
}
}
}
/// A service network mode retaining local namespace reference shapes.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum NetworkMode {
/// No network namespace.
None,
/// The host network namespace.
Host,
/// A local service namespace reference.
Service(String),
/// A container namespace reference.
Container(String),
/// An unclassified raw scalar.
Raw(String),
}
impl NetworkMode {
pub(crate) fn parse(value: String) -> Self {
match value.as_str() {
"none" => Self::None,
"host" => Self::Host,
_ if value.starts_with("service:") => Self::Service(value[8..].to_owned()),
_ if value.starts_with("container:") => Self::Container(value[10..].to_owned()),
_ => Self::Raw(value),
}
}
}
/// A PID namespace mode retaining local reference-shaped forms without runtime interpretation.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum PidMode {
/// A service namespace reference.
Service(String),
/// A container namespace reference.
Container(String),
/// Any other raw scalar.
Raw(String),
}
impl PidMode {
pub(crate) fn parse(value: String) -> Self {
if let Some(name) = value.strip_prefix("service:") {
Self::Service(name.to_owned())
} else if let Some(name) = value.strip_prefix("container:") {
Self::Container(name.to_owned())
} else {
Self::Raw(value)
}
}
}
/// A raw `volumes_from` entry with its default access mode made explicit.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct VolumesFrom {
raw: Located<String>,
source: String,
read_only: bool,
}
impl VolumesFrom {
pub(crate) fn parse(raw: Located<String>) -> Self {
let value = raw.value();
let (source, read_only) = match value.rsplit_once(':') {
Some((source, "ro")) => (source.to_owned(), true),
Some((source, "rw")) => (source.to_owned(), false),
_ => (value.to_owned(), false),
};
Self { raw, source, read_only }
}
/// Returns the original complete entry and span.
#[must_use]
pub const fn raw(&self) -> &Located<String> {
&self.raw
}
/// Returns the referenced service or container spelling.
#[must_use]
pub fn source(&self) -> &str {
&self.source
}
/// Returns whether `ro` was requested; omitted access defaults to `rw`.
#[must_use]
pub const fn read_only(&self) -> bool {
self.read_only
}
}