boxferry-engine 0.6.0

Conversion planning, policy, capability, and diagnostic engine for BoxFerry
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
//! Structured diagnostics with explicit sensitive fields.

use std::{error::Error, fmt};

/// Producer-neutral role of one source location attached to a native finding.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum NativeFindingLabelKind {
    /// Location primarily responsible for the finding.
    Primary,
    /// Related location that adds context.
    Secondary,
}

/// One value-free source location attached to a native-format finding.
///
/// Numeric source identities are invocation-local. Adapters map them to caller-owned aliases;
/// source paths and source contents never enter this DTO.
#[derive(Clone, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub struct NativeFindingLabel {
    kind: NativeFindingLabelKind,
    source_id: u32,
    start: usize,
    end: usize,
    message: String,
}

impl NativeFindingLabel {
    /// Creates a labelled half-open byte range.
    #[must_use]
    pub fn new(
        kind: NativeFindingLabelKind,
        source_id: u32,
        start: usize,
        end: usize,
        message: impl Into<String>,
    ) -> Self {
        Self {
            kind,
            source_id,
            start,
            end,
            message: message.into(),
        }
    }

    /// Returns the location role.
    #[must_use]
    pub const fn kind(&self) -> NativeFindingLabelKind {
        self.kind
    }

    /// Returns the invocation-local numeric source identity.
    #[must_use]
    pub const fn source_id(&self) -> u32 {
        self.source_id
    }

    /// Returns the inclusive byte offset.
    #[must_use]
    pub const fn start(&self) -> usize {
        self.start
    }

    /// Returns the exclusive byte offset.
    #[must_use]
    pub const fn end(&self) -> usize {
        self.end
    }

    /// Returns the value-free location explanation.
    #[must_use]
    pub fn message(&self) -> &str {
        &self.message
    }
}

/// Error returned for an invalid machine-readable diagnostic code.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct InvalidDiagnosticCode;

impl fmt::Display for InvalidDiagnosticCode {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str("diagnostic code must contain only uppercase ASCII letters and digits")
    }
}

impl Error for InvalidDiagnosticCode {}

/// Stable machine-readable diagnostic identifier.
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct DiagnosticCode(String);

impl DiagnosticCode {
    /// Creates an uppercase ASCII alphanumeric code such as `BFE0001`.
    ///
    /// # Errors
    ///
    /// Returns [`InvalidDiagnosticCode`] for an empty or nonconforming code.
    pub fn new(value: impl Into<String>) -> Result<Self, InvalidDiagnosticCode> {
        let value = value.into();
        if value.is_empty()
            || !value
                .bytes()
                .all(|byte| byte.is_ascii_uppercase() || byte.is_ascii_digit())
        {
            return Err(InvalidDiagnosticCode);
        }
        Ok(Self(value))
    }

    /// Returns the code string.
    #[must_use]
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

/// Diagnostic severity independent from presentation and process exit codes.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum Severity {
    /// Conversion cannot continue safely.
    Error,
    /// Conversion can continue only under an explicit loss policy.
    Warning,
    /// Context that does not change conversion fidelity.
    Note,
}

/// Plain or sensitive diagnostic field value.
#[derive(Clone, Eq, PartialEq)]
#[non_exhaustive]
pub enum DiagnosticValue {
    /// Non-sensitive text that presentation may show.
    Plain(String),
    /// Sensitive text that presentation and debug output must redact.
    Sensitive(String),
}

impl DiagnosticValue {
    /// Creates a non-sensitive value.
    #[must_use]
    pub fn plain(value: impl Into<String>) -> Self {
        Self::Plain(value.into())
    }

    /// Creates a sensitive value.
    #[must_use]
    pub fn sensitive(value: impl Into<String>) -> Self {
        Self::Sensitive(value.into())
    }

    /// Returns whether the value is sensitive.
    #[must_use]
    pub const fn is_sensitive(&self) -> bool {
        matches!(self, Self::Sensitive(_))
    }

    /// Explicitly exposes the original field value.
    #[must_use]
    pub fn expose(&self) -> &str {
        match self {
            Self::Plain(value) | Self::Sensitive(value) => value,
        }
    }

