club-kdl-codegen 0.11.1

Generate Rust / TypeScript / Zod / SurrealQL code from KDL schema files
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
//! Rust emitter — renders [`ir::Schema`] into Rust source text.
//!
//! Ported from club-unison's `codegen/rust.rs`. The original used
//! `proc_macro2` + `quote` to build a `TokenStream` and a hand-rolled
//! `format_code` pass; this port writes pre-formatted Rust text directly so
//! `club-kdl-codegen` stays dependency-free during Phase 1.
//!
//! ## What it emits
//!
//! - data dialect: every [`ir::TypeDef`] — `struct` (with fields) and `enum`
//!   (string-valued variants).
//! - entity dialect: every [`ir::Record`] as a `struct` carrying an `id`
//!   field; every [`ir::Relation`] as an edge `struct` carrying `id` / `in` /
//!   `out` fields plus its edge properties.
//! - protocol dialect: for every [`ir::Channel`], a `struct` per request
//!   payload, per `returns` message, and per event payload.
//!
//! ## Tier 1 type mapping
//!
//! - `link<Record>` → `String` (the linked record's id).
//! - `'literal'` and unions of literals → a generated string-valued `enum`
//!   is *not* produced (no schema name is available at the field site);
//!   instead the field type degrades to `String`. A union of non-literal
//!   types also degrades to `serde_json::Value` — Rust has no anonymous sum
//!   type, and inventing names per field is out of Tier 1 scope.
//!
//! Each generated `struct` / `enum` carries `#[derive(...)]` attributes and
//! `serde` annotations matching club-unison's generator. Optional fields
//! become `Option<T>` with `#[serde(skip_serializing_if = "Option::is_none")]`.
//!
//! ## Differences from club-unison (IR-driven port)
//!
//! - The IR has no inline `_inline_*` messages and no `service` / `method` /
//!   `stream` / `send` / `recv` legacy constructs — so the corresponding
//!   branches are dropped.
//! - The IR's [`ir::Prim::Datetime`] maps to `chrono::DateTime<Utc>`; named
//!   type references emit the bare identifier (no `TypeRegistry` indirection).
//!
//! ## Tier 2 — description / constraints
//!
//! - A `description` on a `struct` / `enum` / `record` / `relation` or a
//!   field becomes a `///` doc comment.
//! - Field `constraints` (`min` / `max` / `min_length` / `max_length` /
//!   `pattern`) are **not** emitted — Rust's type system cannot express them,
//!   and JSDoc-style `@minimum` hacks are deliberately avoided.

use crate::Emitter;
use crate::ir;

use super::case::{to_pascal_case, to_snake_case};

/// The Rust code generation target.
#[derive(Debug, Default, Clone, Copy)]
pub struct RustEmitter;

impl RustEmitter {
    /// Create a new [`RustEmitter`].
    pub fn new() -> Self {
        Self
    }
}

impl Emitter for RustEmitter {
    fn emit(&self, schema: &ir::Schema) -> String {
        let mut out = String::new();
        out.push_str(IMPORTS);

        // data dialect — standalone type definitions.
        for ty in &schema.types {
            out.push('\n');
            out.push_str(&render_typedef(ty));
        }

        // entity dialect — records and relations.
        for record in &schema.records {
            out.push('\n');
            out.push_str(&render_record(record));
        }
        for relation in &schema.relations {
            out.push('\n');
            out.push_str(&render_relation(relation));
        }

        // protocol dialect — channel payload structs.
        if let Some(protocol) = &schema.protocol {
            for channel in &protocol.channels {
                out.push_str(&render_channel(channel));
            }
        }

        out
    }
}

/// Header import block, matching club-unison's `generate_imports`.
const IMPORTS: &str = "\
use serde::{Deserialize, Serialize};
use anyhow::Result;
use chrono::{DateTime, Utc};
use uuid::Uuid;
use std::collections::HashMap;
";

/// Render one standalone [`ir::TypeDef`].
fn render_typedef(ty: &ir::TypeDef) -> String {
    match ty {
        ir::TypeDef::Struct {
            name,
            description,
            fields,
        } => render_struct(name, description.as_deref(), fields),
        ir::TypeDef::Enum {
            name,
            description,
            variants,
        } => render_enum(name, description.as_deref(), variants),
    }
}

