zerodds-lang-rules 1.0.0-rc.7.1

Maps the common AST onto the language CSTs through rules that are data, not code: seesaw v2 rule specifications, comparable and checkable.
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
942
943
944
945
946
947
// SPDX-License-Identifier: Apache-2.0
// Copyright 2026 ZeroDDS Contributors
//! The Enhanced Service Mapping of DDS-RPC 1.0 §7.5.1.2 as Rust.
//!
//! `${I}_Request` and `${I}_Reply` are `@Choice @Autoid` structures
//! (§7.5.1.2.4, §7.5.1.2.5): "the structure shall be interpreted as if
//! the structure had the `@Extensibility(MUTABLE_EXTENSIBILITY)`
//! annotation and all the members of the structure had the `@Optional`
//! annotation. Furthermore, exactly one member shall be present at any
//! given time" (§7.5.1.2.1.1). A Rust `enum` is that: one variant per
//! member, and its codec is a PL_CDR2 object (XTypes 1.3 §7.4.3.4) with
//! the one member present: a `DHEADER`, an `EMHEADER` whose member id is
//! the HASH of the member name (§7.5.1.2.1.2) and the member.
//!
//! A member the binding does not know is a variant of its own,
//! `UnknownOp(member id)`, so that a service can answer
//! `REMOTE_EX_UNKNOWN_OPERATION` (§7.7.2) instead of failing to decode.
//!
//! The `In`, `Out` and `Result` structures reuse the Basic mapping's
//! members and codec where they are `@final`; an operation with
//! `@Mutable` gets the PL_CDR2 form of `In` and `Out` (§7.5.1.2.2,
//! §7.5.1.2.3 rule 1) and of `Result` (rule 7), which is an
//! `EMHEADER` per member.
//!
//! The generated client and dispatcher are the Basic mapping's, over
//! `${I}_Call` and `${I}_Return` declared as aliases of the two choice
//! types, and `zerodds_rpc::endpoint_options::RpcMapping::Enhanced`.
//! Their variants are the same, so the same match arms read both.

use super::*;
use crate::adapter::interface::{
    EnhancedMemberType, EnhancedOperation, EnhancedService, EnhancedStruct,
};

/// How a case of a choice structure is written inside its `EMHEADER`.
enum CaseBody {
    /// A structure with its own `encode`/`decode`, at this path.
    Payload(Path),
    /// `dds::rpc::RemoteExceptionCode_t`, an enumeration of four octets.
    RemoteException,
}

/// What a choice structure's decoder does with a member id it does not
/// know: it keeps the id in a variant of its own.
#[derive(Clone, Copy, PartialEq, Eq)]
enum Unknown {
    /// `UnknownOp(member id)`: a request for an operation the interface
    /// does not have (DDS-RPC 1.0 §7.7.2).
    Operation,
    /// `UnknownEx(member id)`: an exception member of a `Result` that
    /// this version of the interface does not declare (§7.7.2.5.6).
    Exception,
}

impl Unknown {
    /// The variant that holds the unknown member id.
    const fn variant(self) -> &'static str {
        match self {
            Self::Operation => UNKNOWN_OP_VARIANT,
            Self::Exception => UNKNOWN_EX_VARIANT,
        }
    }
}

/// One member of a choice structure.
struct ChoiceCase {
    variant: String,
    body: CaseBody,
    member_id: u32,
    doc: String,
}

fn path(parts: &[&str]) -> rust::Expr {
    rust::Expr::Path(Path::new(parts))
}

fn local(name: &str) -> rust::Expr {
    rust::Expr::Path(Path::single(name))
}

fn int(value: i128) -> rust::Expr {
    rust::Expr::Literal(Literal::int(value))
}

fn call(callee: rust::Expr, args: Vec<rust::Expr>) -> rust::Expr {
    rust::Expr::Call {
        callee: Box::new(callee),
        args,
    }
}

