compose-lens 0.1.0

Loss-aware parsing, processing, validation, and rendering of Compose projects
Documentation
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
//! Typed service-volume mounts that preserve their authored syntax form.

use super::{BooleanValue, FieldReference, Located};
use crate::source::SourceSpan;

/// The authored Compose syntax form of a service-volume mount.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum VolumeSyntax {
    /// A colon-delimited scalar such as `./data:/var/lib/data:Z`.
    Short,
    /// A mapping with fields such as `type`, `source`, and `target`.
    Long,
}

/// A requested `SELinux` relabel mode.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum SelinuxRelabel {
    /// Shared relabeling, spelled `z`.
    Shared,
    /// Private relabeling, spelled `Z`.
    Private,
}

/// A container-side mount path classified independently of the host operating system.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ContainerPath {
    raw: String,
    kind: ContainerPathKind,
}

impl ContainerPath {
    fn parse(raw: String) -> Self {
        let kind = if raw.starts_with('/') {
            ContainerPathKind::UnixAbsolute
        } else if is_windows_drive_absolute(&raw) {
            ContainerPathKind::WindowsDriveAbsolute
        } else if raw.starts_with(r"\\") || raw.starts_with("//") {
            ContainerPathKind::WindowsUnc
        } else if raw.contains('$') {
            ContainerPathKind::Deferred
        } else {
            ContainerPathKind::Relative
        };
        Self { raw, kind }
    }

    /// Returns the target path without changing separators or spelling.
    #[must_use]
    pub fn raw(&self) -> &str {
        &self.raw
    }

    /// Returns the container-platform lexical path kind.
    #[must_use]
    pub const fn kind(&self) -> ContainerPathKind {
        self.kind
    }
}

/// The lexical family of a container-side path.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ContainerPathKind {
    /// A `/`-prefixed Unix container path.
    UnixAbsolute,
    /// A drive-letter Windows container path.
    WindowsDriveAbsolute,
    /// A Windows UNC container path.
    WindowsUnc,
    /// A relative path retained for later validation.
    Relative,
    /// A path whose shape depends on interpolation.
    Deferred,
}

/// A long-syntax service-volume mount type.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum MountType {
    /// A named or anonymous container volume.
    Volume,
    /// A host bind mount.
    Bind,
    /// An in-memory temporary filesystem.
    Tmpfs,
    /// A Windows named pipe.
    NamedPipe,
    /// An image-backed mount.
    Image,
    /// A cluster-managed mount.
    Cluster,
    /// A value not recognized by this `ComposeLens` release.
    Other(String),
}

impl MountType {
    pub(super) fn from_text(value: String) -> Self {
        match value.as_str() {
            "volume" => Self::Volume,
            "bind" => Self::Bind,
            "tmpfs" => Self::Tmpfs,
            "npipe" => Self::NamedPipe,
            "image" => Self::Image,
            "cluster" => Self::Cluster,
            _ => Self::Other(value),
        }
    }
}

/// A short-syntax service-volume mount.
///
/// `source`, `target`, and `options` are a conservative decomposition for common Compose
/// strings. [`Self::raw`] remains authoritative because platform-specific path grammars and
/// implementation extensions can make a short string ambiguous.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ShortVolumeMount {
    raw: Located<String>,
    source: Option<String>,
    target: Option<String>,
    target_path: Option<ContainerPath>,
    options: Vec<String>,
}

impl ShortVolumeMount {
    pub(crate) fn new(raw: Located<String>) -> Self {
        let (source, target, options) = split_short_volume(raw.value());
        Self {
            raw,
            source,
            target_path: target.as_ref().map(|value| ContainerPath::parse(value.clone())),
            target,
            options,
        }
    }

    /// Returns the unquoted semantic scalar and its source span.
    #[must_use]
    pub const fn raw(&self) -> &Located<String> {
        &self.raw
    }

    /// Returns the conservatively parsed source, if one was present.
    #[must_use]
    pub fn source(&self) -> Option<&str> {
        self.source.as_deref()
    }

    /// Returns the conservatively parsed container target.
    #[must_use]
    pub fn target(&self) -> Option<&str> {
        self.target.as_deref()
    }

    /// Returns the target classified using container-platform lexical rules.
    ///
    /// This does not consult the host operating system, so `/project/node_modules` remains an
    /// anonymous Unix-container target even when `ComposeLens` runs on Windows.
    #[must_use]
    pub const fn target_path(&self) -> Option<&ContainerPath> {
        self.target_path.as_ref()
    }

    /// Returns access-mode tokens in authored order.
    #[must_use]
    pub fn options(&self) -> &[String] {
        &self.options
    }

    /// Returns the requested `SELinux` relabel mode, if present.
    #[must_use]
    pub fn selinux_relabel(&self) -> Option<SelinuxRelabel> {
        self.options.iter().find_map(|option| match option.as_str() {
            "z" => Some(SelinuxRelabel::Shared),
            "Z" => Some(SelinuxRelabel::Private),
            _ => None,
        })
    }
}