/// Render a `///` doc comment block at the given indentation from an optional
/// description. Each line of a multi-line description gets its own `///`.
fn render_doc(description: Option<&str>, indent: &str) -> String {
    match description {
        Some(text) => text
            .lines()
            .map(|line| format!("{indent}/// {line}\n"))
            .collect(),
        None => String::new(),
    }
}

/// Render a `struct` from a name and field list. A fieldless struct becomes a
/// unit struct (`pub struct Name;`), matching club-unison.
fn render_struct(name: &str, description: Option<&str>, fields: &[ir::Field]) -> String {
    let derive = "#[derive(Debug, Clone, Serialize, Deserialize)]\n";
    let doc = render_doc(description, "");
    if fields.is_empty() {
        return format!("{doc}{derive}pub struct {name};\n");
    }
    let mut out = String::new();
    out.push_str(&doc);
    out.push_str(derive);
    out.push_str(&format!("pub struct {name} {{\n"));
    for field in fields {
        out.push_str(&render_field(field));
    }
    out.push_str("}\n");
    out
}

/// Render an `enum` of string-valued variants.
fn render_enum(name: &str, description: Option<&str>, variants: &[String]) -> String {
    let mut out = String::new();
    out.push_str(&render_doc(description, ""));
    out.push_str("#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]\n");
    out.push_str("#[serde(rename_all = \"snake_case\")]\n");
    out.push_str(&format!("pub enum {name} {{\n"));
    for v in variants {
        out.push_str(&format!("    #[serde(rename = \"{v}\")]\n"));
        out.push_str(&format!("    {},\n", to_pascal_case(v)));
    }
    out.push_str("}\n");
    out
}

/// Render a single struct field with its `serde` attributes.
fn render_field(field: &ir::Field) -> String {
    let mut out = String::new();

    // `///` doc comment from the field description.
    out.push_str(&render_doc(field.description.as_deref(), "    "));

    // `#[serde(rename = "...")]` when the source name is not snake_case.
    let snake = to_snake_case(&field.name);
    if field.name != snake {
        out.push_str(&format!("    #[serde(rename = \"{}\")]\n", field.name));
    }

    let base = ty_to_rust(&field.ty);
    let rust_ty = if field.required {
        base
    } else {
        out.push_str("    #[serde(skip_serializing_if = \"Option::is_none\")]\n");
        format!("Option<{base}>")
    };

    out.push_str(&format!(
        "    pub {}: {rust_ty},\n",
        field_ident(&field.name)
    ));
    out
}

/// Render a field name as a valid Rust identifier. A name that collides with
/// a Rust keyword is escaped as a raw identifier (`r#type`) so the generated
/// source compiles. `serde` strips the `r#` prefix, so the wire name is
/// unaffected.
///
/// `crate` / `self` / `Self` / `super` cannot be raw identifiers; they are
/// left as-is (a KDL schema field is extremely unlikely to use them).
fn field_ident(name: &str) -> String {
    const KEYWORDS: &[&str] = &[
        "as", "break", "const", "continue", "dyn", "else", "enum", "extern", "false", "fn", "for",
        "if", "impl", "in", "let", "loop", "match", "mod", "move", "mut", "pub", "ref", "return",
        "static", "struct", "trait", "true", "type", "unsafe", "use", "where", "while", "async",
        "await", "gen", "abstract", "become", "box", "do", "final", "macro", "override", "priv",
        "try", "typeof", "unsized", "virtual", "yield",
    ];
    if KEYWORDS.contains(&name) {
        format!("r#{name}")
    } else {
        name.to_string()
    }
}

/// Map an [`ir::Ty`] to its Rust type expression.
fn ty_to_rust(ty: &ir::Ty) -> String {
    match ty {
        ir::Ty::Primitive(p) => prim_to_rust(*p).to_string(),
        ir::Ty::Array(inner) => format!("Vec<{}>", ty_to_rust(inner)),
        ir::Ty::Named(name) => name.clone(),
        // a link stores the target record's id — a plain string.
        ir::Ty::Link(_) => "String".to_string(),
        // a literal degrades to String (no per-field type name available).
        ir::Ty::Literal(_) => "String".to_string(),
        // a union of string literals stays a String. Otherwise, if every
        // member maps to the same Rust type, collapse to it (`link<X> |
        // string` → `String`); a genuinely heterogeneous union has no Rust
        // anonymous-sum representation, so it degrades to a JSON value.
        ir::Ty::Union(members) => {
            if members.iter().all(|m| matches!(m, ir::Ty::Literal(_))) {
                "String".to_string()
            } else {
                let mut mapped: Vec<String> = Vec::new();
                for m in members {
                    let t = ty_to_rust(m);
                    if !mapped.contains(&t) {
                        mapped.push(t);
                    }
                }
                if mapped.len() == 1 {
                    mapped.into_iter().next().unwrap()
                } else {
                    "serde_json::Value".to_string()
                }
            }
        }
    }
}

