quadlet-lens 0.1.13

Loss-aware parsing, validation, and rendering of version-aware Quadlet documents
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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
//! Validated programmatic construction of deterministic Quadlet documents.
//!
//! This module owns native section and key spelling, repeated-entry rules, physical-line safety,
//! deterministic section order, and parse-back validation. Entry values remain exact authored
//! values: callers are responsible for key-specific semantic validity, while future focused value
//! encoders can provide stronger construction APIs without changing the document builder.

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

use crate::{
    diagnostic::Diagnostic,
    model::{
        BuildKey, ContainerKey, EntryKind, ImageKey, NetworkKey, PodKey, QuadletDocument, QuadletParseResult,
        QuadletUnitType, SectionKind, TypedModelError, VolumeKey,
    },
    source::SourceId,
};

/// An exact, single-physical-line native Quadlet value.
///
/// The value is retained exactly. It may contain native systemd quoting and specifiers, but it may
/// not contain line endings or NUL bytes. This type enforces physical-line safety only; it does
/// not validate command arguments, environment assignments, lifecycle values, mount options, or
/// other key-specific semantics.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct EntryValue(String);

impl EntryValue {
    /// Creates an exact native value that fits on one physical line.
    ///
    /// # Errors
    ///
    /// Returns [`RenderError::InvalidValue`] when the value contains a line ending or NUL byte.
    pub fn new(value: impl Into<String>) -> Result<Self, RenderError> {
        let value = value.into();
        if value.bytes().any(|byte| matches!(byte, 0 | b'\n' | b'\r')) {
            return Err(RenderError::InvalidValue);
        }
        Ok(Self(value))
    }

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

/// Safely constructible process-ID limit for a container.
///
/// This helper covers only the documented unlimited spelling (`-1`) and positive finite values
/// written as nonzero ASCII decimal text. It deliberately does not parse the decimal into a Rust
/// integer, so large values and leading zeros retain their exact spelling without overflow.
/// Parsed and raw [`EntryValue`] inputs remain uninterpreted, so authored zero and noncanonical
/// values can still be preserved.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct PidsLimit(String);

impl PidsLimit {
    /// Creates an unlimited process-ID limit rendered as `-1`.
    #[must_use]
    pub fn unlimited() -> Self {
        Self("-1".to_owned())
    }

    /// Creates a positive finite process-ID limit from exact ASCII decimal spelling.
    ///
    /// # Errors
    ///
    /// Returns [`PidsLimitError::Empty`] for empty text, [`PidsLimitError::NonDecimal`] for any
    /// non-ASCII-digit byte, and [`PidsLimitError::Zero`] when every digit is zero.
    pub fn finite(limit: impl Into<String>) -> Result<Self, PidsLimitError> {
        let limit = limit.into();
        if limit.is_empty() {
            return Err(PidsLimitError::Empty);
        }
        if !limit.bytes().all(|byte| byte.is_ascii_digit()) {
            return Err(PidsLimitError::NonDecimal);
        }
        if !limit.bytes().any(|byte| byte != b'0') {
            return Err(PidsLimitError::Zero);
        }
        Ok(Self(limit))
    }

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

impl From<PidsLimit> for EntryValue {
    fn from(limit: PidsLimit) -> Self {
        Self(limit.0)
    }
}

/// Invalid input to [`PidsLimit::finite`].
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum PidsLimitError {
    /// The finite decimal spelling is empty.
    Empty,
    /// The finite spelling contains a byte other than an ASCII decimal digit.
    NonDecimal,
    /// Zero is deliberately outside the typed construction contract.
    Zero,
}

impl fmt::Display for PidsLimitError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Empty => formatter.write_str("a finite process-ID limit must not be empty"),
            Self::NonDecimal => formatter.write_str("a finite process-ID limit must contain only ASCII decimal digits"),
            Self::Zero => formatter.write_str("a finite process-ID limit must be positive"),
        }
    }
}

impl Error for PidsLimitError {}