fn method(receiver: rust::Expr, name: &str, args: Vec<rust::Expr>) -> rust::Expr {
    rust::Expr::MethodCall {
        receiver: Box::new(receiver),
        method: Ident::new(name),
        args,
    }
}

fn try_(expr: rust::Expr) -> rust::Expr {
    rust::Expr::Try(Box::new(expr))
}

fn let_(name: &str, value: rust::Expr, mutable: bool) -> rust::Statement {
    rust::Statement::Let {
        name: Ident::new(name),
        type_: None,
        value: Some(value),
        mutable,
    }
}

fn field(receiver: rust::Expr, name: &str) -> rust::Expr {
    rust::Expr::Field {
        receiver: Box::new(receiver),
        name: Ident::new(name),
    }
}

/// `Err(WireError::refused(<position>, "<why>"))`.
fn refused(position_of: &str, why: &str) -> rust::Expr {
    call(
        local("Err"),
        vec![call(
            path(&["WireError", "refused"]),
            vec![
                method(local(position_of), "position", Vec::new()),
                rust::Expr::Literal(Literal::string(why)),
            ],
        )],
    )
}

fn ok(value: rust::Expr) -> rust::Expr {
    call(local("Ok"), vec![value])
}

/// The length code of an `EMHEADER` for a member of this kind
/// (XTypes 1.3 §7.4.3.4.2): 0 to 3 for a member of 1, 2, 4 or 8 octets,
/// 4 — a `NEXTINT` with the length — for any other.
pub(super) fn length_code(kind: &MemberWrite) -> u8 {
    match kind {
        MemberWrite::Primitive(primitive) => match primitive {
            PrimitiveKind::Boolean
            | PrimitiveKind::Octet
            | PrimitiveKind::Char
            | PrimitiveKind::Int8
            | PrimitiveKind::UInt8 => 0,
            PrimitiveKind::Int16 | PrimitiveKind::UInt16 => 1,
            PrimitiveKind::Int32 | PrimitiveKind::UInt32 | PrimitiveKind::Float32 => 2,
            PrimitiveKind::Int64 | PrimitiveKind::UInt64 | PrimitiveKind::Float64 => 3,
            _ => 4,
        },
        _ => 4,
    }
}

/// The path to a structure declared in module `module`, from module
/// `from`.
fn struct_path(
    ast: &Ast,
    from: Id<ModuleNode>,
    module: Id<ModuleNode>,
    name: &str,
) -> Result<Path, RustInterfaceError> {
    if from == module {
        Ok(Path::single(name))
    } else {
        relative_module_path(ast, from, module, name)
    }
}

/// The enum a choice structure is, and its `UnknownOp` variant when it
/// has one.
fn choice_enum(name: &str, doc: Doc, cases: &[ChoiceCase], unknown: Unknown) -> rust::Item {
    let mut variants = Vec::new();
    for case in cases {
        let payload = match &case.body {
            CaseBody::Payload(path) => rust::Type::named(path.clone()),
            CaseBody::RemoteException => rust::Type::named(Path::new(&[
                "zerodds_rpc",
                "common_types",
                "RemoteExceptionCode",
            ])),
        };
        variants.push(rust::Variant {
            doc: Doc::line(&case.doc),
            attrs: Vec::new(),
            name: Ident::new(&case.variant),
            body: rust::VariantBody::Tuple(vec![payload]),
            discriminant: None,
        });
    }
    {
        let variant = unknown.variant();
        let variant_doc = if unknown == Unknown::Exception {
            "An exception member this binding does not know, by its member id: the \
             client reports `REMOTE_EX_UNKNOWN_EXCEPTION` (DDS-RPC 1.0 §7.7.2.5.6)."
        } else {
            "A member this binding does not know, by its member id: a request for an \
             operation the interface does not have is answered with \
             `REMOTE_EX_UNKNOWN_OPERATION` (DDS-RPC 1.0 §7.7.2)."
        };
        variants.push(rust::Variant {
            doc: Doc::line(variant_doc),
            attrs: Vec::new(),
            name: Ident::new(variant),
            body: rust::VariantBody::Tuple(vec![rust::Type::Primitive(rust::Primitive::U32)]),
            discriminant: None,
        });
    }
    rust::Item::Enum(rust::Enum {
        doc,
        attrs: vec![allow_non_camel_case()],
        derives: data_derives(),
        visibility: Visibility::Public,
        name: Ident::new(name),
        generics: Vec::new(),
        variants,
    })
}

