reallyme-buffa-codegen 0.1.3

ReallyMe fork: Shared code generation logic for buffa (descriptor → Rust source)
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
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
//! Validate the complete request before returning any native declarations.

use std::collections::{BTreeMap, BTreeSet};

use proc_macro2::{Ident, TokenStream};
use quote::quote;

use crate::generated::descriptor::field_descriptor_proto::{Label, Type};
use crate::generated::descriptor::{
    DescriptorProto, EnumDescriptorProto, FieldDescriptorProto, FileDescriptorProto,
};
use crate::{GeneratedFile, GeneratedFileKind};

use super::*;

const MAX_FIELD_DEPTH: usize = 64;

struct Schema<'a> {
    enums: BTreeMap<String, (&'a EnumDescriptorProto, &'a str)>,
    messages: BTreeMap<String, (&'a DescriptorProto, &'a str)>,
    native_selectors: BTreeMap<String, BTreeSet<String>>,
}

impl<'a> Schema<'a> {
    fn new(files: &'a [FileDescriptorProto]) -> Result<Self, AdapterError> {
        let mut schema = Self {
            enums: BTreeMap::new(),
            messages: BTreeMap::new(),
            native_selectors: BTreeMap::new(),
        };
        for file in files {
            let file_name = file.name.as_deref().ok_or(ErrorReason::InvalidDescriptor)?;
            let package = file.package.as_deref().unwrap_or_default();
            let prefix = if package.is_empty() {
                String::new()
            } else {
                format!(".{package}")
            };
            schema.add_enums(&prefix, &file.enum_type, file_name)?;
            // An explicit stack also bounds traversal of adversarial hand-built
            // descriptors without relying on the thread's recursion limit.
            let mut pending: Vec<_> = file
                .message_type
                .iter()
                .map(|m| (prefix.clone(), m))
                .collect();
            while let Some((parent, message)) = pending.pop() {
                let name = message
                    .name
                    .as_deref()
                    .ok_or(ErrorReason::InvalidDescriptor)?;
                let fqn = format!("{parent}.{name}");
                if schema
                    .messages
                    .insert(fqn.clone(), (message, file_name))
                    .is_some()
                {
                    return Err(ErrorReason::InvalidDescriptor.into());
                }
                schema.add_enums(&fqn, &message.enum_type, file_name)?;
                pending.extend(message.nested_type.iter().map(|m| (fqn.clone(), m)));
            }
        }
        if schema
            .enums
            .keys()
            .any(|name| schema.messages.contains_key(name))
        {
            return Err(ErrorReason::InvalidDescriptor.into());
        }
        Ok(schema)
    }

    fn add_enums(
        &mut self,
        parent: &str,
        enums: &'a [EnumDescriptorProto],
        file: &'a str,
    ) -> Result<(), AdapterError> {
        for descriptor in enums {
            let name = descriptor
                .name
                .as_deref()
                .ok_or(ErrorReason::InvalidDescriptor)?;
            if self
                .enums
                .insert(format!("{parent}.{name}"), (descriptor, file))
                .is_some()
            {
                return Err(ErrorReason::InvalidDescriptor.into());
            }
        }
        Ok(())
    }

