compose-lens 0.1.1

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
//! Compatibility-feature discovery and report generation.

use super::{CompatibilityClassification, CompatibilityFeature, CompatibilityProfile, CompatibilityRule};
use crate::diagnostic::{Diagnostic, DiagnosticCode, DiagnosticLabel, Severity};
use crate::merge::{MergeOperation, MergedEntry, MergedProject, MergedValue, MergedValueKind};
use crate::model::{Located, ShortExtraHost, ShortVolumeMount, UserNamespaceMode};
use crate::profiles::ProfileSelection;
use crate::resolution::{effective_span, selection_matches, service_in_scope};
use crate::source::SourceSpan;

/// A construct is unsupported by the selected compatibility profile.
pub const UNSUPPORTED_FEATURE: DiagnosticCode = DiagnosticCode::new("compose.compatibility.unsupported");

/// A construct relies on implementation-specific behavior.
pub const IMPLEMENTATION_SPECIFIC_FEATURE: DiagnosticCode =
    DiagnosticCode::new("compose.compatibility.implementation-specific");

/// Compatibility evidence is insufficient for the selected versions.
pub const UNKNOWN_FEATURE_SUPPORT: DiagnosticCode = DiagnosticCode::new("compose.compatibility.unknown");

/// A construct is deprecated by the selected compatibility profile.
pub const DEPRECATED_FEATURE: DiagnosticCode = DiagnosticCode::new("compose.compatibility.deprecated");

/// One source occurrence of a compatibility-sensitive construct.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CompatibilityOccurrence {
    feature: CompatibilityFeature,
    path: Vec<String>,
    source: SourceSpan,
    sensitive: bool,
}

impl CompatibilityOccurrence {
    /// Returns the detected feature.
    #[must_use]
    pub const fn feature(&self) -> CompatibilityFeature {
        self.feature
    }

    /// Returns semantic mapping/sequence path segments without embedding the authored value.
    #[must_use]
    pub fn path(&self) -> &[String] {
        &self.path
    }

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

    /// Reports whether the source value includes sensitive interpolation output.
    #[must_use]
    pub const fn is_sensitive(&self) -> bool {
        self.sensitive
    }
}

/// One compatibility occurrence and the selected profile's decision.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CompatibilityFinding {
    occurrence: CompatibilityOccurrence,
    rule: CompatibilityRule,
}

impl CompatibilityFinding {
    /// Returns the detected source occurrence.
    #[must_use]
    pub const fn occurrence(&self) -> &CompatibilityOccurrence {
        &self.occurrence
    }

    /// Returns the profile rule applied to that occurrence.
    #[must_use]
    pub const fn rule(&self) -> &CompatibilityRule {
        &self.rule
    }
}

/// A non-destructive compatibility assessment for one merged project view.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CompatibilityReport {
    profile: CompatibilityProfile,
    findings: Vec<CompatibilityFinding>,
    diagnostics: Vec<Diagnostic>,
}

impl CompatibilityReport {
    /// Returns the exact compatibility context selected by the caller.
    #[must_use]
    pub const fn profile(&self) -> CompatibilityProfile {
        self.profile
    }

    /// Returns all discovered constructs, including supported ones, in deterministic source order.
    #[must_use]
    pub fn findings(&self) -> &[CompatibilityFinding] {
        &self.findings
    }

    /// Returns compatibility diagnostics for non-portable, unknown, deprecated, or unsupported constructs.
    #[must_use]
    pub fn diagnostics(&self) -> &[Diagnostic] {
        &self.diagnostics
    }

    /// Reports whether compatibility assessment emitted no error diagnostics.
    #[must_use]
    pub fn is_valid(&self) -> bool {
        self.diagnostics
            .iter()
            .all(|diagnostic| diagnostic.severity() != Severity::Error)
    }
}

/// Detects and classifies compatibility-sensitive constructs in the selected project view.
#[must_use]
pub fn validate_compatibility(
    project: &MergedProject,
    selection: Option<&ProfileSelection>,
    profile: CompatibilityProfile,
) -> CompatibilityReport {
    let mut diagnostics = Vec::new();
    if !selection_matches(project, selection, &mut diagnostics) {
        return CompatibilityReport {
            profile,
            findings: Vec::new(),
            diagnostics,
        };
    }

    let occurrences = discover(project, selection);
    let findings = occurrences
        .into_iter()
        .map(|occurrence| {
            let rule = profile.classify(occurrence.feature);
            if let Some(severity) = rule.diagnostic_severity() {
                diagnostics.push(diagnostic(&occurrence, &rule, severity));
            }
            CompatibilityFinding { occurrence, rule }
        })
        .collect();
    CompatibilityReport {
        profile,
        findings,
        diagnostics,
    }
}