/// Map an [`ir::Prim`] to its Rust type.
fn prim_to_rust(p: ir::Prim) -> &'static str {
    match p {
        ir::Prim::String => "String",
        ir::Prim::Int => "i64",
        ir::Prim::Float => "f64",
        ir::Prim::Bool => "bool",
        ir::Prim::Datetime => "DateTime<Utc>",
        ir::Prim::Json => "serde_json::Value",
    }
}

/// Render one [`ir::Record`] as a `struct`. The record's `id` becomes a
/// leading `String` field; the remaining fields follow in source order. The
/// struct name is PascalCased so a camelCase record name still compiles.
fn render_record(record: &ir::Record) -> String {
    let mut fields = Vec::with_capacity(record.fields.len() + 1);
    fields.push(id_field());
    fields.extend(record.fields.iter().cloned());
    render_struct(
        &to_pascal_case(&record.name),
        record.description.as_deref(),
        &fields,
    )
}

/// Render one [`ir::Relation`] as an edge `struct` carrying `id` / `in` /
/// `out` (the edge endpoints, as record ids) plus its edge-property fields.
fn render_relation(relation: &ir::Relation) -> String {
    let mut fields = Vec::with_capacity(relation.fields.len() + 3);
    fields.push(id_field());
    fields.push(ir::Field {
        name: "in".to_string(),
        ty: ir::Ty::Primitive(ir::Prim::String),
        required: true,
        flexible: false,
        default: None,
        description: None,
        constraints: ir::Constraints::default(),
    });
    fields.push(ir::Field {
        name: "out".to_string(),
        ty: ir::Ty::Primitive(ir::Prim::String),
        required: true,
        flexible: false,
        default: None,
        description: None,
        constraints: ir::Constraints::default(),
    });
    fields.extend(relation.fields.iter().cloned());
    render_struct(
        &to_pascal_case(&relation.name),
        relation.description.as_deref(),
        &fields,
    )
}

/// The synthetic `id: String` field shared by records and relations.
fn id_field() -> ir::Field {
    ir::Field {
        name: "id".to_string(),
        ty: ir::Ty::Primitive(ir::Prim::String),
        required: true,
        flexible: false,
        default: None,
        description: None,
        constraints: ir::Constraints::default(),
    }
}

/// Render every payload struct for one channel: request payloads, `returns`
/// messages, and event payloads. Payload names are PascalCased so a wire-style
/// schema name (`process:toggle`) becomes a valid Rust identifier
/// (`ProcessToggle`).
///
/// When the channel declares `envelope="<tag>"`, a discriminated-union enum
/// bundling every request payload is appended (see [`render_envelope_enum`]).
fn render_channel(channel: &ir::Channel) -> String {
    let mut out = String::new();
    for req in &channel.requests {
        out.push('\n');
        out.push_str(&render_struct(
            &to_pascal_case(&req.name),
            None,
            &req.fields,
        ));
        if let Some(returns) = &req.returns {
            out.push('\n');
            out.push_str(&render_struct(
                &to_pascal_case(&returns.name),
                None,
                &returns.fields,
            ));
        }
    }
    for evt in &channel.events {
        out.push('\n');
        out.push_str(&render_struct(
            &to_pascal_case(&evt.name),
            None,
            &evt.fields,
        ));
    }
    if let Some(tag) = &channel.envelope
        && !channel.requests.is_empty()
    {
        out.push('\n');
        out.push_str(&render_envelope_enum(channel, tag));
    }
    out
}