/// Safely constructible native shared-memory size for a container or pod.
///
/// The exact spelling is retained without parsing into a machine integer. Accepted values contain
/// a non-negative ASCII-decimal amount followed by no unit or one lowercase native unit: `b`,
/// `k`, `m`, or `g`. Leading zeros and arbitrary-precision amounts remain unchanged. Parsed and
/// raw [`EntryValue`] inputs remain uninterpreted.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ShmSize(String);

impl ShmSize {
    /// Creates a shared-memory size from exact native spelling.
    ///
    /// # Errors
    ///
    /// Returns [`ShmSizeError::Empty`] for empty text and [`ShmSizeError::InvalidFormat`] unless
    /// the value is an ASCII-decimal amount with an optional lowercase `b`, `k`, `m`, or `g` unit.
    pub fn new(size: impl Into<String>) -> Result<Self, ShmSizeError> {
        let size = size.into();
        if size.is_empty() {
            return Err(ShmSizeError::Empty);
        }
        let amount = match size.as_bytes().last() {
            Some(b'b' | b'k' | b'm' | b'g') => &size[..size.len() - 1],
            _ => size.as_str(),
        };
        if amount.is_empty() || !amount.bytes().all(|byte| byte.is_ascii_digit()) {
            return Err(ShmSizeError::InvalidFormat);
        }
        Ok(Self(size))
    }

    /// Creates Podman's documented explicit unlimited shared-memory value, `0`.
    #[must_use]
    pub fn unlimited() -> Self {
        Self("0".to_owned())
    }

    /// Returns whether the exact spelling denotes a zero amount, Podman's documented unlimited value.
    #[must_use]
    pub fn is_unlimited(&self) -> bool {
        let amount = match self.0.as_bytes().last() {
            Some(b'b' | b'k' | b'm' | b'g') => &self.0[..self.0.len() - 1],
            _ => self.0.as_str(),
        };
        amount.bytes().all(|byte| byte == b'0')
    }

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

impl From<ShmSize> for EntryValue {
    fn from(size: ShmSize) -> Self {
        Self(size.0)
    }
}

/// Invalid input to [`ShmSize::new`].
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum ShmSizeError {
    /// The shared-memory size is empty.
    Empty,
    /// The value is not an ASCII-decimal amount with an optional supported lowercase unit.
    InvalidFormat,
}

impl fmt::Display for ShmSizeError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Empty => formatter.write_str("a shared-memory size must not be empty"),
            Self::InvalidFormat => formatter
                .write_str("a shared-memory size must be an ASCII decimal amount with optional unit b, k, m, or g"),
        }
    }
}

impl Error for ShmSizeError {}

/// Safely constructible native memory limit for a container.
///
/// The exact spelling is retained without parsing into a machine integer. Accepted values contain
/// a positive ASCII-decimal amount followed by no unit or one lowercase native unit: `b`, `k`,
/// `m`, or `g`. Leading zeros and arbitrary-precision amounts remain unchanged. Parsed and raw
/// [`EntryValue`] inputs remain uninterpreted.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Memory(String);