    fn enumeration(&self, name: &str) -> Result<&'a EnumDescriptorProto, AdapterError> {
        self.enums
            .get(name)
            .map(|(e, _)| *e)
            .ok_or_else(|| ErrorReason::MissingDescriptor.into())
    }

    fn message(&self, name: &str) -> Result<&'a DescriptorProto, AdapterError> {
        self.messages
            .get(name)
            .map(|(m, _)| *m)
            .ok_or_else(|| ErrorReason::MissingDescriptor.into())
    }

    fn in_scope(&self, name: &str, requested: &[String]) -> Result<(), AdapterError> {
        let file = self
            .enums
            .get(name)
            .map(|(_, f)| f)
            .or_else(|| self.messages.get(name).map(|(_, f)| f))
            .ok_or(ErrorReason::MissingDescriptor)?;
        if !requested.iter().any(|f| f == file) {
            return Err(ErrorReason::GenerationScope.into());
        }
        Ok(())
    }

    fn field(&self, source: &FieldSource) -> Result<&'a FieldDescriptorProto, AdapterError> {
        if source.path.is_empty() || source.path.len() > MAX_FIELD_DEPTH {
            return Err(ErrorReason::InvalidPayload.into());
        }
        let mut message = self.message(&source.message)?;
        let mut parts = source.path.iter().peekable();
        while let Some(part) = parts.next() {
            let field = unique_field(message, part)?;
            if field.label == Some(Label::LABEL_REPEATED) {
                return Err(ErrorReason::OwnershipMismatch.into());
            }
            if parts.peek().is_none() {
                return Ok(field);
            }
            if field.r#type != Some(Type::TYPE_MESSAGE) || field.proto3_optional == Some(true) {
                return Err(ErrorReason::InvalidPayload.into());
            }
            message = self.message(
                field
                    .type_name
                    .as_deref()
                    .ok_or(ErrorReason::InvalidDescriptor)?,
            )?;
        }
        Err(ErrorReason::InvalidPayload.into())
    }
}

fn unique_field<'a>(
    message: &'a DescriptorProto,
    name: &str,
) -> Result<&'a FieldDescriptorProto, AdapterError> {
    let mut fields = message
        .field
        .iter()
        .filter(|f| f.name.as_deref() == Some(name));
    let field = fields.next().ok_or(ErrorReason::MissingDescriptor)?;
    if fields.next().is_some() {
        return Err(ErrorReason::InvalidDescriptor.into());
    }
    Ok(field)
}

fn ident(name: &str) -> Result<Ident, AdapterError> {
    // Reject structural syntax before tokenization: a deeply nested token
    // group must never reach a general Rust parser through a name string.
    if !name
        .bytes()
        .all(|b| !b.is_ascii() || b.is_ascii_alphanumeric() || b == b'_')
    {
        return Err(ErrorReason::InvalidIdentifier.into());
    }
    // syn's identifier parser does not track every newer edition keyword.
    // Native declarations must remain usable by consumers on those editions.
    if name.starts_with("r#") || crate::idents::is_rust_keyword(name) {
        return Err(ErrorReason::InvalidIdentifier.into());
    }
    let value: Ident = syn::parse_str(name).map_err(|_| ErrorReason::InvalidIdentifier)?;
    if matches!(
        name,
        "self" | "Self" | "super" | "crate" | "_" | "r#self" | "r#Self" | "r#super" | "r#crate"
    ) {
        return Err(ErrorReason::InvalidIdentifier.into());
    }
    Ok(value)
}

fn path(name: &str) -> Result<syn::Path, AdapterError> {
    // Only module-path syntax reaches syn. In particular, nested generics or
    // token groups cannot exhaust the parser's stack before being rejected.
    if !name.bytes().all(|b| {
        !b.is_ascii()
            || b.is_ascii_alphanumeric()
            || b.is_ascii_whitespace()
            || matches!(b, b'_' | b':' | b'#')
    }) {
        return Err(ErrorReason::InvalidRustPath.into());
    }
    let value: syn::Path = syn::parse_str(name).map_err(|_| ErrorReason::InvalidRustPath)?;
    if value.segments.is_empty()
        || value.segments.iter().any(|s| {
            let name = s.ident.to_string();
            !matches!(s.arguments, syn::PathArguments::None)
                || (crate::idents::is_rust_keyword(&name)
                    && !matches!(name.as_str(), "self" | "Self" | "super" | "crate"))
        })
    {
        return Err(ErrorReason::InvalidRustPath.into());
    }
    Ok(value)
}

fn error(mapping: &ErrorMapping) -> Result<(syn::Path, syn::Path), AdapterError> {
    Ok((path(&mapping.rust_type)?, path(&mapping.value)?))
}