/// The `encode`/`decode` of a choice structure: PL_CDR2 with the one
/// member present.
fn choice_codec(name: &str, cases: &[ChoiceCase], unknown: Unknown) -> rust::Item {
    let mut encode_arms = Vec::new();
    let mut decode_arms = Vec::new();
    for case in cases {
        encode_arms.push(rust::MatchArm {
            pattern: rust::Pattern::TupleStruct {
                path: Path::new(&["Self", &case.variant]),
                elements: vec![rust::Pattern::Binding(Ident::new("value"))],
            },
            guard: None,
            body: rust::Expr::Block(rust::Block {
                statements: case_encode(case),
            }),
        });
        decode_arms.push(rust::MatchArm {
            pattern: rust::Pattern::Literal(Literal::int(i128::from(case.member_id))),
            guard: None,
            body: case_decode(case),
        });
    }
    let variant = unknown.variant();
    encode_arms.push(rust::MatchArm {
        pattern: rust::Pattern::TupleStruct {
            path: Path::new(&["Self", variant]),
            elements: vec![rust::Pattern::Wildcard],
        },
        guard: None,
        body: rust::Expr::Return(Some(Box::new(refused(
            "w",
            "an unknown member has no body to write",
        )))),
    });
    decode_arms.push(rust::MatchArm {
        pattern: rust::Pattern::Wildcard,
        guard: None,
        body: call(
            path(&["Self", variant]),
            vec![field(local("header"), "member_id")],
        ),
    });

    let encode_statements = vec![
        let_(
            "dheader",
            method(local("w"), "write_dheader", Vec::new()),
            false,
        ),
        rust::Statement::Semi(rust::Expr::Match {
            scrutinee: Box::new(local("self")),
            arms: encode_arms,
        }),
        rust::Statement::Semi(method(local("w"), "finish_dheader", vec![local("dheader")])),
        rust::Statement::Tail(ok_unit()),
    ];
    // The size of an `EMHEADER`: what is left after the one member, when
    // it is at least this much, is a second member.
    let emheader_size = || rust::Expr::Literal(Literal::int(4));
    let decode_statements = vec![
        let_(
            "length",
            try_(method(local("r"), "read_dheader", Vec::new())),
            false,
        ),
        // §7.5.1.2.1.1: "exactly one member shall be present at any given
        // time" — none is as wrong as two.
        rust::Statement::Semi(rust::Expr::If {
            condition: Box::new(rust::Expr::Binary {
                op: rust::BinaryOp::Lt,
                lhs: Box::new(local("length")),
                rhs: Box::new(emheader_size()),
            }),
            then: rust::Block {
                statements: vec![rust::Statement::Semi(rust::Expr::Return(Some(Box::new(
                    refused("r", "no member of the @Choice structure is present"),
                ))))],
            },
            otherwise: None,
        }),
        let_(
            "end",
            rust::Expr::Binary {
                op: rust::BinaryOp::Add,
                lhs: Box::new(method(local("r"), "position", Vec::new())),
                rhs: Box::new(local("length")),
            },
            false,
        ),
        let_(
            "header",
            try_(method(local("r"), "read_emheader", Vec::new())),
            false,
        ),
        let_(
            "after",
            rust::Expr::Binary {
                op: rust::BinaryOp::Add,
                lhs: Box::new(method(local("r"), "position", Vec::new())),
                rhs: Box::new(field(local("header"), "extent")),
            },
            false,
        ),
        let_(
            "value",
            rust::Expr::Match {
                scrutinee: Box::new(field(local("header"), "member_id")),
                arms: decode_arms,
            },
            false,
        ),
        // Whatever the member did not read is skipped. A further member
        // behind it is two options active in a `@Choice` structure
        // (§7.5.1.2.1.1); the service answers `REMOTE_EX_INVALID_ARGUMENT`
        // for the request that cannot be decoded (§7.5.2).
        rust::Statement::Semi(try_(method(local("r"), "skip_to", vec![local("after")]))),
        rust::Statement::Semi(rust::Expr::If {
            condition: Box::new(rust::Expr::Binary {
                op: rust::BinaryOp::Ge,
                lhs: Box::new(local("end")),
                rhs: Box::new(rust::Expr::Binary {
                    op: rust::BinaryOp::Add,
                    lhs: Box::new(method(local("r"), "position", Vec::new())),
                    rhs: Box::new(emheader_size()),
                }),
            }),
            then: rust::Block {
                statements: vec![rust::Statement::Semi(rust::Expr::Return(Some(Box::new(
                    refused("r", "two members of the @Choice structure are present"),
                ))))],
            },
            otherwise: None,
        }),
        rust::Statement::Semi(try_(method(local("r"), "skip_to", vec![local("end")]))),
        rust::Statement::Tail(ok(local("value"))),
    ];
    rust::Item::Impl(rust::Impl {
        doc: Doc::none(),
        attrs: Vec::new(),
        generics: Vec::new(),
        trait_: None,
        self_type: rust::Type::named(Path::single(name)),
        items: vec![
            encode_fn_item_named("encode", name, encode_statements),
            decode_fn_item_named("decode", name, decode_statements),
        ],
    })
}