impl Memory {
    /// Creates a positive memory limit from exact native spelling.
    ///
    /// # Errors
    ///
    /// Returns [`MemoryError::Empty`] for empty text, [`MemoryError::InvalidFormat`] unless the
    /// value is an ASCII-decimal amount with an optional lowercase `b`, `k`, `m`, or `g` unit,
    /// and [`MemoryError::Zero`] when every amount digit is zero.
    pub fn new(memory: impl Into<String>) -> Result<Self, MemoryError> {
        let memory = memory.into();
        if memory.is_empty() {
            return Err(MemoryError::Empty);
        }
        let amount = match memory.as_bytes().last() {
            Some(b'b' | b'k' | b'm' | b'g') => &memory[..memory.len() - 1],
            _ => memory.as_str(),
        };
        if amount.is_empty() || !amount.bytes().all(|byte| byte.is_ascii_digit()) {
            return Err(MemoryError::InvalidFormat);
        }
        if !amount.bytes().any(|byte| byte != b'0') {
            return Err(MemoryError::Zero);
        }
        Ok(Self(memory))
    }

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

impl From<Memory> for EntryValue {
    fn from(memory: Memory) -> Self {
        Self(memory.0)
    }
}

/// Invalid input to [`Memory::new`].
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum MemoryError {
    /// The memory-limit spelling is empty.
    Empty,
    /// The value is not an ASCII-decimal amount with an optional supported lowercase unit.
    InvalidFormat,
    /// A memory limit must be positive.
    Zero,
}

impl fmt::Display for MemoryError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Empty => formatter.write_str("a memory limit must not be empty"),
            Self::InvalidFormat => {
                formatter.write_str("a memory limit must be an ASCII decimal amount with optional unit b, k, m, or g")
            }
            Self::Zero => formatter.write_str("a memory limit must be positive"),
        }
    }
}

impl Error for MemoryError {}

/// Generic systemd section supported in generated Quadlet files.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum SystemdSection {
    /// The generic `[Unit]` section.
    Unit,
    /// The generic `[Service]` section.
    Service,
    /// The generic `[Install]` section.
    Install,
}

/// Evidence-backed dependency and ordering directives in a generic systemd `[Unit]` section.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum SystemdUnitKey {
    /// Strong requirement that also pulls the referenced unit into the transaction.
    Requires,
    /// Weak requirement that does not fail this unit when the referenced unit fails.
    Wants,
    /// Orders this unit after the referenced unit without pulling it into the transaction.
    After,
}

impl SystemdUnitKey {
    const fn name(self) -> &'static str {
        match self {
            Self::Requires => "Requires",
            Self::Wants => "Wants",
            Self::After => "After",
        }
    }
}

impl SystemdSection {
    const fn kind(self) -> SectionKind {
        match self {
            Self::Unit => SectionKind::Unit,
            Self::Service => SectionKind::Service,
            Self::Install => SectionKind::Install,
        }
    }
}

#[derive(Clone, Eq, PartialEq)]
struct GeneratedEntry {
    section: SectionKind,
    kind: EntryKind,
    key: String,
    value: EntryValue,
}

impl fmt::Debug for GeneratedEntry {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut debug = formatter.debug_struct("GeneratedEntry");
        debug
            .field("section", &self.section)
            .field("kind", &self.kind)
            .field("key", &self.key);
        if self.kind.has_sensitive_value() {
            debug.field("value", &"<redacted sensitive value>")
        } else {
            debug.field("value", &self.value)
        };
        debug.finish()
    }
}

/// Ordered builder for one supported Quadlet document.
///
/// Native entries use typed keys and must match the selected unit type. Repeated keys retain
/// insertion order; duplicate singleton native keys are rejected. Generated sections use the
/// deterministic order `[Unit]`, the selected native section, `[Service]`, and `[Install]`.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct QuadletDocumentBuilder {
    unit_type: QuadletUnitType,
    entries: Vec<GeneratedEntry>,
}

impl QuadletDocumentBuilder {
    /// Creates an empty document with the selected required native section.
    #[must_use]
    pub const fn new(unit_type: QuadletUnitType) -> Self {
        Self {
            unit_type,
            entries: Vec::new(),
        }
    }

    /// Returns the selected native unit type.
    #[must_use]
    pub const fn unit_type(&self) -> QuadletUnitType {
        self.unit_type
    }