/// Render the channel's envelope `enum`: an internally `#[serde(tag = "...")]`
/// discriminated union bundling every request payload.
///
/// A request carrying fields becomes a newtype variant wrapping its payload
/// struct (`ProcessToggle(ProcessToggle)`); a fieldless request becomes a unit
/// variant (`ProcessAdd`). The unit form is required — serde rejects an
/// internally tagged newtype variant that wraps a unit struct at runtime.
///
/// The variant identifier is the PascalCased request name; the original
/// (possibly `:`-bearing) wire name is preserved with `#[serde(rename = ...)]`
/// whenever sanitizing changed it.
fn render_envelope_enum(channel: &ir::Channel, tag: &str) -> String {
    let enum_name = format!("{}Envelope", to_pascal_case(&channel.name));
    let mut out = String::new();
    out.push_str(&format!(
        "/// Envelope enum for channel {:?} — a discriminated union over its\n\
         /// requests, internally tagged by the {tag:?} field.\n",
        channel.name
    ));
    out.push_str("#[derive(Debug, Clone, Serialize, Deserialize)]\n");
    out.push_str(&format!("#[serde(tag = \"{tag}\")]\n"));
    out.push_str(&format!("pub enum {enum_name} {{\n"));
    for req in &channel.requests {
        let variant = to_pascal_case(&req.name);
        if variant != req.name {
            out.push_str(&format!("    #[serde(rename = \"{}\")]\n", req.name));
        }
        if req.fields.is_empty() {
            out.push_str(&format!("    {variant},\n"));
        } else {
            out.push_str(&format!("    {variant}({variant}),\n"));
        }
    }
    out.push_str("}\n");
    out
}

#[cfg(test)]
mod tests {
    use super::*;

    fn field(name: &str, ty: ir::Ty, required: bool) -> ir::Field {
        ir::Field {
            name: name.to_string(),
            ty,
            required,
            flexible: false,
            default: None,
            description: None,
            constraints: ir::Constraints::default(),
        }
    }

    #[test]
    fn emits_import_header() {
        let out = RustEmitter::new().emit(&ir::Schema::default());
        assert!(out.contains("use serde::{Deserialize, Serialize};"));
        assert!(out.contains("use chrono::{DateTime, Utc};"));
    }

    #[test]
    fn emits_struct_with_required_field() {
        let schema = ir::Schema {
            types: vec![ir::TypeDef::Struct {
                name: "User".to_string(),
                description: None,
                fields: vec![field("name", ir::Ty::Primitive(ir::Prim::String), true)],
            }],
            protocol: None,
            ..Default::default()
        };
        let out = RustEmitter::new().emit(&schema);
        assert!(out.contains("#[derive(Debug, Clone, Serialize, Deserialize)]"));
        assert!(out.contains("pub struct User {"));
        assert!(out.contains("    pub name: String,"));
    }

    #[test]
    fn keyword_field_name_is_raw_identifier() {
        let schema = ir::Schema {
            types: vec![ir::TypeDef::Struct {
                name: "Node".to_string(),
                description: None,
                fields: vec![field("type", ir::Ty::Primitive(ir::Prim::String), true)],
            }],
            protocol: None,
            ..Default::default()
        };
        let out = RustEmitter::new().emit(&schema);
        // `type` is a Rust keyword — it must be escaped as a raw identifier
        // so the generated source compiles.
        assert!(out.contains("pub r#type: String,"));
    }

    #[test]
    fn optional_field_becomes_option_with_skip() {
        let schema = ir::Schema {
            types: vec![ir::TypeDef::Struct {
                name: "User".to_string(),
                description: None,
                fields: vec![field("nick", ir::Ty::Primitive(ir::Prim::String), false)],
            }],
            protocol: None,
            ..Default::default()
        };
        let out = RustEmitter::new().emit(&schema);
        assert!(out.contains("#[serde(skip_serializing_if = \"Option::is_none\")]"));
        assert!(out.contains("pub nick: Option<String>,"));
    }

    #[test]
    fn non_snake_field_gets_serde_rename() {
        let schema = ir::Schema {
            types: vec![ir::TypeDef::Struct {
                name: "User".to_string(),
                description: None,
                fields: vec![field(
                    "displayName",
                    ir::Ty::Primitive(ir::Prim::String),
                    true,
                )],
            }],
            protocol: None,
            ..Default::default()
        };
        let out = RustEmitter::new().emit(&schema);
        assert!(out.contains("#[serde(rename = \"displayName\")]"));
        assert!(out.contains("pub displayName: String,"));
    }