/// The statements of one arm of a choice's `encode`.
fn case_encode(case: &ChoiceCase) -> Vec<rust::Statement> {
    let (code, write) = match &case.body {
        CaseBody::Payload(_) => (4, try_(method(local("value"), "encode", vec![local("w")]))),
        CaseBody::RemoteException => (
            2,
            method(
                local("w"),
                "write_u32",
                vec![method(local("value"), "as_u32", Vec::new())],
            ),
        ),
    };
    vec![
        let_(
            "member",
            try_(method(
                local("w"),
                "write_emheader",
                vec![
                    int(i128::from(case.member_id)),
                    int(code),
                    rust::Expr::Literal(Literal::Bool(false)),
                ],
            )),
            false,
        ),
        rust::Statement::Semi(write),
        rust::Statement::Semi(try_(method(
            local("w"),
            "finish_emheader",
            vec![local("member")],
        ))),
    ]
}

/// The value one arm of a choice's `decode` reads.
fn case_decode(case: &ChoiceCase) -> rust::Expr {
    match &case.body {
        CaseBody::Payload(payload) => call(
            path(&["Self", &case.variant]),
            vec![try_(call(
                rust::Expr::Path(payload.join("decode")),
                vec![local("r")],
            ))],
        ),
        CaseBody::RemoteException => call(
            path(&["Self", &case.variant]),
            vec![rust::Expr::Match {
                scrutinee: Box::new(call(
                    path(&[
                        "zerodds_rpc",
                        "common_types",
                        "RemoteExceptionCode",
                        "from_u32",
                    ]),
                    vec![try_(method(local("r"), "read_u32", Vec::new()))],
                )),
                arms: vec![
                    rust::MatchArm {
                        pattern: rust::Pattern::TupleStruct {
                            path: Path::single("Ok"),
                            elements: vec![rust::Pattern::Binding(Ident::new("code"))],
                        },
                        guard: None,
                        body: local("code"),
                    },
                    rust::MatchArm {
                        pattern: rust::Pattern::Wildcard,
                        guard: None,
                        body: rust::Expr::Return(Some(Box::new(refused(
                            "r",
                            "a remote exception code outside DDS-RPC 1.0 §7.5.2",
                        )))),
                    },
                ],
            }],
        ),
    }
}