    /// Appends a typed `[Container]` entry.
    ///
    /// # Errors
    ///
    /// Returns [`RenderError::WrongUnitType`] for a non-container document and
    /// [`RenderError::DuplicateSingleton`] for a repeated singleton key.
    pub fn push_container(&mut self, key: ContainerKey, value: EntryValue) -> Result<(), RenderError> {
        let attempted = container_key_name(key);
        if let Some(existing) = match key {
            ContainerKey::ReloadCmd => self.entries.iter().find_map(|entry| {
                (entry.kind == EntryKind::Container(ContainerKey::ReloadSignal)).then_some("ReloadSignal")
            }),
            ContainerKey::ReloadSignal => self
                .entries
                .iter()
                .find_map(|entry| (entry.kind == EntryKind::Container(ContainerKey::ReloadCmd)).then_some("ReloadCmd")),
            _ => None,
        } {
            return Err(RenderError::ConflictingSingletons {
                existing: existing.to_owned(),
                attempted: attempted.to_owned(),
            });
        }
        self.push_native(
            QuadletUnitType::Container,
            SectionKind::Container,
            EntryKind::Container(key),
            container_key_name(key),
            value,
        )
    }

    /// Appends a typed `[Pod]` entry.
    ///
    /// # Errors
    ///
    /// Returns [`RenderError::WrongUnitType`] for a non-pod document and
    /// [`RenderError::DuplicateSingleton`] for a repeated singleton key.
    pub fn push_pod(&mut self, key: PodKey, value: EntryValue) -> Result<(), RenderError> {
        self.push_native(
            QuadletUnitType::Pod,
            SectionKind::Pod,
            EntryKind::Pod(key),
            pod_key_name(key),
            value,
        )
    }

    /// Appends a typed `[Network]` entry.
    ///
    /// # Errors
    ///
    /// Returns [`RenderError::WrongUnitType`] for a non-network document and
    /// [`RenderError::DuplicateSingleton`] for a repeated singleton key.
    pub fn push_network(&mut self, key: NetworkKey, value: EntryValue) -> Result<(), RenderError> {
        self.push_native(
            QuadletUnitType::Network,
            SectionKind::Network,
            EntryKind::Network(key),
            network_key_name(key),
            value,
        )
    }

    /// Appends a typed `[Volume]` entry.
    ///
    /// # Errors
    ///
    /// Returns [`RenderError::WrongUnitType`] for a non-volume document and
    /// [`RenderError::DuplicateSingleton`] for a repeated singleton key.
    pub fn push_volume(&mut self, key: VolumeKey, value: EntryValue) -> Result<(), RenderError> {
        self.push_native(
            QuadletUnitType::Volume,
            SectionKind::Volume,
            EntryKind::Volume(key),
            volume_key_name(key),
            value,
        )
    }

    /// Appends a typed `[Build]` entry.
    ///
    /// `ImageTag`, `File`, `Network`, `Label`, `BuildArg`, `Secret`, `GroupAdd`, `DNS`, `DNSOption`, `DNSSearch`,
    /// `Annotation`, `Environment`, `ContainersConfModule`, `GlobalArgs`, `Volume`, and `PodmanArgs` entries remain repeatable and ordered; `SetWorkingDirectory`, `Target`, `Arch`, `Variant`,
    /// `Pull`, `Retry`, `RetryDelay`, `TLSVerify`, `ForceRM`, `AuthFile`, `IgnoreFile`, and `ServiceName` are singletons. Values are exact
    /// physical-line-safe native text and are not interpreted by the builder.
    ///
    /// # Errors
    ///
    /// Returns [`RenderError::WrongUnitType`] for a non-build document and
    /// [`RenderError::DuplicateSingleton`] for a repeated singleton Build key.
    pub fn push_build(&mut self, key: BuildKey, value: EntryValue) -> Result<(), RenderError> {
        self.push_native(
            QuadletUnitType::Build,
            SectionKind::Build,
            EntryKind::Build(key),
            build_key_name(key),
            value,
        )
    }