/// Generate standalone native adapter files, validating complete coverage first.
///
/// Outputs have [`GeneratedFileKind::Adapter`] and are intentionally excluded
/// from the portable module tree. Include each in its native API module. This
/// function neither reads files nor changes descriptors or portable generation.
///
/// # Errors
/// Returns a fixed [`ErrorReason`] for invalid metadata, incomplete coverage,
/// incompatible ownership, ambiguous aliases or sources outside the request.
pub fn generate_adapters(
    files: &[FileDescriptorProto],
    requested: &[String],
    config: &AdapterConfig,
) -> Result<Vec<GeneratedFile>, AdapterError> {
    if config.version != 1 {
        return Err(ErrorReason::UnsupportedVersion.into());
    }
    let mut schema = Schema::new(files)?;
    for selector in &config.selectors {
        schema
            .native_selectors
            .entry(selector.wire.source.clone())
            .or_default()
            .insert(selector.name.clone());
    }
    let mut names = BTreeSet::new();
    let mut ports = BTreeMap::new();
    for port in &config.ports {
        ident(&port.name)?;
        if ports
            .insert(port.name.as_str(), path(&port.rust_trait)?)
            .is_some()
        {
            return Err(ErrorReason::DuplicateName.into());
        }
    }
    let mut output = BTreeMap::new();
    let mut emit = |file: &str, name: &str, tokens: TokenStream| -> Result<(), AdapterError> {
        ident(name)?;
        if !names.insert(name.to_owned()) {
            return Err(ErrorReason::DuplicateName.into());
        }
        let stem = file.strip_suffix(".rs").ok_or(ErrorReason::InvalidOutput)?;
        if stem.is_empty()
            || stem.eq_ignore_ascii_case("mod")
            || !stem.bytes().all(|b| b.is_ascii_alphanumeric() || b == b'_')
            || is_reserved_windows_file_stem(stem)
        {
            return Err(ErrorReason::InvalidOutput.into());
        }
        let syntax = syn::parse2::<syn::File>(tokens).map_err(|_| ErrorReason::InvalidSyntax)?;
        let content = format!(
            "{}{}",
            crate::GENERATED_HEADER,
            prettyplease::unparse(&syntax)
        );
        let generated = GeneratedFile {
            name: file.to_owned(),
            package: String::new(),
            content,
            kind: GeneratedFileKind::Adapter,
        };
        // Files may be written on a case-insensitive filesystem even when
        // generation runs on a case-sensitive build host.
        if output
            .insert(file.to_ascii_lowercase(), generated)
            .is_some()
        {
            return Err(ErrorReason::DuplicateOutput.into());
        }
        Ok(())
    };
    for selector in &config.selectors {
        schema.in_scope(&selector.wire.source, requested)?;
        emit(&selector.file, &selector.name, closed(&schema, selector)?)?;
    }
    for selector in &config.payloads {
        schema.in_scope(&selector.source, requested)?;
        let cases = enum_cases(schema.enumeration(&selector.source)?)?;
        let tokens = payload_enum(
            &schema,
            &ports,
            &selector.name,
            &selector.variants,
            &cases,
            false,
            None,
        )?;
        emit(&selector.file, &selector.name, tokens)?;
    }
    for adapter in &config.borrowed {
        let (cases, oneof) = match &adapter.source {
            BorrowedSource::Enum { source } => {
                schema.in_scope(source, requested)?;
                (enum_cases(schema.enumeration(source)?)?, None)
            }
            BorrowedSource::Oneof { message, name } => {
                schema.in_scope(message, requested)?;
                let descriptor = schema.message(message)?;
                let mut declarations = descriptor
                    .oneof_decl
                    .iter()
                    .enumerate()
                    .filter(|(_, o)| o.name.as_deref() == Some(name));
                let (index, _) = declarations.next().ok_or(ErrorReason::MissingDescriptor)?;
                if declarations.next().is_some() {
                    return Err(ErrorReason::InvalidDescriptor.into());
                }
                let index = i32::try_from(index).map_err(|_| ErrorReason::InvalidDescriptor)?;
                let mut cases = BTreeMap::new();
                for field in &descriptor.field {
                    if field.oneof_index != Some(index) || field.proto3_optional == Some(true) {
                        return Err(ErrorReason::UncoveredField.into());
                    }
                    let name = field
                        .name
                        .as_deref()
                        .ok_or(ErrorReason::InvalidDescriptor)?;
                    if cases
                        .insert(
                            name.to_owned(),
                            field.number.ok_or(ErrorReason::InvalidDescriptor)?,
                        )
                        .is_some()
                    {
                        return Err(ErrorReason::InvalidDescriptor.into());
                    }
                }
                if cases.is_empty() {
                    return Err(ErrorReason::InvalidDescriptor.into());
                }
                (cases, Some(message.as_str()))
            }
        };
        let tokens = payload_enum(
            &schema,
            &ports,
            &adapter.name,
            &adapter.variants,
            &cases,
            true,
            oneof,
        )?;
        emit(&adapter.file, &adapter.name, tokens)?;
    }
    Ok(output.into_values().collect())
}