    #[test]
    fn fieldless_struct_is_unit() {
        let schema = ir::Schema {
            types: vec![ir::TypeDef::Struct {
                name: "Empty".to_string(),
                description: None,
                fields: vec![],
            }],
            protocol: None,
            ..Default::default()
        };
        let out = RustEmitter::new().emit(&schema);
        assert!(out.contains("pub struct Empty;"));
    }

    #[test]
    fn emits_enum_with_rename() {
        let schema = ir::Schema {
            types: vec![ir::TypeDef::Enum {
                name: "Role".to_string(),
                description: None,
                variants: vec!["admin".to_string(), "guest_user".to_string()],
            }],
            protocol: None,
            ..Default::default()
        };
        let out = RustEmitter::new().emit(&schema);
        assert!(out.contains("#[serde(rename_all = \"snake_case\")]"));
        assert!(out.contains("pub enum Role {"));
        assert!(out.contains("#[serde(rename = \"admin\")]"));
        assert!(out.contains("    Admin,"));
        assert!(out.contains("#[serde(rename = \"guest_user\")]"));
        assert!(out.contains("    GuestUser,"));
    }

    #[test]
    fn maps_primitive_and_compound_types() {
        let schema = ir::Schema {
            types: vec![ir::TypeDef::Struct {
                name: "T".to_string(),
                description: None,
                fields: vec![
                    field("n", ir::Ty::Primitive(ir::Prim::Int), true),
                    field("f", ir::Ty::Primitive(ir::Prim::Float), true),
                    field("b", ir::Ty::Primitive(ir::Prim::Bool), true),
                    field("at", ir::Ty::Primitive(ir::Prim::Datetime), true),
                    field("blob", ir::Ty::Primitive(ir::Prim::Json), true),
                    field(
                        "tags",
                        ir::Ty::Array(Box::new(ir::Ty::Primitive(ir::Prim::String))),
                        true,
                    ),
                    field("owner", ir::Ty::Named("User".to_string()), true),
                ],
            }],
            protocol: None,
            ..Default::default()
        };
        let out = RustEmitter::new().emit(&schema);
        assert!(out.contains("pub n: i64,"));
        assert!(out.contains("pub f: f64,"));
        assert!(out.contains("pub b: bool,"));
        assert!(out.contains("pub at: DateTime<Utc>,"));
        assert!(out.contains("pub blob: serde_json::Value,"));
        assert!(out.contains("pub tags: Vec<String>,"));
        assert!(out.contains("pub owner: User,"));
    }

    #[test]
    fn emits_channel_request_returns_and_event_structs() {
        let schema = ir::Schema {
            types: vec![],
            records: vec![],
            relations: vec![],
            protocol: Some(ir::Protocol {
                name: "ping-pong".to_string(),
                version: "2.0.0".to_string(),
                namespace: None,
                description: None,
                channels: vec![ir::Channel {
                    name: "ping-pong".to_string(),
                    from: ir::ChannelFrom::Client,
                    lifetime: ir::ChannelLifetime::Persistent,
                    backend: ir::ChannelBackend::Stream,
                    channel_id: None,
                    envelope: None,
                    requests: vec![ir::Request {
                        name: "Ping".to_string(),
                        fields: vec![field("seq", ir::Ty::Primitive(ir::Prim::Int), true)],
                        returns: Some(ir::Message {
                            name: "Pong".to_string(),
                            fields: vec![field("seq", ir::Ty::Primitive(ir::Prim::Int), true)],
                        }),
                    }],
                    events: vec![ir::Event {
                        name: "Tick".to_string(),
                        fields: vec![],
                    }],
                }],
            }),
        };
        let out = RustEmitter::new().emit(&schema);
        assert!(out.contains("pub struct Ping {"));
        assert!(out.contains("pub struct Pong {"));
        assert!(out.contains("pub struct Tick;"));
    }

    // -------------------------------------------------------------------------
    // protocol dialect — envelope enum + identifier sanitize
    // -------------------------------------------------------------------------

    /// The sidebar-IPC spike channel: `:`-bearing request names, a fieldless
    /// request, and an `envelope` tag.
    fn sidebar_channel(envelope: Option<&str>) -> ir::Channel {
        ir::Channel {
            name: "ipc".to_string(),
            from: ir::ChannelFrom::Client,
            lifetime: ir::ChannelLifetime::Transient,
            backend: ir::ChannelBackend::Stream,
            channel_id: None,
            envelope: envelope.map(str::to_string),
            requests: vec![
                ir::Request {
                    name: "process:toggle".to_string(),
                    fields: vec![
                        field("path", ir::Ty::Primitive(ir::Prim::String), true),
                        field("expanded", ir::Ty::Primitive(ir::Prim::Bool), true),
                    ],
                    returns: None,
                },
                ir::Request {
                    name: "process:add".to_string(),
                    fields: vec![],
                    returns: None,
                },
            ],
            events: vec![],
        }
    }