    /// Returns presentation-safe text.
    #[must_use]
    pub fn redacted(&self) -> &str {
        match self {
            Self::Plain(value) => value,
            Self::Sensitive(_) => "[REDACTED]",
        }
    }
}

impl fmt::Debug for DiagnosticValue {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_tuple("DiagnosticValue")
            .field(&self.redacted())
            .finish()
    }
}

/// Named structured context attached to a diagnostic.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct DiagnosticField {
    name: String,
    value: DiagnosticValue,
}

/// A native parser, model, runtime, or platform finding retained at an adapter boundary.
///
/// This envelope is intentionally independent of Compose, Quadlet, Docker, Podman, and
/// Kubernetes types. Native libraries keep ownership of their codes; `BoxFerry` retains those codes
/// as provenance while applying its own rule and loss policy in [`Diagnostic`].
#[derive(Clone, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub struct NativeFinding {
    source_format: String,
    producer: String,
    producer_version: Option<String>,
    code: String,
    stage: String,
    severity: Severity,
    summary: String,
    fields: Vec<DiagnosticField>,
    labels: Vec<NativeFindingLabel>,
    notes: Vec<String>,
    help: Option<String>,
}

impl NativeFinding {
    /// Creates a value-free native finding.
    #[must_use]
    pub fn new(
        source_format: impl Into<String>,
        producer: impl Into<String>,
        code: impl Into<String>,
        stage: impl Into<String>,
        severity: Severity,
        summary: impl Into<String>,
    ) -> Self {
        Self {
            source_format: source_format.into(),
            producer: producer.into(),
            producer_version: None,
            code: code.into(),
            stage: stage.into(),
            severity,
            summary: summary.into(),
            fields: Vec::new(),
            labels: Vec::new(),
            notes: Vec::new(),
            help: None,
        }
    }

    /// Attaches the exact native producer version when the adapter can prove it.
    #[must_use]
    pub fn with_producer_version(mut self, version: impl Into<String>) -> Self {
        self.producer_version = Some(version.into());
        self
    }

    /// Appends protected structured native context.
    #[must_use]
    pub fn with_field(mut self, field: DiagnosticField) -> Self {
        self.fields.push(field);
        self
    }

    /// Appends a value-free labelled location.
    #[must_use]
    pub fn with_label(mut self, label: NativeFindingLabel) -> Self {
        self.labels.push(label);
        self
    }

    /// Appends value-free native context.
    #[must_use]
    pub fn with_note(mut self, note: impl Into<String>) -> Self {
        self.notes.push(note.into());
        self
    }

    /// Attaches native remediation distinct from the `BoxFerry` rule help.
    #[must_use]
    pub fn with_help(mut self, help: impl Into<String>) -> Self {
        self.help = Some(help.into());
        self
    }

    /// Returns the source format, such as `compose` or `quadlet`.
    #[must_use]
    pub fn source_format(&self) -> &str {
        &self.source_format
    }

    /// Returns the native producer, such as `compose-lens`.
    #[must_use]
    pub fn producer(&self) -> &str {
        &self.producer
    }

    /// Returns the exact producer version when recorded.
    #[must_use]
    pub fn producer_version(&self) -> Option<&str> {
        self.producer_version.as_deref()
    }

    /// Returns the producer-owned stable code.
    #[must_use]
    pub fn code(&self) -> &str {
        &self.code
    }

    /// Returns the native processing stage.
    #[must_use]
    pub fn stage(&self) -> &str {
        &self.stage
    }

    /// Returns the native severity.
    #[must_use]
    pub const fn severity(&self) -> Severity {
        self.severity
    }

    /// Returns the value-free native summary.
    #[must_use]
    pub fn summary(&self) -> &str {
        &self.summary
    }

    /// Returns protected structured native context.
    #[must_use]
    pub fn fields(&self) -> &[DiagnosticField] {
        &self.fields
    }

    /// Returns labelled native locations in producer order.
    #[must_use]
    pub fn labels(&self) -> &[NativeFindingLabel] {
        &self.labels
    }

    /// Returns value-free producer notes.
    #[must_use]
    pub fn notes(&self) -> &[String] {
        &self.notes
    }