/// The PL_CDR2 form of a structure with `@Mutable`: a `DHEADER`, then an
/// `EMHEADER` and the value of each member, in any order on decode.
fn mutable_struct_codec(
    ast: &Ast,
    from: Id<ModuleNode>,
    type_name: &str,
    fields: &[FieldSpec],
    ids: &[u32],
) -> Result<rust::Item, RustInterfaceError> {
    let mut encode_statements = vec![let_(
        "dheader",
        method(local("w"), "write_dheader", Vec::new()),
        false,
    )];
    let mut decode_statements = vec![
        let_(
            "length",
            try_(method(local("r"), "read_dheader", Vec::new())),
            false,
        ),
        let_(
            "end",
            rust::Expr::Binary {
                op: rust::BinaryOp::Add,
                lhs: Box::new(method(local("r"), "position", Vec::new())),
                rhs: Box::new(local("length")),
            },
            false,
        ),
    ];
    let mut slots = Vec::new();
    let mut arms = Vec::new();
    let mut struct_fields = Vec::new();
    for (spec, id) in fields.iter().zip(ids) {
        let (name, kind) = match spec {
            FieldSpec::Real(name, target) => (
                name.clone(),
                member_write_with(ast, from, *target, CodecVariant::Xcdr2),
            ),
            FieldSpec::Octet(name) => (name.clone(), MemberWrite::Primitive(PrimitiveKind::Octet)),
        };
        let code = length_code(&kind);
        encode_statements.push(let_(
            "member",
            try_(method(
                local("w"),
                "write_emheader",
                vec![
                    int(i128::from(*id)),
                    int(i128::from(code)),
                    rust::Expr::Literal(Literal::Bool(false)),
                ],
            )),
            false,
        ));
        encode_statements.push(write_value_to("w", &name, &kind, self_field(&name), false)?);
        encode_statements.push(rust::Statement::Semi(try_(method(
            local("w"),
            "finish_emheader",
            vec![local("member")],
        ))));

        let slot = format!("slot_{name}");
        slots.push(let_(&slot, local("None"), true));
        // One `if` per member and not a `match`: a structure of one member
        // would be a `match` of one arm.
        arms.push(rust::Statement::Semi(rust::Expr::If {
            condition: Box::new(rust::Expr::Binary {
                op: rust::BinaryOp::Eq,
                lhs: Box::new(field(local("header"), "member_id")),
                rhs: Box::new(int(i128::from(*id))),
            }),
            then: rust::Block {
                statements: vec![rust::Statement::Semi(rust::Expr::Assign {
                    target: Box::new(local(&slot)),
                    value: Box::new(call(local("Some"), vec![try_(read_for(&name, &kind)?)])),
                })],
            },
            otherwise: None,
        }));
        let absent_default = match spec {
            FieldSpec::Real(_, target) => {
                crate::default_rust::default_of(ast, from, *target, &name)?
            }
            FieldSpec::Octet(_) => int(0),
        };
        struct_fields.push((
            Ident::new(&name),
            // XTypes 1.3 §7.2.4.4.8 (assignability of aggregated types):
            // "Each non-optional member in a T1 object that is not present
            // in the T2 object takes its default value." A peer that does
            // not send a member of a mutable type, one that predates it, is
            // read with the default.
            method(local(&slot), "unwrap_or", vec![absent_default]),
        ));
    }
    encode_statements.push(rust::Statement::Semi(method(
        local("w"),
        "finish_dheader",
        vec![local("dheader")],
    )));
    encode_statements.push(rust::Statement::Tail(ok_unit()));

    decode_statements.extend(slots);
    decode_statements.push(rust::Statement::Semi(rust::Expr::While {
        condition: Box::new(rust::Expr::Binary {
            op: rust::BinaryOp::Lt,
            lhs: Box::new(method(local("r"), "position", Vec::new())),
            rhs: Box::new(local("end")),
        }),
        body: rust::Block {
            statements: vec![
                let_(
                    "header",
                    try_(method(local("r"), "read_emheader", Vec::new())),
                    false,
                ),
                let_(
                    "after",
                    rust::Expr::Binary {
                        op: rust::BinaryOp::Add,
                        lhs: Box::new(method(local("r"), "position", Vec::new())),
                        rhs: Box::new(field(local("header"), "extent")),
                    },
                    false,
                ),
            ]
            .into_iter()
            .chain(arms)
            .chain([rust::Statement::Semi(try_(method(
                local("r"),
                "skip_to",
                vec![local("after")],
            )))])
            .collect(),
        },
    }));
    decode_statements.push(rust::Statement::Tail(ok(rust::Expr::StructLit {
        path: Path::single("Self"),
        fields: struct_fields,
        base: None,
    })));

    Ok(rust::Item::Impl(rust::Impl {
        doc: Doc::none(),
        attrs: Vec::new(),
        generics: Vec::new(),
        trait_: None,
        self_type: rust::Type::named(Path::single(type_name)),
        items: vec![
            encode_fn_item_named("encode", type_name, encode_statements),
            decode_fn_item_named("decode", type_name, decode_statements),
        ],
    }))
}