fn enum_values(descriptor: &EnumDescriptorProto) -> Result<BTreeMap<String, i32>, AdapterError> {
    let mut values = BTreeMap::new();
    for value in &descriptor.value {
        let name = value
            .name
            .as_deref()
            .ok_or(ErrorReason::InvalidDescriptor)?;
        let number = value.number.ok_or(ErrorReason::InvalidDescriptor)?;
        let mut bytes = name.bytes();
        if !bytes
            .next()
            .is_some_and(|b| b.is_ascii_alphabetic() || b == b'_')
            || !bytes.all(|b| b.is_ascii_alphanumeric() || b == b'_')
        {
            return Err(ErrorReason::InvalidDescriptor.into());
        }
        if values.insert(name.to_owned(), number).is_some() {
            return Err(ErrorReason::InvalidDescriptor.into());
        }
    }
    // Unspecified is a numeric boundary, not a unique spelling: protobuf
    // enums may legally declare several names for zero with allow_alias.
    if !values.values().any(|&number| number == 0) {
        return Err(ErrorReason::InvalidDescriptor.into());
    }
    Ok(values)
}

fn enum_cases(descriptor: &EnumDescriptorProto) -> Result<BTreeMap<String, i32>, AdapterError> {
    let mut cases = enum_values(descriptor)?;
    cases.retain(|_, n| *n != 0);
    if cases.is_empty() {
        return Err(ErrorReason::InvalidDescriptor.into());
    }
    Ok(cases)
}

fn claim<'a>(
    values: &'a BTreeMap<String, i32>,
    claimed: &mut BTreeSet<String>,
    source: &str,
) -> Result<&'a i32, AdapterError> {
    let number = values.get(source).ok_or(ErrorReason::UnknownCase)?;
    if *number == 0 {
        return Err(ErrorReason::UnspecifiedCase.into());
    }
    if !claimed.insert(source.to_owned()) {
        return Err(ErrorReason::DuplicateCase.into());
    }
    Ok(number)
}

struct InputMapping<'a> {
    wire: &'a WireEnum,
    error: &'a ErrorMapping,
    canonical: &'a [(String, String)],
    normalized: &'a [Normalization],
    rejected: &'a [String],
}