/// Bind-specific options in a long-syntax mount.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BindOptions {
    span: SourceSpan,
    propagation: Option<Located<String>>,
    create_host_path: Option<Located<BooleanValue>>,
    selinux: Option<Located<SelinuxRelabel>>,
    extension_fields: Vec<FieldReference>,
    unknown_fields: Vec<FieldReference>,
}

impl BindOptions {
    pub(super) fn new(span: SourceSpan) -> Self {
        Self {
            span,
            propagation: None,
            create_host_path: None,
            selinux: None,
            extension_fields: Vec::new(),
            unknown_fields: Vec::new(),
        }
    }

    pub(super) fn set_propagation(&mut self, value: Located<String>) {
        self.propagation = Some(value);
    }

    pub(super) fn set_create_host_path(&mut self, value: Located<BooleanValue>) {
        self.create_host_path = Some(value);
    }

    pub(super) fn set_selinux(&mut self, value: Located<SelinuxRelabel>) {
        self.selinux = Some(value);
    }

    pub(super) fn push_extension(&mut self, field: FieldReference) {
        self.extension_fields.push(field);
    }

    pub(super) fn push_unknown(&mut self, field: FieldReference) {
        self.unknown_fields.push(field);
    }

    /// Returns the complete `bind` mapping span.
    #[must_use]
    pub const fn span(&self) -> SourceSpan {
        self.span
    }

    /// Returns the requested bind-propagation mode.
    #[must_use]
    pub const fn propagation(&self) -> Option<&Located<String>> {
        self.propagation.as_ref()
    }

    /// Returns the explicitly authored host-path creation setting.
    ///
    /// `None` means the field was omitted; it is deliberately not replaced by an
    /// implementation default during typed parsing.
    #[must_use]
    pub const fn create_host_path(&self) -> Option<&Located<BooleanValue>> {
        self.create_host_path.as_ref()
    }

    /// Returns the requested `SELinux` relabel mode.
    #[must_use]
    pub const fn selinux(&self) -> Option<&Located<SelinuxRelabel>> {
        self.selinux.as_ref()
    }

    /// Returns `x-` extension fields retained from the bind mapping.
    #[must_use]
    pub fn extension_fields(&self) -> &[FieldReference] {
        &self.extension_fields
    }

    /// Returns unrecognized bind fields retained from the source.
    #[must_use]
    pub fn unknown_fields(&self) -> &[FieldReference] {
        &self.unknown_fields
    }
}

/// A long-syntax service-volume mount.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LongVolumeMount {
    span: SourceSpan,
    mount_type: Option<Located<MountType>>,
    source: Option<Located<String>>,
    target: Option<Located<String>>,
    target_path: Option<Located<ContainerPath>>,
    read_only: Option<Located<BooleanValue>>,
    bind: Option<BindOptions>,
    extension_fields: Vec<FieldReference>,
    unknown_fields: Vec<FieldReference>,
}

impl LongVolumeMount {
    pub(super) fn new(span: SourceSpan) -> Self {
        Self {
            span,
            mount_type: None,
            source: None,
            target: None,
            target_path: None,
            read_only: None,
            bind: None,
            extension_fields: Vec::new(),
            unknown_fields: Vec::new(),
        }
    }

    pub(super) fn set_mount_type(&mut self, value: Located<MountType>) {
        self.mount_type = Some(value);
    }

    pub(super) fn set_source(&mut self, value: Located<String>) {
        self.source = Some(value);
    }

    pub(super) fn set_target(&mut self, value: Located<String>) {
        self.target_path = Some(Located::new(ContainerPath::parse(value.value.clone()), value.span));
        self.target = Some(value);
    }

    pub(super) fn set_read_only(&mut self, value: Located<BooleanValue>) {
        self.read_only = Some(value);
    }

    pub(super) fn set_bind(&mut self, value: BindOptions) {
        self.bind = Some(value);
    }

    pub(super) fn push_extension(&mut self, field: FieldReference) {
        self.extension_fields.push(field);
    }

    pub(super) fn push_unknown(&mut self, field: FieldReference) {
        self.unknown_fields.push(field);
    }

    /// Returns the complete mapping span.
    #[must_use]
    pub const fn span(&self) -> SourceSpan {
        self.span
    }

    /// Returns the explicitly authored mount type.
    #[must_use]
    pub const fn mount_type(&self) -> Option<&Located<MountType>> {
        self.mount_type.as_ref()
    }

    /// Returns the mount source.
    #[must_use]
    pub const fn source(&self) -> Option<&Located<String>> {
        self.source.as_ref()
    }

    /// Returns the container target.
    #[must_use]
    pub const fn target(&self) -> Option<&Located<String>> {
        self.target.as_ref()
    }

    /// Returns the target classified using container-platform lexical rules.
    #[must_use]
    pub const fn target_path(&self) -> Option<&Located<ContainerPath>> {
        self.target_path.as_ref()
    }