/// The member ids of a structure's members, in order.
fn ids_of(structure: &EnhancedStruct) -> Vec<u32> {
    structure.members.iter().map(|m| m.member_id).collect()
}

/// The cases of a `Result` structure (§7.5.1.2.3).
fn result_cases(
    ast: &Ast,
    module: Id<ModuleNode>,
    operation: &EnhancedOperation,
) -> Result<Vec<ChoiceCase>, RustInterfaceError> {
    let mut cases = Vec::new();
    for member in &operation.result_struct.members {
        match &member.type_ {
            EnhancedMemberType::Struct(name) => cases.push(ChoiceCase {
                variant: "Ok".to_string(),
                body: CaseBody::Payload(Path::single(name)),
                member_id: member.member_id,
                doc: "`result`. DDS-RPC 1.0 §7.5.1.2.3 rule 5.".to_string(),
            }),
            EnhancedMemberType::Exception(raise) => cases.push(ChoiceCase {
                variant: rust::escape_keyword(&raise.name),
                body: CaseBody::Payload(exception_path(ast, module, raise.id)?),
                member_id: member.member_id,
                doc: format!("`{}`. DDS-RPC 1.0 §7.5.1.2.3 rule 6.", member.name),
            }),
            _ => {}
        }
    }
    Ok(cases)
}

/// The cases of `${I}_Request` or `${I}_Reply`.
fn service_cases(
    ast: &Ast,
    module: Id<ModuleNode>,
    service: &EnhancedService,
    reply: bool,
) -> Result<Vec<ChoiceCase>, RustInterfaceError> {
    let structure = if reply {
        &service.reply
    } else {
        &service.request
    };
    let mut cases = Vec::new();
    for member in &structure.members {
        match &member.type_ {
            EnhancedMemberType::Struct(name) => {
                let owner = service
                    .operations
                    .iter()
                    .find(|operation| operation.operation.name == member.name)
                    .map_or(module, |operation| operation.module);
                cases.push(ChoiceCase {
                    variant: variant_name(&member.name),
                    body: CaseBody::Payload(struct_path(ast, module, owner, name)?),
                    member_id: member.member_id,
                    doc: format!(
                        "`{}`. DDS-RPC 1.0 §7.5.1.2.{}.",
                        member.name,
                        if reply { 5 } else { 4 }
                    ),
                });
            }
            EnhancedMemberType::RemoteException => cases.push(ChoiceCase {
                variant: "RemoteEx".to_string(),
                body: CaseBody::RemoteException,
                member_id: member.member_id,
                doc: format!(
                    "`{}`: the remote exception code. DDS-RPC 1.0 §7.5.1.2.5 rule 5.",
                    member.name
                ),
            }),
            _ => {}
        }
    }
    Ok(cases)
}