fn conversion(
    schema: &Schema<'_>,
    variants: &BTreeMap<String, Ident>,
    native: &Ident,
    mapping: InputMapping<'_>,
) -> Result<TokenStream, AdapterError> {
    let InputMapping {
        wire,
        error: error_mapping,
        canonical,
        normalized,
        rejected,
    } = mapping;
    let values = enum_values(schema.enumeration(&wire.source)?)?;
    let wire_type = path(&wire.rust_type)?;
    let (error_type, error_value) = error(error_mapping)?;
    let mut claimed = BTreeSet::new();
    let mut accepted = BTreeMap::new();
    for (source, target) in canonical
        .iter()
        .map(|(s, t)| (s, t))
        .chain(normalized.iter().map(|n| (&n.source, &n.target)))
    {
        let number = *claim(&values, &mut claimed, source)?;
        let variant = variants
            .get(target)
            .ok_or(ErrorReason::InvalidNormalization)?;
        if let Some(previous) = accepted.insert(number, variant) {
            if previous != variant {
                return Err(ErrorReason::InvalidNormalization.into());
            }
        }
    }
    for source in rejected {
        let number = claim(&values, &mut claimed, source)?;
        if accepted.contains_key(number) {
            return Err(ErrorReason::InvalidNormalization.into());
        }
    }
    if values
        .iter()
        .any(|(name, &n)| n != 0 && !claimed.contains(name))
    {
        return Err(ErrorReason::UncoveredCase.into());
    }
    let arms = accepted
        .iter()
        .map(|(number, variant)| quote! { #number => ::core::result::Result::Ok(Self::#variant), });
    // The public Unknown variant can be constructed with a recognized number.
    // Preserve the known-value boundary before applying numeric normalizations.
    Ok(quote! {
        impl ::core::convert::TryFrom<::buffa::EnumValue<#wire_type>> for #native {
            type Error = #error_type;
            fn try_from(value: ::buffa::EnumValue<#wire_type>) -> ::core::result::Result<Self, #error_type> {
                if value.is_unknown() {
                    return ::core::result::Result::Err(::core::convert::Into::into(#error_value));
                }
                match value.to_i32() {
                    #(#arms)*
                    _ => ::core::result::Result::Err(::core::convert::Into::into(#error_value)),
                }
            }
        }
    })
}

fn closed(schema: &Schema<'_>, selector: &ClosedSelector) -> Result<TokenStream, AdapterError> {
    let name = ident(&selector.name)?;
    if selector.variants.is_empty() {
        return Err(ErrorReason::UncoveredCase.into());
    }
    let mut variants = BTreeMap::new();
    let mut occupied = BTreeSet::from([
        String::from("ALL"),
        String::from("identifier"),
        String::from("from_identifier"),
    ]);
    let mut declarations = Vec::new();
    let mut aliases = Vec::new();
    let mut canonical = Vec::new();
    let mut reverse = Vec::new();
    let mut ordered = Vec::new();
    let wire = path(&selector.wire.rust_type)?;
    let mut canonical_numbers = BTreeSet::new();
    let values = enum_values(schema.enumeration(&selector.wire.source)?)?;
    for variant in &selector.variants {
        let native = ident(&variant.name)?;
        if !occupied.insert(variant.name.clone()) {
            return Err(ErrorReason::DuplicateName.into());
        }
        let number = values
            .get(&variant.source)
            .ok_or(ErrorReason::UnknownCase)?;
        if !canonical_numbers.insert(number) {
            return Err(ErrorReason::InvalidNormalization.into());
        }
        // Wire values follow the portable generator's keyword escaping; the
        // explicitly named native API uses the stricter identifier validator.
        let source = crate::idents::make_field_ident(&variant.source);
        variants.insert(variant.name.clone(), native.clone());
        canonical.push((variant.source.clone(), variant.name.clone()));
        ordered.push(native.clone());
        declarations.push(quote! { #[doc = "Canonical descriptor selector."] #native, });
        reverse.push(quote! { #name::#native => Self::#source, });
        for alias in &variant.aliases {
            let alias_ident = ident(alias)?;
            if !occupied.insert(alias.clone()) {
                return Err(ErrorReason::DuplicateName.into());
            }
            aliases.push(quote! {
                #[doc = "Compatibility spelling of the canonical selector."]
                #[allow(non_upper_case_globals)]
                pub const #alias_ident: Self = Self::#native;
            });
        }
    }
    let conversion = conversion(
        schema,
        &variants,
        &name,
        InputMapping {
            wire: &selector.wire,
            error: &selector.error,
            canonical: &canonical,
            normalized: &selector.normalizations,
            rejected: &selector.rejected,
        },
    )?;
    let mut legacy = Vec::new();
    let mut legacy_types = BTreeSet::from([quote!(#wire).to_string()]);
    for source in &selector.legacy {
        // Distinct paths prevent duplicate impls within one declaration. Rust
        // additionally checks whether application aliases name the same type.
        let legacy_path = path(&source.wire.rust_type)?;
        if !legacy_types.insert(quote!(#legacy_path).to_string()) {
            return Err(ErrorReason::InvalidNormalization.into());
        }
        legacy.push(conversion_for_legacy(
            schema,
            source,
            &selector.error,
            &variants,
            &name,
        )?);
    }
    let identities = identities(selector, &ordered)?;
    let non_exhaustive = selector
        .non_exhaustive
        .then(|| quote! { #[non_exhaustive] });
    Ok(quote! {
        #[doc = "Closed native selector generated from authoritative descriptors."]
        #non_exhaustive
        #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
        pub enum #name { #(#declarations)* }
        #conversion
        #(#legacy)*
        #[allow(deprecated)]
        impl ::core::convert::From<#name> for #wire {
            fn from(value: #name) -> Self { match value { #(#reverse)* } }
        }
        impl #name {
            #[doc = "Complete canonical selector set in manifest order."]
            pub const ALL: &'static [Self] = &[#(Self::#ordered),*];
            #(#aliases)*
        }
        #identities
    })
}

fn conversion_for_legacy(
    schema: &Schema<'_>,
    legacy: &LegacySelector,
    error: &ErrorMapping,
    variants: &BTreeMap<String, Ident>,
    name: &Ident,
) -> Result<TokenStream, AdapterError> {
    conversion(
        schema,
        variants,
        name,
        InputMapping {
            wire: &legacy.wire,
            error,
            canonical: &[],
            normalized: &legacy.normalizations,
            rejected: &legacy.rejected,
        },
    )
}

fn identities(selector: &ClosedSelector, variants: &[Ident]) -> Result<TokenStream, AdapterError> {
    let count = selector
        .variants
        .iter()
        .filter(|v| v.identifier.is_some())
        .count();
    let Some(mapping) = &selector.identifier_error else {
        if count != 0 {
            return Err(ErrorReason::InvalidIdentifiers.into());
        }
        return Ok(TokenStream::new());
    };
    if count != variants.len() {
        return Err(ErrorReason::InvalidIdentifiers.into());
    }
    let (error_type, error_value) = error(mapping)?;
    let name = ident(&selector.name)?;
    let numbers: Vec<_> = selector
        .variants
        .iter()
        .filter_map(|v| v.identifier)
        .collect();
    if numbers.iter().collect::<BTreeSet<_>>().len() != numbers.len() {
        return Err(ErrorReason::InvalidIdentifiers.into());
    }
    Ok(quote! {
        impl #name {
            #[doc = "Exact external protocol identity, distinct from the protobuf number."]
            #[must_use]
            pub const fn identifier(self) -> ::core::primitive::i64 { match self { #(Self::#variants => #numbers,)* } }
            #[doc = "Resolve an exact external identity without policy substitution."]
            #[doc = ""]
            #[doc = "# Errors"]
            #[doc = "Returns the configured fixed domain error for an unsupported identity."]
            pub fn from_identifier(value: ::core::primitive::i64) -> ::core::result::Result<Self, #error_type> {
                match value {
                    #(#numbers => ::core::result::Result::Ok(Self::#variants),)*
                    _ => ::core::result::Result::Err(::core::convert::Into::into(#error_value)),
                }
            }
        }
    })
}

fn payload_type(
    schema: &Schema<'_>,
    field: &PayloadField,
    borrowed: bool,
) -> Result<TokenStream, AdapterError> {
    ident(&field.name)?;
    let descriptor = schema.field(&field.source)?;
    match &field.representation {
        PayloadType::Bytes => {
            if !borrowed {
                return Err(ErrorReason::OwnershipMismatch.into());
            }
            if descriptor.r#type != Some(Type::TYPE_BYTES) {
                return Err(ErrorReason::InvalidPayload.into());
            }
            Ok(quote! { &'a [::core::primitive::u8] })
        }
        PayloadType::Value { source, rust_type } => {
            if !matches!(
                descriptor.r#type,
                Some(Type::TYPE_ENUM | Type::TYPE_MESSAGE)
            ) || descriptor.type_name.as_deref() != Some(source)
            {
                return Err(ErrorReason::InvalidPayload.into());
            }
            match descriptor.r#type {
                Some(Type::TYPE_ENUM) => {
                    schema.enumeration(source)?;
                }
                Some(Type::TYPE_MESSAGE) => {
                    // A Copy compatibility value can aggregate closed selectors,
                    // but cannot smuggle byte owners or collections into a borrow.
                    let message = schema.message(source)?;
                    if message.field.is_empty()
                        || message.field.iter().any(|field| {
                            field.r#type != Some(Type::TYPE_ENUM)
                                || field.label == Some(Label::LABEL_REPEATED)
                                || !field
                                    .type_name
                                    .as_ref()
                                    .is_some_and(|name| schema.native_selectors.contains_key(name))
                        })
                    {
                        return Err(ErrorReason::OwnershipMismatch.into());
                    }
                }
                _ => return Err(ErrorReason::InvalidPayload.into()),
            }
            let value = path(rust_type)?;
            if descriptor.r#type == Some(Type::TYPE_ENUM) {
                let native = value
                    .segments
                    .last()
                    .ok_or(ErrorReason::InvalidRustPath)?
                    .ident
                    .to_string();
                if !schema
                    .native_selectors
                    .get(source)
                    .is_some_and(|names| names.contains(&native))
                {
                    return Err(ErrorReason::InvalidPayload.into());
                }
            }
            Ok(quote! { #value })
        }
    }
}

fn payload_fields(payload: &Payload) -> &[PayloadField] {
    match payload {
        Payload::Tuple { field } => std::slice::from_ref(field),
        Payload::Struct { fields } => fields,
        Payload::Unit | Payload::Port { .. } => &[],
    }
}

fn payload_enum(
    schema: &Schema<'_>,
    ports: &BTreeMap<&str, syn::Path>,
    name: &str,
    variants: &[PayloadVariant],
    cases: &BTreeMap<String, i32>,
    borrowed: bool,
    oneof: Option<&str>,
) -> Result<TokenStream, AdapterError> {
    let name = ident(name)?;
    let mut claimed = BTreeSet::new();
    let mut names = BTreeSet::new();
    let mut declarations = Vec::new();
    let mut has_borrow = false;
    let mut numeric_targets = BTreeMap::new();
    let mut copy_checks = Vec::new();
    for variant in variants {
        let native = ident(&variant.name)?;
        if !names.insert(variant.name.clone()) {
            return Err(ErrorReason::DuplicateName.into());
        }
        if variant.cases.is_empty() {
            return Err(ErrorReason::UncoveredCase.into());
        }
        for case in &variant.cases {
            let number = claim(cases, &mut claimed, case)?;
            if let Some(previous) = numeric_targets.insert(number, &variant.name) {
                if previous != &variant.name {
                    return Err(ErrorReason::InvalidNormalization.into());
                }
            }
        }
        let fields = payload_fields(&variant.payload);
        let mut field_names = BTreeSet::new();
        let mut sources = BTreeSet::new();
        for field in fields {
            if !field_names.insert(&field.name) {
                return Err(ErrorReason::DuplicateName.into());
            }
            let key = (&field.source.message, &field.source.path);
            if !sources.insert(key) {
                return Err(ErrorReason::DuplicateCase.into());
            }
            if fields.iter().any(|other| {
                other.source.message == field.source.message
                    && other.source.path != field.source.path
                    && other.source.path.starts_with(&field.source.path)
            }) {
                return Err(ErrorReason::DuplicateCase.into());
            }
            if matches!(field.representation, PayloadType::Value { .. }) {
                let ty = payload_type(schema, field, borrowed)?;
                copy_checks.push(quote! { __assert_copy::<#ty>(); });
            }
            if let Some(root) = oneof {
                if field.source.message != root
                    || !field
                        .source
                        .path
                        .first()
                        .is_some_and(|p| variant.cases.contains(p))
                {
                    return Err(ErrorReason::UnexpectedField.into());
                }
            }
        }
        if let Some(root) = oneof {
            if variant.cases.len() != 1 {
                return Err(ErrorReason::InvalidPayload.into());
            }
            for case in &variant.cases {
                complete_payload(schema, root, std::slice::from_ref(case), fields)?;
            }
        }
        let payload = match &variant.payload {
            Payload::Unit => TokenStream::new(),
            Payload::Tuple { field } => {
                has_borrow |= matches!(field.representation, PayloadType::Bytes);
                let ty = payload_type(schema, field, borrowed)?;
                quote! { (#ty) }
            }
            Payload::Struct { fields } => {
                if fields.is_empty() {
                    return Err(ErrorReason::InvalidPayload.into());
                }
                let mut members = Vec::new();
                for field in fields {
                    has_borrow |= matches!(field.representation, PayloadType::Bytes);
                    let field_name = ident(&field.name)?;
                    let ty = payload_type(schema, field, borrowed)?;
                    members.push(quote! { #[doc = "Projected canonical descriptor field."] #field_name: #ty, });
                }
                quote! { { #(#members)* } }
            }
            Payload::Port { name } => {
                if !borrowed || oneof.is_some() {
                    return Err(ErrorReason::OwnershipMismatch.into());
                }
                let port = ports
                    .get(name.as_str())
                    .ok_or(ErrorReason::InvalidPayload)?;
                has_borrow = true;
                quote! { (&'a dyn #port) }
            }
        };
        declarations
            .push(quote! { #[doc = "Descriptor-defined native alternative."] #native #payload, });
    }
    if claimed.len() != cases.len() {
        return Err(ErrorReason::UncoveredCase.into());
    }
    if borrowed && !has_borrow {
        return Err(ErrorReason::OwnershipMismatch.into());
    }
    let lifetime = borrowed.then(|| quote! { <'a> });
    // Borrowed bytes and capabilities must not inherit diagnostics or an owning
    // cleanup implementation. Copy payload selectors cannot hide an owner.
    let derives =
        (!borrowed).then(|| quote! { #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] });
    let copy_guard = (!copy_checks.is_empty()).then(|| {
        quote! {
            const _: fn() = || {
                fn __assert_copy<T: ::core::marker::Copy>() {}
                #(#copy_checks)*
            };
        }
    });
    Ok(quote! {
        #[doc = "Process-local adapter generated from authoritative descriptors."]
        #derives
        pub enum #name #lifetime { #(#declarations)* }
        #copy_guard
    })
}

fn complete_payload(
    schema: &Schema<'_>,
    root: &str,
    prefix: &[String],
    fields: &[PayloadField],
) -> Result<(), AdapterError> {
    let mut pending = vec![prefix.to_vec()];
    while let Some(current) = pending.pop() {
        if current.len() > MAX_FIELD_DEPTH {
            return Err(ErrorReason::InvalidPayload.into());
        }
        if fields
            .iter()
            .any(|f| f.source.message == root && f.source.path == current)
        {
            continue;
        }
        let field = schema.field(&FieldSource {
            message: root.to_owned(),
            path: current.clone(),
        })?;
        let descendants = fields
            .iter()
            .any(|f| f.source.message == root && f.source.path.starts_with(&current));
        if field.r#type != Some(Type::TYPE_MESSAGE) || !descendants {
            return Err(ErrorReason::UncoveredField.into());
        }
        let child = schema.message(
            field
                .type_name
                .as_deref()
                .ok_or(ErrorReason::InvalidDescriptor)?,
        )?;
        if child.field.is_empty() {
            return Err(ErrorReason::UncoveredField.into());
        }
        for member in &child.field {
            let mut next = current.clone();
            next.push(member.name.clone().ok_or(ErrorReason::InvalidDescriptor)?);
            pending.push(next);
        }
    }
    // A parent and its descendant cannot both be claimed as distinct payloads.
    for field in fields {
        if fields.iter().any(|other| {
            other.source.path != field.source.path
                && other.source.path.starts_with(&field.source.path)
        }) {
            return Err(ErrorReason::DuplicateCase.into());
        }
    }
    Ok(())
}

/// These names resolve to devices on Windows even with an `.rs` extension.
/// Apply the restriction on every host so generated files remain portable.
fn is_reserved_windows_file_stem(stem: &str) -> bool {
    if ["CON", "PRN", "AUX", "NUL"]
        .iter()
        .any(|reserved| stem.eq_ignore_ascii_case(reserved))
    {
        return true;
    }
    let Some((&number, prefix)) = stem.as_bytes().split_last() else {
        return false;
    };
    (b'1'..=b'9').contains(&number)
        && (prefix.eq_ignore_ascii_case(b"COM") || prefix.eq_ignore_ascii_case(b"LPT"))
}