    /// Appends a typed `[Image]` entry.
    ///
    /// `ContainersConfModule` and `GlobalArgs` entries remain repeatable and ordered. All other Image keys, including `OS`, are
    /// singletons. `Creds` and `DecryptionKey` are debug-redacted after key assignment, while
    /// explicit rendering and raw-value access remain exact. Values are exact physical-line-safe
    /// native text and are not interpreted by the builder; it does not read paths or modules, parse configuration,
    /// validate a CLI, or model pull, runtime, graph, or conversion semantics.
    ///
    /// # Errors
    ///
    /// Returns [`RenderError::WrongUnitType`] for a non-image document and
    /// [`RenderError::DuplicateSingleton`] only for a repeated singleton Image key.
    pub fn push_image(&mut self, key: ImageKey, value: EntryValue) -> Result<(), RenderError> {
        self.push_native(
            QuadletUnitType::Image,
            SectionKind::Image,
            EntryKind::Image(key),
            image_key_name(key),
            value,
        )
    }

    /// Appends an open-ended entry to a generic systemd section.
    ///
    /// Generic entries retain insertion order and may repeat because their reset and list behavior
    /// is directive-specific and intentionally not guessed by this builder.
    ///
    /// # Errors
    ///
    /// Returns [`RenderError::InvalidKey`] when the key is not an ASCII alphanumeric directive
    /// name.
    pub fn push_systemd(
        &mut self,
        section: SystemdSection,
        key: impl Into<String>,
        value: EntryValue,
    ) -> Result<(), RenderError> {
        let key = key.into();
        if key.is_empty() || !key.bytes().all(|byte| byte.is_ascii_alphanumeric()) {
            return Err(RenderError::InvalidKey(key));
        }
        self.entries.push(GeneratedEntry {
            section: section.kind(),
            kind: EntryKind::GenericSystemd,
            key,
            value,
        });
        Ok(())
    }

    /// Appends an evidence-backed dependency or ordering directive to `[Unit]`.
    ///
    /// These entries remain repeatable and retain insertion order. The value is an exact systemd
    /// unit-list spelling; this method does not resolve unit names or infer relationships between
    /// Quadlet source files.
    ///
    /// # Errors
    ///
    /// Returns the same errors as [`Self::push_systemd`].
    pub fn push_systemd_unit(&mut self, key: SystemdUnitKey, value: EntryValue) -> Result<(), RenderError> {
        self.push_systemd(SystemdSection::Unit, key.name(), value)
    }

    /// Renders, reparses, and validates the complete generated document.
    ///
    /// # Errors
    ///
    /// Returns [`RenderError::InvalidDocument`] when native shape validation fails, or
    /// [`RenderError::TypedModel`] for an internal source-span consistency failure.
    pub fn build(&self, source_id: SourceId) -> Result<GeneratedQuadletDocument, RenderError> {
        let text = self.render_text();
        let parsed = QuadletDocument::parse(self.unit_type, source_id, text).map_err(RenderError::TypedModel)?;
        if !parsed.is_valid() {
            let mut diagnostics = parsed.syntax().diagnostics().to_vec();
            diagnostics.extend_from_slice(parsed.model_diagnostics());
            return Err(RenderError::InvalidDocument(diagnostics));
        }
        Ok(GeneratedQuadletDocument { parsed })
    }

    fn push_native(
        &mut self,
        required: QuadletUnitType,
        section: SectionKind,
        kind: EntryKind,
        key: &'static str,
        value: EntryValue,
    ) -> Result<(), RenderError> {
        if self.unit_type != required {
            return Err(RenderError::WrongUnitType {
                document: self.unit_type,
                entry: required,
            });
        }
        if !kind.is_repeatable() && self.entries.iter().any(|entry| entry.kind == kind) {
            return Err(RenderError::DuplicateSingleton(key.to_owned()));
        }
        self.entries.push(GeneratedEntry {
            section,
            kind,
            key: key.to_owned(),
            value,
        });
        Ok(())
    }

    fn render_text(&self) -> String {
        let native = self.unit_type.native_section();
        let sections = [SectionKind::Unit, native, SectionKind::Service, SectionKind::Install];
        let mut output = String::new();
        let mut wrote_section = false;

        for section in sections {
            let entries: Vec<_> = self.entries.iter().filter(|entry| entry.section == section).collect();
            if entries.is_empty() && section != native {
                continue;
            }
            if wrote_section {
                output.push('\n');
            }
            wrote_section = true;
            output.push('[');
            output.push_str(section_name(section));
            output.push_str("]\n");
            for entry in entries {
                output.push_str(&entry.key);
                output.push('=');
                output.push_str(entry.value.as_str());
                output.push('\n');
            }
        }
        output
    }
}