/// What `declare` writes for one interface under the Enhanced Service
/// Mapping.
pub(super) fn declare(
    ast: &Ast,
    service: &Service,
    out: &mut Vec<(Id<ModuleNode>, rust::Item)>,
) -> Result<(), RustInterfaceError> {
    // The client and the dispatcher are shared with the Basic mapping and
    // read which of the two they bind off the service.
    let service = &Service {
        mapping: if service.mapping.is_enhanced() {
            service.mapping
        } else {
            interface::ServiceMapping::Enhanced
        },
        ..service.clone()
    };
    let module = service.module;
    let ops = service.all_operations();
    let model = interface::enhanced_service(ast, service, service.mapping.empty_style())?;

    // The exceptions the interface's own operations raise, one codec each
    // in the exception's module.
    for raise in exceptions_of(&ops) {
        out.push((
            exception_module(ast, raise.id)?,
            exception_codec_item(ast, raise.id)?,
        ));
    }

    for operation in model
        .operations
        .iter()
        .filter(|operation| operation.module == module && operation.interface == service.name)
    {
        let op = &operation.operation;
        out.push((module, rust::Item::Struct(in_struct(ast, service, op)?)));
        let in_name = &operation.in_struct.name;
        if operation.in_struct.mutable {
            out.push((
                module,
                mutable_struct_codec(
                    ast,
                    module,
                    in_name,
                    &in_fields_styled(op, service.mapping.empty_style()),
                    &ids_of(&operation.in_struct),
                )?,
            ));
        } else {
            out.push((
                module,
                struct_codec_item(
                    ast,
                    module,
                    in_name,
                    &in_fields_styled(op, service.mapping.empty_style()),
                )?,
            ));
        }
        out.push((module, rust::Item::Struct(out_struct(ast, service, op)?)));
        let out_name = &operation.out_struct.name;
        if operation.out_struct.mutable {
            out.push((
                module,
                mutable_struct_codec(
                    ast,
                    module,
                    out_name,
                    &out_fields_styled(op, service.mapping.empty_style()),
                    &ids_of(&operation.out_struct),
                )?,
            ));
        } else {
            out.push((
                module,
                struct_codec_item(
                    ast,
                    module,
                    out_name,
                    &out_fields_styled(op, service.mapping.empty_style()),
                )?,
            ));
        }
        let cases = result_cases(ast, module, operation)?;
        out.push((
            module,
            choice_enum(
                &operation.result_struct.name,
                Doc::line(&format!(
                    "The outcome of `{}`: a `@Choice` over the result and the exceptions. \
                     DDS-RPC 1.0 §7.5.1.2.3.",
                    op.name
                )),
                &cases,
                Unknown::Exception,
            ),
        ));
        out.push((
            module,
            choice_codec(&operation.result_struct.name, &cases, Unknown::Exception),
        ));
    }

    let request = service_cases(ast, module, &model, false)?;
    let request_name = interface::request_type_name(&service.name);
    out.push((
        module,
        choice_enum(
            &request_name,
            Doc::line(
                "The request topic type: a `@Choice @Autoid` over every operation, the \
                 inherited ones included. DDS-RPC 1.0 §7.5.1.2.4.",
            ),
            &request,
            Unknown::Operation,
        ),
    ));
    out.push((
        module,
        choice_codec(&request_name, &request, Unknown::Operation),
    ));
    let reply = service_cases(ast, module, &model, true)?;
    let reply_name = interface::reply_type_name(&service.name);
    out.push((
        module,
        choice_enum(
            &reply_name,
            Doc::line(
                "The reply topic type: a `@Choice @Autoid` over every operation's result and \
                 `remoteEx`. DDS-RPC 1.0 §7.5.1.2.5.",
            ),
            &reply,
            Unknown::Operation,
        ),
    ));
    out.push((
        module,
        choice_codec(&reply_name, &reply, Unknown::Operation),
    ));

    // The Basic mapping's client and dispatcher name `${I}_Call` and
    // `${I}_Return`; here they are the two choice types.
    for (alias, target) in [
        (interface::call_union_name(&service.name), &request_name),
        (interface::return_union_name(&service.name), &reply_name),
    ] {
        out.push((
            module,
            rust::Item::TypeAlias(rust::TypeAlias {
                doc: Doc::line(
                    "The name the client and the dispatcher of this binding use for the topic type.",
                ),
                attrs: vec![allow_non_camel_case()],
                visibility: Visibility::Public,
                name: Ident::new(&alias),
                generics: Vec::new(),
                type_: rust::Type::named(Path::single(target)),
            }),
        ));
    }

    let exceptions = exceptions_of(&ops);
    out.push((
        module,
        rust::Item::Enum(error_enum(ast, module, service, &exceptions)?),
    ));
    out.push((module, dds_type_impl(&request_name, &request_name, false)));
    out.push((module, dds_type_impl(&reply_name, &reply_name, false)));

    if service.is_remote() {
        out.push((module, rust::Item::Trait(service_trait(ast, service)?)));
        out.push((module, rust::Item::Struct(client_struct(ast, service)?)));
        out.push((module, client_impl(ast, service)?));
        out.push((module, rust::Item::Struct(dispatcher_struct(service))));
        // §7.5.1.2.6: the request choice holds the inherited operations too,
        // and the derived service implements the base trait, so the derived
        // dispatcher serves them with the arms the base's dispatcher has.
        let mut inherited = Vec::new();
        for operation in model
            .operations
            .iter()
            .filter(|operation| operation.module != module || operation.interface != service.name)
        {
            let declaring = Service {
                name: operation.interface.clone(),
                module: operation.module,
                ..service.clone()
            };
            let owner = operation.module;
            inherited.push(dispatcher_call_arm_via(
                service,
                &declaring,
                &operation.operation,
                &|name| struct_path(ast, module, owner, name),
            )?);
        }
        for item in dispatcher_impl(ast, service)? {
            out.push((
                module,
                answer_unknown_operations(item, service, inherited.clone()),
            ));
        }
    }
    Ok(())
}