    /// Returns producer remediation when recorded.
    #[must_use]
    pub fn help(&self) -> Option<&str> {
        self.help.as_deref()
    }
}

impl DiagnosticField {
    /// Creates a named field.
    #[must_use]
    pub fn new(name: impl Into<String>, value: DiagnosticValue) -> Self {
        Self {
            name: name.into(),
            value,
        }
    }

    /// Returns the field name.
    #[must_use]
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Returns the protected field value.
    #[must_use]
    pub const fn value(&self) -> &DiagnosticValue {
        &self.value
    }
}

/// Structured conversion diagnostic.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Diagnostic {
    code: DiagnosticCode,
    severity: Severity,
    summary: String,
    fields: Vec<DiagnosticField>,
    native_finding: Option<NativeFinding>,
}

impl Diagnostic {
    /// Creates a value-free summary with no fields.
    #[must_use]
    pub fn new(code: DiagnosticCode, severity: Severity, summary: impl Into<String>) -> Self {
        Self {
            code,
            severity,
            summary: summary.into(),
            fields: Vec::new(),
            native_finding: None,
        }
    }

    /// Appends structured context in presentation order.
    #[must_use]
    pub fn with_field(mut self, field: DiagnosticField) -> Self {
        self.fields.push(field);
        self
    }

    /// Attaches the native finding that caused this `BoxFerry` rule occurrence.
    #[must_use]
    pub fn with_native_finding(mut self, finding: NativeFinding) -> Self {
        self.native_finding = Some(finding);
        self
    }

    /// Returns the stable code.
    #[must_use]
    pub const fn code(&self) -> &DiagnosticCode {
        &self.code
    }

    /// Returns the severity.
    #[must_use]
    pub const fn severity(&self) -> Severity {
        self.severity
    }

    /// Returns the human-readable, value-free summary.
    #[must_use]
    pub fn summary(&self) -> &str {
        &self.summary
    }

    /// Returns structured fields in presentation order.
    #[must_use]
    pub fn fields(&self) -> &[DiagnosticField] {
        &self.fields
    }

    /// Returns the native finding retained by the source adapter.
    #[must_use]
    pub const fn native_finding(&self) -> Option<&NativeFinding> {
        self.native_finding.as_ref()
    }
}

impl fmt::Display for Diagnostic {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(formatter, "{}: {}", self.code.as_str(), self.summary)?;
        for field in &self.fields {
            write!(formatter, " {}={}", field.name(), field.value().redacted())?;
        }
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::{
        Diagnostic, DiagnosticCode, DiagnosticField, DiagnosticValue, NativeFinding, NativeFindingLabel,
        NativeFindingLabelKind, Severity,
    };

    #[test]
    fn sensitive_fields_are_redacted_from_debug_and_display() -> Result<(), String> {
        let finding = NativeFinding::new(
            "compose",
            "compose-lens",
            "compose.example",
            "model",
            Severity::Warning,
            "native value needs review",
        )
        .with_field(DiagnosticField::new(
            "native_value",
            DiagnosticValue::sensitive("never-print-native-this"),
        ))
        .with_label(NativeFindingLabel::new(
            NativeFindingLabelKind::Primary,
            1,
            4,
            8,
            "value is here",
        ));
        let diagnostic = Diagnostic::new(code("BFE0001")?, Severity::Warning, "value was adjusted")
            .with_field(DiagnosticField::new(
                "value",
                DiagnosticValue::sensitive("never-print-this"),
            ))
            .with_native_finding(finding);
        for rendered in [format!("{diagnostic:?}"), diagnostic.to_string()] {
            assert!(!rendered.contains("never-print-this"));
            assert!(!rendered.contains("never-print-native-this"));
            assert!(rendered.contains("[REDACTED]"));
        }
        let native = diagnostic.native_finding().ok_or("missing native finding")?;
        assert_eq!(native.producer(), "compose-lens");
        assert_eq!(native.labels()[0].source_id(), 1);
        Ok(())
    }

    fn code(value: &str) -> Result<DiagnosticCode, String> {
        DiagnosticCode::new(value).map_err(|error| error.to_string())
    }
}