    fn protocol_schema(channel: ir::Channel) -> ir::Schema {
        ir::Schema {
            protocol: Some(ir::Protocol {
                name: "sidebar".to_string(),
                version: "1.0.0".to_string(),
                namespace: None,
                description: None,
                channels: vec![channel],
            }),
            ..Default::default()
        }
    }

    #[test]
    fn channel_request_names_are_sanitized_to_valid_identifiers() {
        // A `:`-bearing request name must not leak into `pub struct foo:bar`.
        let out = RustEmitter::new().emit(&protocol_schema(sidebar_channel(None)));
        assert!(out.contains("pub struct ProcessToggle {"));
        assert!(out.contains("pub struct ProcessAdd;"));
        assert!(
            !out.contains("process:toggle"),
            "raw `:` name must not leak"
        );
    }

    #[test]
    fn channel_without_envelope_emits_no_enum() {
        // Backward compatibility: an `envelope`-less channel emits only structs.
        let out = RustEmitter::new().emit(&protocol_schema(sidebar_channel(None)));
        assert!(!out.contains("pub enum"), "no envelope ⇒ no enum");
    }

    #[test]
    fn envelope_channel_emits_internally_tagged_enum() {
        let out = RustEmitter::new().emit(&protocol_schema(sidebar_channel(Some("t"))));
        assert!(out.contains("#[serde(tag = \"t\")]"), "internally tagged");
        assert!(
            out.contains("pub enum IpcEnvelope {"),
            "enum named <Channel>Envelope"
        );
        // a request with fields → newtype variant wrapping its struct.
        assert!(out.contains("    #[serde(rename = \"process:toggle\")]"));
        assert!(out.contains("    ProcessToggle(ProcessToggle),"));
        // a fieldless request → unit variant (serde rejects newtype-of-unit).
        assert!(out.contains("    #[serde(rename = \"process:add\")]"));
        assert!(out.contains("    ProcessAdd,\n"));
        assert!(
            !out.contains("ProcessAdd(ProcessAdd)"),
            "fieldless request must not become a newtype variant"
        );
    }

    #[test]
    fn envelope_variant_without_colon_name_needs_no_rename() {
        // A request whose name is already PascalCase carries no `#[serde(rename)]`.
        let mut channel = sidebar_channel(Some("t"));
        channel.requests = vec![ir::Request {
            name: "Ping".to_string(),
            fields: vec![field("seq", ir::Ty::Primitive(ir::Prim::Int), true)],
            returns: None,
        }];
        let out = RustEmitter::new().emit(&protocol_schema(channel));
        assert!(out.contains("    Ping(Ping),"));
        // `Ping` == to_pascal_case("Ping") → no rename attribute precedes it.
        let variant_line = out.find("    Ping(Ping),").unwrap();
        let preceding = &out[..variant_line];
        assert!(
            !preceding.trim_end().ends_with("rename = \"Ping\")]"),
            "an already-PascalCase name needs no rename"
        );
    }

    // -------------------------------------------------------------------------
    // Tier 1 — record / relation / link / union
    // -------------------------------------------------------------------------

    #[test]
    fn record_becomes_struct_with_id_field() {
        let schema = ir::Schema {
            records: vec![ir::Record {
                name: "Atlas".to_string(),
                description: None,
                id_strategy: ir::IdStrategy::Uuidv7,
                fields: vec![field("name", ir::Ty::Primitive(ir::Prim::String), true)],
            }],
            ..Default::default()
        };
        let out = RustEmitter::new().emit(&schema);
        assert!(out.contains("pub struct Atlas {"));
        assert!(out.contains("pub id: String,"), "record gets an id field");
        assert!(out.contains("pub name: String,"));
    }