fn discover(project: &MergedProject, selection: Option<&ProfileSelection>) -> Vec<CompatibilityOccurrence> {
    let mut occurrences = Vec::new();
    let Some(root) = project.root().as_mapping() else {
        return occurrences;
    };
    let mut path = Vec::new();
    collect_operation(project.root(), &path, &mut occurrences);
    for entry in root {
        if entry.key() == "services" {
            collect_services(entry, selection, &mut path, &mut occurrences);
        } else {
            collect_entry(entry, &mut path, &mut occurrences);
        }
    }
    occurrences
}

fn collect_services(
    services: &MergedEntry,
    selection: Option<&ProfileSelection>,
    path: &mut Vec<String>,
    occurrences: &mut Vec<CompatibilityOccurrence>,
) {
    path.push("services".to_owned());
    collect_operation(services.value(), path, occurrences);
    if let Some(entries) = services.value().as_mapping() {
        for service in entries {
            if !service_in_scope(selection, service.key()) {
                continue;
            }
            path.push(service.key().to_owned());
            collect_service_features(service.value(), path, occurrences);
            collect_value(service.value(), path, occurrences);
            let _ = path.pop();
        }
    }
    let _ = path.pop();
}

fn collect_service_features(service: &MergedValue, path: &[String], occurrences: &mut Vec<CompatibilityOccurrence>) {
    if let Some(image) = service.get("image") {
        if let Some(scalar) = image.as_scalar() {
            if has_tag_and_digest(scalar.value()) {
                let mut image_path = path.to_owned();
                image_path.push("image".to_owned());
                occurrences.push(CompatibilityOccurrence {
                    feature: CompatibilityFeature::ImageTagAndDigest,
                    path: image_path,
                    source: effective_span(image),
                    sensitive: scalar.is_sensitive(),
                });
            }
        }
    }

    if let Some(userns_mode) = service.get("userns_mode") {
        if let Some(scalar) = userns_mode.as_scalar() {
            let source = effective_span(userns_mode);
            let mode = UserNamespaceMode::parse(Located::new(scalar.value().to_owned(), source));
            if mode.is_podman_specific() {
                let mut mode_path = path.to_owned();
                mode_path.push("userns_mode".to_owned());
                occurrences.push(CompatibilityOccurrence {
                    feature: CompatibilityFeature::PodmanUserNamespaceMode,
                    path: mode_path,
                    source,
                    sensitive: scalar.is_sensitive(),
                });
            }
        }
    }

    if let Some(extra_hosts) = service.get("extra_hosts") {
        let mut hosts_path = path.to_owned();
        hosts_path.push("extra_hosts".to_owned());
        collect_host_gateway(extra_hosts, &hosts_path, occurrences);
    }
    if let Some(extra_hosts) = service.get("build").and_then(|build| build.get("extra_hosts")) {
        let mut hosts_path = path.to_owned();
        hosts_path.push("build".to_owned());
        hosts_path.push("extra_hosts".to_owned());
        collect_host_gateway(extra_hosts, &hosts_path, occurrences);
    }

    let Some(volumes) = service.get("volumes").and_then(MergedValue::as_sequence) else {
        return;
    };
    for (index, volume) in volumes.iter().enumerate() {
        let mut volume_path = path.to_owned();
        volume_path.push("volumes".to_owned());
        volume_path.push(index.to_string());
        if let Some(scalar) = volume.as_scalar() {
            let source = effective_span(volume);
            let mount = ShortVolumeMount::new(Located::new(scalar.value().to_owned(), source));
            if mount.selinux_relabel().is_some() {
                occurrences.push(CompatibilityOccurrence {
                    feature: CompatibilityFeature::ShortBindSelinuxRelabel,
                    path: volume_path,
                    source,
                    sensitive: scalar.is_sensitive(),
                });
            }
            continue;
        }
        let Some(selinux) = volume.get("bind").and_then(|bind| bind.get("selinux")) else {
            continue;
        };
        volume_path.push("bind".to_owned());
        volume_path.push("selinux".to_owned());
        occurrences.push(CompatibilityOccurrence {
            feature: CompatibilityFeature::LongBindSelinuxRelabel,
            path: volume_path,
            source: effective_span(selinux),
            sensitive: selinux.is_sensitive(),
        });
    }
}