    /// Returns the explicitly authored read-only setting.
    #[must_use]
    pub const fn read_only(&self) -> Option<&Located<BooleanValue>> {
        self.read_only.as_ref()
    }

    /// Returns long-syntax bind options.
    #[must_use]
    pub const fn bind(&self) -> Option<&BindOptions> {
        self.bind.as_ref()
    }

    /// Returns `x-` extension fields retained from the mount mapping.
    #[must_use]
    pub fn extension_fields(&self) -> &[FieldReference] {
        &self.extension_fields
    }

    /// Returns recognized-by-Compose but not-yet-typed and unrecognized fields.
    #[must_use]
    pub fn unknown_fields(&self) -> &[FieldReference] {
        &self.unknown_fields
    }
}

/// A service-volume mount with its authored syntax form retained.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum VolumeMount {
    /// Colon-delimited short syntax.
    Short(ShortVolumeMount),
    /// Mapping-based long syntax.
    Long(Box<LongVolumeMount>),
}

impl VolumeMount {
    /// Returns the authored syntax form.
    #[must_use]
    pub const fn syntax(&self) -> VolumeSyntax {
        match self {
            Self::Short(_) => VolumeSyntax::Short,
            Self::Long(_) => VolumeSyntax::Long,
        }
    }

    /// Returns the complete source span of the mount value.
    #[must_use]
    pub const fn span(&self) -> SourceSpan {
        match self {
            Self::Short(value) => value.raw().span(),
            Self::Long(value) => value.span(),
        }
    }

    /// Returns the requested `SELinux` relabel mode without erasing syntax provenance.
    #[must_use]
    pub fn selinux_relabel(&self) -> Option<SelinuxRelabel> {
        match self {
            Self::Short(value) => value.selinux_relabel(),
            Self::Long(value) => value.bind()?.selinux().map(|mode| *mode.value()),
        }
    }
}

fn split_short_volume(value: &str) -> (Option<String>, Option<String>, Vec<String>) {
    let fields = split_colon_fields(value);
    match fields.as_slice() {
        [] => (None, None, Vec::new()),
        [target] => (None, Some((*target).to_owned()), Vec::new()),
        [source, target] => (Some((*source).to_owned()), Some((*target).to_owned()), Vec::new()),
        [source, middle @ .., options] => (
            Some((*source).to_owned()),
            Some(middle.join(":")),
            options.split(',').map(str::to_owned).collect(),
        ),
    }
}

fn split_colon_fields(value: &str) -> Vec<&str> {
    let mut fields = Vec::new();
    let mut start = 0;
    for (index, character) in value.char_indices() {
        if character != ':' || is_drive_separator(value, start, index) {
            continue;
        }
        fields.push(&value[start..index]);
        start = index + character.len_utf8();
    }
    fields.push(&value[start..]);
    fields
}

fn is_drive_separator(value: &str, field_start: usize, colon_index: usize) -> bool {
    let field = &value[field_start..colon_index];
    let next = value[colon_index + 1..].chars().next();
    field.len() == 1 && field.as_bytes()[0].is_ascii_alphabetic() && matches!(next, Some('/' | '\\'))
}

fn is_windows_drive_absolute(value: &str) -> bool {
    value.as_bytes().get(1) == Some(&b':')
        && value.as_bytes().first().is_some_and(u8::is_ascii_alphabetic)
        && matches!(value.as_bytes().get(2), Some(b'/' | b'\\'))
}

#[cfg(test)]
mod tests {
    use super::{ContainerPathKind, ShortVolumeMount, split_short_volume};
    use crate::model::Located;
    use crate::source::{SourceId, SourceSpan};

    #[test]
    fn conservatively_splits_linux_and_windows_short_mounts() {
        assert_eq!(
            split_short_volume("./data:/var/lib/data:Z,ro"),
            (
                Some("./data".to_owned()),
                Some("/var/lib/data".to_owned()),
                vec!["Z".to_owned(), "ro".to_owned()]
            )
        );
        assert_eq!(
            split_short_volume(r"C:\data:/var/lib/data:z"),
            (
                Some(r"C:\data".to_owned()),
                Some("/var/lib/data".to_owned()),
                vec!["z".to_owned()]
            )
        );
        assert_eq!(
            split_short_volume("cache:/cache"),
            (Some("cache".to_owned()), Some("/cache".to_owned()), Vec::new())
        );
    }

    #[test]
    fn classifies_anonymous_targets_without_host_path_apis() -> Result<(), &'static str> {
        let raw = "/project/node_modules";
        let span = SourceSpan::new(SourceId::new(1), 0, raw.len()).ok_or("valid test span expected")?;
        let mount = ShortVolumeMount::new(Located::new(raw.to_owned(), span));
        assert_eq!(mount.source(), None);
        assert_eq!(
            mount.target_path().map(super::ContainerPath::kind),
            Some(ContainerPathKind::UnixAbsolute)
        );
        Ok(())
    }
}