/// Successfully generated and parse-back-validated Quadlet document.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct GeneratedQuadletDocument {
    parsed: QuadletParseResult,
}

impl GeneratedQuadletDocument {
    /// Returns the deterministic generated source text.
    #[must_use]
    pub fn text(&self) -> &str {
        self.parsed.syntax().document().render_preserved()
    }

    /// Returns the validated native typed document.
    #[must_use]
    pub const fn document(&self) -> &QuadletDocument {
        self.parsed.document()
    }

    /// Returns the complete syntax and model result.
    #[must_use]
    pub const fn parse_result(&self) -> &QuadletParseResult {
        &self.parsed
    }

    /// Decomposes the generated document into its complete parse result.
    #[must_use]
    pub fn into_parse_result(self) -> QuadletParseResult {
        self.parsed
    }
}

/// Failure while constructing or validating generated Quadlet source.
#[derive(Clone, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum RenderError {
    /// A native value contained a line ending or NUL byte.
    InvalidValue,
    /// A generic systemd key was empty or contained non-alphanumeric bytes.
    InvalidKey(String),
    /// A native key did not belong to the builder's selected unit type.
    WrongUnitType {
        /// Unit type selected for the document.
        document: QuadletUnitType,
        /// Unit type required by the attempted entry.
        entry: QuadletUnitType,
    },
    /// A singleton native key was added more than once.
    DuplicateSingleton(String),
    /// Two mutually exclusive singleton native keys were added to one document.
    ConflictingSingletons {
        /// Existing native key.
        existing: String,
        /// Attempted native key.
        attempted: String,
    },
    /// Generated source failed syntax or native-model validation.
    InvalidDocument(Vec<Diagnostic>),
    /// Parser-owned spans could not be interpreted consistently.
    TypedModel(TypedModelError),
}

impl RenderError {
    /// Returns validation diagnostics for an invalid generated document.
    #[must_use]
    pub fn diagnostics(&self) -> &[Diagnostic] {
        match self {
            Self::InvalidDocument(diagnostics) => diagnostics,
            _ => &[],
        }
    }
}

impl fmt::Display for RenderError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::InvalidValue => formatter.write_str("generated Quadlet values must fit on one physical line"),
            Self::InvalidKey(key) => write!(formatter, "invalid generic systemd key `{key}`"),
            Self::WrongUnitType { document, entry } => {
                write!(formatter, "cannot add a {entry:?} entry to a {document:?} document")
            }
            Self::DuplicateSingleton(key) => write!(formatter, "singleton Quadlet key `{key}` is repeated"),
            Self::ConflictingSingletons { existing, attempted } => {
                write!(
                    formatter,
                    "singleton Quadlet keys `{existing}` and `{attempted}` conflict"
                )
            }
            Self::InvalidDocument(diagnostics) => {
                write!(
                    formatter,
                    "generated Quadlet document has {} diagnostic(s)",
                    diagnostics.len()
                )
            }
            Self::TypedModel(error) => write!(formatter, "generated Quadlet model is inconsistent: {error}"),
        }
    }
}

impl Error for RenderError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        match self {
            Self::TypedModel(error) => Some(error),
            _ => None,
        }
    }
}