fn collect_host_gateway(extra_hosts: &MergedValue, path: &[String], occurrences: &mut Vec<CompatibilityOccurrence>) {
    if let Some(values) = extra_hosts.as_sequence() {
        for (index, value) in values.iter().enumerate() {
            let Some(scalar) = value.as_scalar() else {
                continue;
            };
            let source = effective_span(value);
            let entry = ShortExtraHost::parse(Located::new(scalar.value().to_owned(), source));
            if entry.address().is_some_and(crate::model::HostAddress::is_host_gateway) {
                let mut occurrence_path = path.to_owned();
                occurrence_path.push(index.to_string());
                occurrences.push(CompatibilityOccurrence {
                    feature: CompatibilityFeature::HostGatewayToken,
                    path: occurrence_path,
                    source,
                    sensitive: scalar.is_sensitive(),
                });
            }
        }
    } else if let Some(entries) = extra_hosts.as_mapping() {
        for entry in entries {
            let Some(address) = entry.value().as_scalar() else {
                continue;
            };
            if address.value() == "host-gateway" {
                let mut occurrence_path = path.to_owned();
                occurrence_path.push(entry.key().to_owned());
                occurrences.push(CompatibilityOccurrence {
                    feature: CompatibilityFeature::HostGatewayToken,
                    path: occurrence_path,
                    source: effective_span(entry.value()),
                    sensitive: address.is_sensitive(),
                });
            }
        }
    }
}

fn collect_entry(entry: &MergedEntry, path: &mut Vec<String>, occurrences: &mut Vec<CompatibilityOccurrence>) {
    path.push(entry.key().to_owned());
    if entry.key().starts_with("x-") {
        occurrences.push(CompatibilityOccurrence {
            feature: CompatibilityFeature::ExtensionField,
            path: path.clone(),
            source: entry
                .key_sources()
                .last()
                .copied()
                .unwrap_or_else(|| effective_span(entry.value())),
            sensitive: entry.value().is_sensitive(),
        });
    }
    collect_value(entry.value(), path, occurrences);
    let _ = path.pop();
}

fn collect_value(value: &MergedValue, path: &mut Vec<String>, occurrences: &mut Vec<CompatibilityOccurrence>) {
    collect_operation(value, path, occurrences);
    match value.kind() {
        MergedValueKind::Mapping(entries) => {
            for entry in entries {
                collect_entry(entry, path, occurrences);
            }
        }
        MergedValueKind::Sequence(values) => {
            for (index, value) in values.iter().enumerate() {
                path.push(index.to_string());
                collect_value(value, path, occurrences);
                let _ = path.pop();
            }
        }
        MergedValueKind::Tagged { value, .. } => collect_value(value, path, occurrences),
        MergedValueKind::Null(_) | MergedValueKind::Scalar(_) | MergedValueKind::Alias(_) => {}
    }
}

fn collect_operation(value: &MergedValue, path: &[String], occurrences: &mut Vec<CompatibilityOccurrence>) {
    let feature = match value.provenance().operation() {
        MergeOperation::Reset => Some(CompatibilityFeature::ResetTag),
        MergeOperation::Override => Some(CompatibilityFeature::OverrideTag),
        MergeOperation::Authored
        | MergeOperation::Added
        | MergeOperation::Replaced
        | MergeOperation::Merged
        | MergeOperation::Appended => None,
    };
    if let Some(feature) = feature {
        occurrences.push(CompatibilityOccurrence {
            feature,
            path: path.to_vec(),
            source: effective_span(value),
            sensitive: value.is_sensitive(),
        });
    }
}

fn has_tag_and_digest(value: &str) -> bool {
    let Some((name_and_tag, _)) = value.split_once('@') else {
        return false;
    };
    let last_slash = name_and_tag.rfind('/');
    name_and_tag
        .rfind(':')
        .is_some_and(|separator| last_slash.is_none_or(|slash| separator > slash))
}

fn diagnostic(occurrence: &CompatibilityOccurrence, rule: &CompatibilityRule, severity: Severity) -> Diagnostic {
    let (code, message) = match rule.classification() {
        CompatibilityClassification::ImplementationSpecific => (
            IMPLEMENTATION_SPECIFIC_FEATURE,
            "construct has implementation-specific compatibility",
        ),
        CompatibilityClassification::Deprecated => (
            DEPRECATED_FEATURE,
            "construct is deprecated by the compatibility profile",
        ),
        CompatibilityClassification::Unsupported => (
            UNSUPPORTED_FEATURE,
            "construct is unsupported by the compatibility profile",
        ),
        CompatibilityClassification::Unknown => (
            UNKNOWN_FEATURE_SUPPORT,
            "construct has no established support for the compatibility profile",
        ),
        CompatibilityClassification::Supported | CompatibilityClassification::Extension => (
            UNKNOWN_FEATURE_SUPPORT,
            "construct has an unexpected compatibility diagnostic",
        ),
    };
    Diagnostic::new(code, severity, message)
        .with_label(DiagnosticLabel::primary(
            occurrence.source,
            "compatibility-sensitive construct",
        ))
        .with_note(rule.explanation())
}