    #[test]
    fn relation_becomes_edge_struct_with_in_out() {
        let schema = ir::Schema {
            relations: vec![ir::Relation {
                name: "derivedFrom".to_string(),
                description: None,
                from: "Memory".to_string(),
                to: "Memory".to_string(),
                unique: true,
                fields: vec![field("reason", ir::Ty::Primitive(ir::Prim::String), false)],
            }],
            ..Default::default()
        };
        let out = RustEmitter::new().emit(&schema);
        assert!(out.contains("pub struct DerivedFrom {"));
        assert!(out.contains("pub id: String,"));
        // `in` is a Rust keyword → escaped as a raw identifier.
        assert!(out.contains("pub r#in: String,"));
        assert!(out.contains("pub out: String,"));
        assert!(out.contains("pub reason: Option<String>,"));
    }

    #[test]
    fn link_field_becomes_string() {
        let schema = ir::Schema {
            records: vec![ir::Record {
                name: "Atlas".to_string(),
                description: None,
                id_strategy: ir::IdStrategy::Uuidv7,
                fields: vec![field("parent", ir::Ty::Link("Atlas".to_string()), false)],
            }],
            ..Default::default()
        };
        let out = RustEmitter::new().emit(&schema);
        assert!(out.contains("pub parent: Option<String>,"));
    }

    #[test]
    fn literal_union_degrades_to_string() {
        let schema = ir::Schema {
            records: vec![ir::Record {
                name: "Doc".to_string(),
                description: None,
                id_strategy: ir::IdStrategy::Uuidv7,
                fields: vec![field(
                    "visibility",
                    ir::Ty::Union(vec![
                        ir::Ty::Literal("public".to_string()),
                        ir::Ty::Literal("private".to_string()),
                    ]),
                    true,
                )],
            }],
            ..Default::default()
        };
        let out = RustEmitter::new().emit(&schema);
        assert!(out.contains("pub visibility: String,"));
    }

    #[test]
    fn mixed_union_degrades_to_json_value() {
        let schema = ir::Schema {
            types: vec![ir::TypeDef::Struct {
                name: "T".to_string(),
                description: None,
                fields: vec![field(
                    "v",
                    ir::Ty::Union(vec![
                        ir::Ty::Primitive(ir::Prim::String),
                        ir::Ty::Primitive(ir::Prim::Int),
                    ]),
                    true,
                )],
            }],
            ..Default::default()
        };
        let out = RustEmitter::new().emit(&schema);
        assert!(out.contains("pub v: serde_json::Value,"));
    }

    // -------------------------------------------------------------------------
    // Tier 2 — description → `///` doc comments (constraints are not emitted)
    // -------------------------------------------------------------------------

    #[test]
    fn struct_and_field_descriptions_become_doc_comments() {
        let mut content = field("content", ir::Ty::Primitive(ir::Prim::String), true);
        content.description = Some("Memory content text".to_string());
        let schema = ir::Schema {
            types: vec![ir::TypeDef::Struct {
                name: "Memory".to_string(),
                description: Some("User memory".to_string()),
                fields: vec![content],
            }],
            ..Default::default()
        };
        let out = RustEmitter::new().emit(&schema);
        assert!(out.contains("/// User memory\n"), "struct doc comment");
        assert!(
            out.contains("    /// Memory content text\n"),
            "field doc comment"
        );
    }

    #[test]
    fn enum_description_becomes_doc_comment() {
        let schema = ir::Schema {
            types: vec![ir::TypeDef::Enum {
                name: "Role".to_string(),
                description: Some("An access role".to_string()),
                variants: vec!["admin".to_string()],
            }],
            ..Default::default()
        };
        let out = RustEmitter::new().emit(&schema);
        assert!(out.contains("/// An access role\n"));
    }

    #[test]
    fn constraints_do_not_appear_in_rust_output() {
        // Rust's type system cannot express min/max/pattern — they must be
        // dropped, not emitted as attributes or comments.
        let mut f = field("confidence", ir::Ty::Primitive(ir::Prim::Float), true);
        f.constraints = ir::Constraints {
            min: Some(0),
            max: Some(1),
            pattern: Some("x".to_string()),
            ..Default::default()
        };
        let schema = ir::Schema {
            types: vec![ir::TypeDef::Struct {
                name: "T".to_string(),
                description: None,
                fields: vec![f],
            }],
            ..Default::default()
        };
        let out = RustEmitter::new().emit(&schema);
        assert!(out.contains("pub confidence: f64,"));
        assert!(!out.contains("minimum"), "no constraint metadata leaks");
    }
}