const fn container_key_name(key: ContainerKey) -> &'static str {
    match key {
        ContainerKey::AddHost => "AddHost",
        ContainerKey::Image => "Image",
        ContainerKey::Exec => "Exec",
        ContainerKey::Environment => "Environment",
        ContainerKey::EnvironmentFile => "EnvironmentFile",
        ContainerKey::Label => "Label",
        ContainerKey::Secret => "Secret",
        ContainerKey::PublishPort => "PublishPort",
        ContainerKey::Volume => "Volume",
        ContainerKey::Network => "Network",
        ContainerKey::Pod => "Pod",
        ContainerKey::HealthCmd => "HealthCmd",
        ContainerKey::Notify => "Notify",
        ContainerKey::HealthInterval => "HealthInterval",
        ContainerKey::HealthRetries => "HealthRetries",
        ContainerKey::HealthStartPeriod => "HealthStartPeriod",
        ContainerKey::HealthTimeout => "HealthTimeout",
        ContainerKey::PodmanArgs => "PodmanArgs",
        ContainerKey::User => "User",
        ContainerKey::Group => "Group",
        ContainerKey::UserNS => "UserNS",
        ContainerKey::GroupAdd => "GroupAdd",
        ContainerKey::WorkingDir => "WorkingDir",
        ContainerKey::ReadOnly => "ReadOnly",
        ContainerKey::Rootfs => "Rootfs",
        ContainerKey::ContainerName => "ContainerName",
        ContainerKey::Entrypoint => "Entrypoint",
        ContainerKey::RunInit => "RunInit",
        ContainerKey::StopSignal => "StopSignal",
        ContainerKey::StopTimeout => "StopTimeout",
        ContainerKey::Pull => "Pull",
        ContainerKey::PidsLimit => "PidsLimit",
        ContainerKey::HostName => "HostName",
        ContainerKey::ShmSize => "ShmSize",
        ContainerKey::DropCapability => "DropCapability",
        ContainerKey::AddCapability => "AddCapability",
        ContainerKey::Tmpfs => "Tmpfs",
        ContainerKey::Sysctl => "Sysctl",
        ContainerKey::Ulimit => "Ulimit",
        ContainerKey::AddDevice => "AddDevice",
        ContainerKey::Memory => "Memory",
        ContainerKey::DNS => "DNS",
        ContainerKey::DNSOption => "DNSOption",
        ContainerKey::DNSSearch => "DNSSearch",
        ContainerKey::ExposeHostPort => "ExposeHostPort",
        ContainerKey::Annotation => "Annotation",
        ContainerKey::AppArmor => "AppArmor",
        ContainerKey::NoNewPrivileges => "NoNewPrivileges",
        ContainerKey::SeccompProfile => "SeccompProfile",
        ContainerKey::SecurityLabelDisable => "SecurityLabelDisable",
        ContainerKey::SecurityLabelFileType => "SecurityLabelFileType",
        ContainerKey::SecurityLabelLevel => "SecurityLabelLevel",
        ContainerKey::SecurityLabelNested => "SecurityLabelNested",
        ContainerKey::SecurityLabelType => "SecurityLabelType",
        ContainerKey::Mask => "Mask",
        ContainerKey::Unmask => "Unmask",
        ContainerKey::LogDriver => "LogDriver",
        ContainerKey::LogOpt => "LogOpt",
        ContainerKey::IP => "IP",
        ContainerKey::IP6 => "IP6",
        ContainerKey::NetworkAlias => "NetworkAlias",
        ContainerKey::ReloadCmd => "ReloadCmd",
        ContainerKey::ReloadSignal => "ReloadSignal",
    }
}