/// The dispatcher's `handle`, as the Enhanced mapping answers it.
///
/// A member the binding does not know is a request for an operation the
/// interface does not have: `REMOTE_EX_UNKNOWN_OPERATION` (DDS-RPC 1.0
/// §7.7.2; the Basic mapping's `REMOTE_EX_UNSUPPORTED` is §7.7.1.1). The
/// request choice of an interface with a base has a variant for each
/// inherited operation too; `inherited` holds an arm for each, which routes
/// it to the base trait the derived service implements.
fn answer_unknown_operations(
    item: rust::Item,
    service: &Service,
    inherited: Vec<rust::MatchArm>,
) -> rust::Item {
    let rust::Item::Impl(mut implementation) = item else {
        return item;
    };
    let unknown = Path::new(&[
        &interface::call_union_name(&service.name),
        UNKNOWN_OP_VARIANT,
    ]);
    let answer = || {
        call(
            local("Err"),
            vec![rust::Expr::Path(remote_exception_code("UnknownOperation"))],
        )
    };
    for member in &mut implementation.items {
        let rust::Item::Function(function) = member else {
            continue;
        };
        if function.name.as_str() != "handle" {
            continue;
        }
        let Some(body) = function.body.as_mut() else {
            continue;
        };
        for statement in &mut body.statements {
            if let rust::Statement::Tail(rust::Expr::Match { arms, .. }) = statement {
                for arm in arms.iter_mut() {
                    if matches!(
                        &arm.pattern,
                        rust::Pattern::TupleStruct { path, .. } if *path == unknown
                    ) {
                        arm.body = answer();
                    }
                }
                // The inherited operations are served, one arm each.
                arms.extend(inherited.iter().cloned());
            }
        }
    }
    rust::Item::Impl(implementation)
}