const fn build_key_name(key: BuildKey) -> &'static str {
    match key {
        BuildKey::ImageTag => "ImageTag",
        BuildKey::SetWorkingDirectory => "SetWorkingDirectory",
        BuildKey::File => "File",
        BuildKey::Target => "Target",
        BuildKey::Network => "Network",
        BuildKey::Label => "Label",
        BuildKey::BuildArg => "BuildArg",
        BuildKey::Secret => "Secret",
        BuildKey::Arch => "Arch",
        BuildKey::Variant => "Variant",
        BuildKey::Pull => "Pull",
        BuildKey::PodmanArgs => "PodmanArgs",
        BuildKey::Retry => "Retry",
        BuildKey::RetryDelay => "RetryDelay",
        BuildKey::TLSVerify => "TLSVerify",
        BuildKey::ForceRM => "ForceRM",
        BuildKey::GroupAdd => "GroupAdd",
        BuildKey::DNS => "DNS",
        BuildKey::DNSOption => "DNSOption",
        BuildKey::DNSSearch => "DNSSearch",
        BuildKey::AuthFile => "AuthFile",
        BuildKey::IgnoreFile => "IgnoreFile",
        BuildKey::Annotation => "Annotation",
        BuildKey::Environment => "Environment",
        BuildKey::ContainersConfModule => "ContainersConfModule",
        BuildKey::GlobalArgs => "GlobalArgs",
        BuildKey::ServiceName => "ServiceName",
        BuildKey::Volume => "Volume",
    }
}

const fn image_key_name(key: ImageKey) -> &'static str {
    match key {
        ImageKey::Image => "Image",
        ImageKey::ImageTag => "ImageTag",
        ImageKey::ServiceName => "ServiceName",
        ImageKey::AllTags => "AllTags",
        ImageKey::Arch => "Arch",
        ImageKey::AuthFile => "AuthFile",
        ImageKey::CertDir => "CertDir",
        ImageKey::ContainersConfModule => "ContainersConfModule",
        ImageKey::Creds => "Creds",
        ImageKey::DecryptionKey => "DecryptionKey",
        ImageKey::GlobalArgs => "GlobalArgs",
        ImageKey::OS => "OS",
    }
}

const fn pod_key_name(key: PodKey) -> &'static str {
    match key {
        PodKey::AddHost => "AddHost",
        PodKey::PodName => "PodName",
        PodKey::PublishPort => "PublishPort",
        PodKey::Network => "Network",
        PodKey::Volume => "Volume",
        PodKey::UserNS => "UserNS",
        PodKey::ShmSize => "ShmSize",
        PodKey::ExitPolicy => "ExitPolicy",
        PodKey::StopTimeout => "StopTimeout",
        PodKey::ServiceName => "ServiceName",
    }
}

const fn network_key_name(key: NetworkKey) -> &'static str {
    match key {
        NetworkKey::NetworkName => "NetworkName",
        NetworkKey::Driver => "Driver",
        NetworkKey::Options => "Options",
        NetworkKey::Internal => "Internal",
        NetworkKey::IPv6 => "IPv6",
        NetworkKey::IPAMDriver => "IPAMDriver",
        NetworkKey::Subnet => "Subnet",
        NetworkKey::Gateway => "Gateway",
        NetworkKey::IPRange => "IPRange",
        NetworkKey::Label => "Label",
    }
}

const fn volume_key_name(key: VolumeKey) -> &'static str {
    match key {
        VolumeKey::VolumeName => "VolumeName",
        VolumeKey::Driver => "Driver",
        VolumeKey::Options => "Options",
        VolumeKey::Label => "Label",
        VolumeKey::Device => "Device",
        VolumeKey::Type => "Type",
        VolumeKey::Copy => "Copy",
        VolumeKey::ContainersConfModule => "ContainersConfModule",
        VolumeKey::GlobalArgs => "GlobalArgs",
        VolumeKey::PodmanArgs => "PodmanArgs",
        VolumeKey::User => "User",
        VolumeKey::Group => "Group",
        VolumeKey::UID => "UID",
        VolumeKey::GID => "GID",
        VolumeKey::ServiceName => "ServiceName",
        VolumeKey::Image => "Image",
    }
}

const fn section_name(section: SectionKind) -> &'static str {
    match section {
        SectionKind::Unit => "Unit",
        SectionKind::Container => "Container",
        SectionKind::Pod => "Pod",
        SectionKind::Network => "Network",
        SectionKind::Volume => "Volume",
        SectionKind::Build => "Build",
        SectionKind::Image => "Image",
        SectionKind::Service => "Service",
        SectionKind::Install => "Install",
        SectionKind::Unknown => "Unknown",
    }
}