facet_generate 0.17.0

Generate Swift, Kotlin and TypeScript from types annotated with `#[derive(Facet)]`
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
//! AST-to-Swift source rendering.
//!
//! This module implements [`Emitter<Swift>`](super::super::Emitter) for each
//! node type in the format AST, turning abstract type descriptions into
//! idiomatic Swift code.
//!
//! # Emitter implementations
//!
//! | AST node | Swift output |
//! |---|---|
//! | [`Module`] | `import` statements, feature helpers |
//! | [`Container`] | `public struct` or `indirect public enum` |
//! | [`Named<Format>`](Named) | `public var` property / `case` declaration |
//! | [`Format`] | Inline type expression (`Int32`, `[String]`, `Set<T>`, …) |
//! | [`Doc`] | `///` doc comments |
//! | `(Named<VariantFormat>, Usage)` | An enum case declaration |
//!
//! # Swift type mapping
//!
//! The [`Format`] emitter maps Rust/reflection types to Swift equivalents —
//! for example `I32` → `Int32`, `Seq(T)` → `[T]`, `Option(T)` → `T?`,
//! `Map(K,V)` → `[K: V]`, tuples of any size → native `(A, B)` (always).
//!
//! # Encoding-dependent output
//!
//! The [`Swift`] language tag carries a list of [`EmitterPlugin`]s. All
//! encoding-specific behaviour is delegated to those plugins — the emitter
//! itself contains no encoding checks. For example:
//!
//! - `BincodePlugin` supplies `serialize` / `deserialize` methods and
//!   `bincodeSerialize` / `bincodeDeserialize` wrappers.
//! - `JsonPlugin` supplies the same `serialize` / `deserialize` methods and
//!   `jsonSerialize` / `jsonDeserialize` wrappers.
//! - With no plugins, only plain type declarations are emitted.
//!
//! # Feature helpers
//!
//! The encoding-specific feature helpers (`serializeArray`, `serializeOption`,
//! etc.) are inlined in `BincodePlugin` and `JsonPlugin`
//! (`generation/bincode/swift.rs` and `generation/json/swift.rs`).
//! They are emitted via the [`EmitterPlugin::module_helpers`] hook when the
//! corresponding [`Feature`] flag is set by
//! [`CodeGeneratorConfig::update_from`].
//!
//! # Hashable / Equatable
//!
//! Types whose fields are all [`Hashable`](https://developer.apple.com/documentation/swift/hashable)
//! will declare `: Hashable` conformance so they can serve as `Set` elements
//! or `Dictionary` keys. A generation-time error is raised if a non-`Hashable`
//! type (native tuple or `[K:V]` dictionary) is used directly as a `Set`
//! element or `Map` key.

#![allow(clippy::too_many_lines)]
use std::{
    collections::{BTreeMap, BTreeSet},
    io::{self, Result, Write},
    sync::Arc,
};

use heck::ToLowerCamelCase as _;
use indoc::formatdoc;

use heck::ToUpperCamelCase as _;

use crate::generation::CodeGeneratorConfig;
use crate::{
    Registry,
    generation::{
        Container, Emitter,
        indent::{IndentWrite, Newlines},
        module::Module,
        plugin::{EmitContext, EmitterPlugin},
        swift::generator::{compute_equatable_types, compute_hashable_types},
    },
    reflection::format::{ContainerFormat, Doc, Format, Named, Namespace, VariantFormat},
};

/// Language tag for Swift code generation.
///
/// Carries the active `Encoding` and the sets of type names (within the
/// current module) that are known to be able to synthesize `Hashable` and
/// `Equatable` conformance respectively. Both sets are computed by a
/// preprocessing pass — see
/// [`SwiftCodeGenerator`](crate::generation::swift::generator::SwiftCodeGenerator).
///
/// The plugin list is built in [`new`](Self::new) from the config encoding.
/// Eventually, plugins will be supplied externally and `encoding` will be
/// removed.
#[derive(Debug, Clone)]
pub struct Swift {
    /// The code-generator configuration for the current module.
    pub(crate) config: CodeGeneratorConfig,
    /// Type names (root-namespace) that can synthesize `Hashable` conformance.
    pub(crate) hashable_types: BTreeSet<String>,
    /// Type names (root-namespace) that can synthesize or manually implement
    /// `Equatable` conformance.
    pub(crate) equatable_types: BTreeSet<String>,
    pub(crate) plugins: Vec<Arc<dyn EmitterPlugin<Self>>>,
}

impl Swift {
    /// Create a Swift language tag with computed type sets and an empty plugin
    /// list. Plugins are added by the code generator (which holds the encoding)
    /// or explicitly via [`with_plugin`](Self::with_plugin).
    ///
    /// The `hashable_types` and `equatable_types` sets are computed from the
    /// registry via fixed-point analysis and are unrelated to plugin selection.
    #[must_use]
    pub fn new(config: &CodeGeneratorConfig, registry: &Registry) -> Self {
        Self {
            config: config.clone(),
            hashable_types: compute_hashable_types(registry),
            equatable_types: compute_equatable_types(registry),
            plugins: vec![],
        }
    }

    /// Access the code-generator configuration.
    #[must_use]
    pub const fn config(&self) -> &CodeGeneratorConfig {
        &self.config
    }

    /// Access the plugin list.
    #[must_use]
    pub fn plugins(&self) -> &[Arc<dyn EmitterPlugin<Self>>] {
        &self.plugins
    }

    /// Add a plugin to this language tag (builder-style).
    #[must_use]
    pub fn with_plugin(mut self, plugin: Arc<dyn EmitterPlugin<Self>>) -> Self {
        self.plugins.push(plugin);
        self
    }
}

// ---------------------------------------------------------------------------
// Hashability helpers
// ---------------------------------------------------------------------------

/// Returns `true` if the Swift type produced by `format` can conform to
/// `Hashable`.
pub fn is_hashable(format: &Format, lang: &Swift) -> bool {
    match format {
        Format::Variable(_)
        | Format::Unit  // Void does not conform to Hashable in Swift
        | Format::Map { .. } => false, // [K: V] is never Hashable

        Format::TypeName(qtn) => match &qtn.namespace {
            Namespace::Root => lang.hashable_types.contains(&qtn.name),
            Namespace::Named(_) => true, // external — assume hashable
        },

        Format::Bool
        | Format::I8
        | Format::I16
        | Format::I32
        | Format::I64
        | Format::I128
        | Format::U8
        | Format::U16
        | Format::U32
        | Format::U64
        | Format::U128
        | Format::F32
        | Format::F64
        | Format::Char
        | Format::Str
        | Format::Bytes => true,

        Format::Option(inner)
        | Format::Set(inner)
        | Format::Seq(inner)
        | Format::TupleArray { content: inner, .. } => is_hashable(inner, lang),

        // A 1-element tuple is transparent; multi-element native tuples are not Hashable.
        Format::Tuple(formats) => {
            formats.len() == 1 && is_hashable(&formats[0], lang)
        }
    }
}

fn variant_is_hashable(format: &VariantFormat, lang: &Swift) -> bool {
    match format {
        VariantFormat::Variable(_) => false,
        VariantFormat::Unit => true,
        VariantFormat::NewType(fmt) => is_hashable(fmt, lang),
        VariantFormat::Tuple(fmts) => fmts.iter().all(|f| is_hashable(f, lang)),
        VariantFormat::Struct(nameds) => nameds.iter().all(|n| is_hashable(&n.value, lang)),
    }
}

// ---------------------------------------------------------------------------
// Usage — field / case rendering context
// ---------------------------------------------------------------------------

enum Usage {
    Field,
    /// Like `Field` but prefixed with `@Indirect` for recursive struct fields
    /// that would otherwise create an infinite-size value type.
    IndirectField,
    Parameter,
    Assignment,
}

// ---------------------------------------------------------------------------
// Recursion / indirection helpers
// ---------------------------------------------------------------------------

/// Returns `true` if a struct field of this format would create an
/// infinite-size value-type cycle back to the containing struct named
/// `struct_name`.
fn needs_indirect(format: &Format, struct_name: &str) -> bool {
    match format {
        Format::TypeName(qtn) => qtn.name == struct_name,
        Format::Option(inner) => needs_indirect(inner, struct_name),
        Format::Tuple(formats) => formats.iter().any(|f| needs_indirect(f, struct_name)),
        _ => false,
    }
}

// ---------------------------------------------------------------------------
// Equatable helpers
// ---------------------------------------------------------------------------

fn is_equatable_auto(format: &Format, lang: &Swift) -> bool {
    match format {
        Format::TypeName(qtn) => match &qtn.namespace {
            Namespace::Root => lang.equatable_types.contains(&qtn.name),
            Namespace::Named(_) => true,
        },
        Format::Variable(_) | Format::Unit => false,
        Format::Bool
        | Format::I8
        | Format::I16
        | Format::I32
        | Format::I64
        | Format::I128
        | Format::U8
        | Format::U16
        | Format::U32
        | Format::U64
        | Format::U128
        | Format::F32
        | Format::F64
        | Format::Char
        | Format::Str
        | Format::Bytes => true,
        Format::Option(inner) | Format::Set(inner) => is_equatable_auto(inner, lang),
        Format::Seq(inner) | Format::TupleArray { content: inner, .. } => {
            is_equatable_auto(inner, lang)
        }
        Format::Map { key, value } => {
            is_equatable_auto(key, lang) && is_equatable_auto(value, lang)
        }
        Format::Tuple(formats) => formats.len() == 1 && is_equatable_auto(&formats[0], lang),
    }
}

fn can_use_eq_operator(format: &Format, lang: &Swift) -> bool {
    match format {
        Format::Tuple(formats) if formats.len() > 1 => {
            formats.iter().all(|f| is_equatable_auto(f, lang))
        }
        _ => is_equatable_auto(format, lang),
    }
}

fn variant_is_equatable_auto(format: &VariantFormat, lang: &Swift) -> bool {
    match format {
        VariantFormat::Variable(_) => false,
        VariantFormat::Unit => true,
        VariantFormat::NewType(fmt) => is_equatable_auto(fmt, lang),
        VariantFormat::Tuple(formats) => formats.iter().all(|f| is_equatable_auto(f, lang)),
        VariantFormat::Struct(nameds) => nameds.iter().all(|n| is_equatable_auto(&n.value, lang)),
    }
}

fn variant_can_use_eq_operator(format: &VariantFormat, lang: &Swift) -> bool {
    match format {
        VariantFormat::Variable(_) => false,
        VariantFormat::Unit => true,
        VariantFormat::NewType(fmt) => can_use_eq_operator(fmt, lang),
        VariantFormat::Tuple(formats) => formats.iter().all(|f| can_use_eq_operator(f, lang)),
        VariantFormat::Struct(nameds) => nameds.iter().all(|n| can_use_eq_operator(&n.value, lang)),
    }
}

// ---------------------------------------------------------------------------
// Module emitter
// ---------------------------------------------------------------------------

impl Emitter<Swift> for Module {
    fn write<W: IndentWrite>(&self, w: &mut W, lang: &Swift) -> Result<()> {
        let mut imports = vec![];

        // Encoding-independent base imports (external namespaces).
        for ns in self.config().external_definitions.keys() {
            imports.push(ns.to_upper_camel_case());
        }

        // Plugin imports (e.g. `import Serde`).
        for plugin in lang.plugins() {
            imports.extend(plugin.imports(self.config()));
        }

        imports.sort();
        imports.dedup();
        for import in &imports {
            writeln!(w, "import {import}")?;
        }

        // Plugin module helpers (feature snippets).
        for plugin in lang.plugins() {
            plugin.module_helpers(w, self.config())?;
        }

        Ok(())
    }
}

// ---------------------------------------------------------------------------
// Container emitter
// ---------------------------------------------------------------------------

impl Emitter<Swift> for Container<'_> {
    fn write<W: IndentWrite>(&self, w: &mut W, lang: &Swift) -> Result<()> {
        let Container { format, .. } = self;
        match format {
            ContainerFormat::UnitStruct(doc) => struct_(w, self, &[], doc, lang),
            ContainerFormat::NewTypeStruct(format, doc) => struct_(
                w,
                self,
                &[&Named::new(format, "value".to_string())],
                doc,
                lang,
            ),
            ContainerFormat::TupleStruct(formats, doc) => {
                let formats = named(formats, "field");
                struct_(w, self, &formats.iter().collect::<Vec<_>>(), doc, lang)
            }
            ContainerFormat::Struct(nameds, doc) => {
                struct_(w, self, &nameds.iter().collect::<Vec<_>>(), doc, lang)
            }
            ContainerFormat::Enum(variants, doc) => enum_(w, self, variants, doc, lang),
        }
    }
}

// ---------------------------------------------------------------------------
// Format emitter
// ---------------------------------------------------------------------------

impl Emitter<Swift> for Format {
    fn write<W: IndentWrite>(&self, w: &mut W, lang: &Swift) -> Result<()> {
        match &self {
            Self::Variable(_variable) => unreachable!("placeholders should not get this far"),
            Self::TypeName(qualified_type_name) => {
                write!(
                    w,
                    "{ty}",
                    ty = qualified_type_name
                        .format(|ns| heck::AsUpperCamelCase(ns).to_string(), ".")
                )
            }
            Self::Unit => write!(w, "Void"),
            Self::Bool => write!(w, "Bool"),
            Self::I8 => write!(w, "Int8"),
            Self::I16 => write!(w, "Int16"),
            Self::I32 => write!(w, "Int32"),
            Self::I64 => write!(w, "Int64"),
            Self::I128 => write!(w, "Int128"),
            Self::U8 => write!(w, "UInt8"),
            Self::U16 => write!(w, "UInt16"),
            Self::U32 => write!(w, "UInt32"),
            Self::U64 => write!(w, "UInt64"),
            Self::U128 => write!(w, "UInt128"),
            Self::F32 => write!(w, "Float"),
            Self::F64 => write!(w, "Double"),
            Self::Char => write!(w, "Character"),
            Self::Str => write!(w, "String"),
            Self::Bytes => write!(w, "[UInt8]"),

            Self::Option(format) => {
                format.write(w, lang)?;
                write!(w, "?")
            }
            Self::Seq(format)
            | Self::TupleArray {
                content: format,
                size: _,
            } => {
                write!(w, "[")?;
                format.write(w, lang)?;
                write!(w, "]")
            }
            Self::Set(format) => {
                if !is_hashable(format, lang) {
                    return Err(io::Error::new(
                        io::ErrorKind::InvalidInput,
                        formatdoc!(
                            "Set element type is not Hashable in Swift; \
                             native tuples and dictionaries do not conform to Hashable"
                        ),
                    ));
                }
                write!(w, "Set<")?;
                format.write(w, lang)?;
                write!(w, ">")
            }
            Self::Map { key, value } => {
                if !is_hashable(key, lang) {
                    return Err(io::Error::new(
                        io::ErrorKind::InvalidInput,
                        formatdoc!(
                            "Map key type is not Hashable in Swift; \
                             native tuples and dictionaries do not conform to Hashable"
                        ),
                    ));
                }
                write!(w, "[")?;
                key.write(w, lang)?;
                write!(w, ": ")?;
                value.write(w, lang)?;
                write!(w, "]")
            }
            Self::Tuple(formats) => {
                let len = formats.len();
                if len == 1 {
                    formats[0].write(w, lang)
                } else {
                    write!(w, "(")?;
                    for (i, format) in formats.iter().enumerate() {
                        if i > 0 {
                            write!(w, ", ")?;
                        }
                        format.write(w, lang)?;
                    }
                    write!(w, ")")
                }
            }
        }
    }
}

// ---------------------------------------------------------------------------
// Named<Format> emitter — field / parameter / argument / assignment
// ---------------------------------------------------------------------------

impl Emitter<Swift> for (&Named<Format>, Usage) {
    fn write<W: IndentWrite>(&self, w: &mut W, lang: &Swift) -> Result<()> {
        let (Named { name, doc, value }, usage) = self;
        let name = &name.to_lower_camel_case();

        match usage {
            Usage::Field => {
                doc.write(w, lang)?;
                write!(w, "public var {name}: ")?;
                value.write(w, lang)?;
                writeln!(w)
            }
            Usage::IndirectField => {
                doc.write(w, lang)?;
                write!(w, "@Indirect public var {name}: ")?;
                value.write(w, lang)?;
                writeln!(w)
            }
            Usage::Parameter => {
                write!(w, "{name}: ")?;
                value.write(w, lang)
            }
            Usage::Assignment => writeln!(w, "self.{name} = {name}"),
        }
    }
}

// ---------------------------------------------------------------------------
// Named<VariantFormat> emitter — case declarations only
// ---------------------------------------------------------------------------

impl Emitter<Swift> for (&Named<VariantFormat>, Usage) {
    fn write<W: IndentWrite>(&self, w: &mut W, lang: &Swift) -> Result<()> {
        let (
            Named {
                name,
                doc,
                value: format,
            },
            usage,
        ) = self;
        let name = name.to_lower_camel_case();

        doc.write(w, lang)?;

        match usage {
            Usage::IndirectField => {
                unreachable!("@Indirect is only used for struct fields, not enum variants")
            }
            Usage::Field => match format {
                VariantFormat::Variable(_variable) => {
                    unreachable!("placeholders should not get this far")
                }
                VariantFormat::Unit => writeln!(w, "case {name}"),
                VariantFormat::NewType(format) => {
                    write!(w, "case {name}(")?;
                    format.write(w, lang)?;
                    writeln!(w, ")")
                }
                VariantFormat::Tuple(formats) => {
                    write!(w, "case {name}(")?;
                    for (i, format) in formats.iter().enumerate() {
                        if i > 0 {
                            write!(w, ", ")?;
                        }
                        format.write(w, lang)?;
                    }
                    writeln!(w, ")")
                }
                VariantFormat::Struct(nameds) => {
                    write!(w, "case {name}(")?;
                    for (i, format) in nameds.iter().enumerate() {
                        if i > 0 {
                            write!(w, ", ")?;
                        }
                        (format, Usage::Parameter).write(w, lang)?;
                    }
                    writeln!(w, ")")
                }
            },
            Usage::Parameter | Usage::Assignment => Ok(()),
        }
    }
}

// ---------------------------------------------------------------------------
// Doc emitter
// ---------------------------------------------------------------------------

impl Emitter<Swift> for Doc {
    fn write<W: IndentWrite>(&self, w: &mut W, _lang: &Swift) -> Result<()> {
        for comment in self.comments() {
            writeln!(w, "/// {comment}")?;
        }
        Ok(())
    }
}

// ---------------------------------------------------------------------------
// struct_ — emits a public struct
// ---------------------------------------------------------------------------

/// Emit a `public struct` with optional `Hashable` / `Equatable` conformance,
/// a memberwise initializer, and (via plugins) `serialize` / `deserialize`
/// methods.
fn struct_<W: IndentWrite>(
    w: &mut W,
    container: &Container<'_>,
    fields: &[&Named<Format>],
    doc: &Doc,
    lang: &Swift,
) -> Result<()> {
    let name = &container.name.name;

    doc.write(w, lang)?;

    let has_plugins = !lang.plugins().is_empty();
    let all_hashable = fields.iter().all(|f| is_hashable(&f.value, lang));
    let all_equatable_auto = fields.iter().all(|f| is_equatable_auto(&f.value, lang));
    let all_can_eq = fields.iter().all(|f| can_use_eq_operator(&f.value, lang));

    if !has_plugins {
        write!(w, "public struct {name} ")?;
    } else if all_hashable {
        write!(w, "public struct {name}: Hashable ")?;
    } else if all_equatable_auto || all_can_eq {
        write!(w, "public struct {name}: Equatable ")?;
    } else {
        write!(w, "public struct {name} ")?;
    }
    let mut w = w.block(Newlines::BOTH)?;

    for field in fields {
        let usage = if has_plugins && needs_indirect(&field.value, name) {
            Usage::IndirectField
        } else {
            Usage::Field
        };
        (*field, usage).write(&mut w, lang)?;
    }

    if !fields.is_empty() {
        writeln!(w)?;
    }

    write!(w, "public init(")?;
    for (i, field) in fields.iter().enumerate() {
        if i > 0 {
            write!(w, ", ")?;
        }
        (*field, Usage::Parameter).write(&mut w, lang)?;
    }
    write!(w, ") ")?;
    {
        let mut w = w.block(Newlines::BOTH)?;
        for field in fields {
            (*field, Usage::Assignment).write(&mut w, lang)?;
        }
    }

    // Plugin type bodies (serialize / deserialize methods).
    let ctx = EmitContext::top_level(container, &lang.config);
    for plugin in lang.plugins() {
        plugin.type_body(&mut w as &mut dyn IndentWrite, &ctx)?;
    }

    // Emit manual Equatable implementation when auto-synthesis is blocked
    // (because one or more fields are native tuples) but == can still be
    // written field-by-field using Swift's built-in tuple == operator.
    if has_plugins && !all_hashable && !all_equatable_auto && all_can_eq {
        write_struct_eq(&mut w, name, fields)?;
    }

    Ok(())
}

fn write_struct_eq<W: IndentWrite>(w: &mut W, name: &str, fields: &[&Named<Format>]) -> Result<()> {
    writeln!(w)?;
    write!(
        w,
        "public static func == (lhs: {name}, rhs: {name}) -> Bool "
    )?;
    let mut w = w.block(Newlines::BOTH)?;
    if fields.is_empty() {
        writeln!(w, "return true")?;
    } else {
        write!(w, "return ")?;
        for (i, field) in fields.iter().enumerate() {
            let fname = field.name.to_lower_camel_case();
            if i > 0 {
                writeln!(w)?;
                write!(w, "    && ")?;
            }
            write!(w, "lhs.{fname} == rhs.{fname}")?;
        }
        writeln!(w)?;
    }
    Ok(())
}

// ---------------------------------------------------------------------------
// enum_ — emits an indirect public enum
// ---------------------------------------------------------------------------

/// Emit an `indirect public enum` with optional `Hashable` / `Equatable`
/// conformance, case declarations, and (via plugins) `serialize` /
/// `deserialize` methods.
fn enum_<W: IndentWrite>(
    w: &mut W,
    container: &Container<'_>,
    variants: &BTreeMap<u32, Named<VariantFormat>>,
    doc: &Doc,
    lang: &Swift,
) -> Result<()> {
    let name = &container.name.name;

    doc.write(w, lang)?;

    let has_plugins = !lang.plugins().is_empty();
    let all_hashable = variants
        .values()
        .all(|v| variant_is_hashable(&v.value, lang));
    let all_equatable_auto = variants
        .values()
        .all(|v| variant_is_equatable_auto(&v.value, lang));
    let all_can_eq = variants
        .values()
        .all(|v| variant_can_use_eq_operator(&v.value, lang));

    if !has_plugins {
        write!(w, "indirect public enum {name} ")?;
    } else if all_hashable {
        write!(w, "indirect public enum {name}: Hashable ")?;
    } else if all_equatable_auto || all_can_eq {
        write!(w, "indirect public enum {name}: Equatable ")?;
    } else {
        write!(w, "indirect public enum {name} ")?;
    }
    let mut w = w.block(Newlines::BOTH)?;

    for variant in variants.values() {
        (variant, Usage::Field).write(&mut w, lang)?;
    }

    // Plugin type bodies (serialize / deserialize methods).
    let ctx = EmitContext::top_level(container, &lang.config);
    for plugin in lang.plugins() {
        plugin.type_body(&mut w as &mut dyn IndentWrite, &ctx)?;
    }

    // Emit manual Equatable implementation when auto-synthesis is blocked.
    if has_plugins && !all_hashable && !all_equatable_auto && all_can_eq {
        write_enum_eq(&mut w, name, &variants.values().collect::<Vec<_>>())?;
    }

    Ok(())
}

fn write_enum_eq<W: IndentWrite>(
    w: &mut W,
    name: &str,
    variants: &[&Named<VariantFormat>],
) -> Result<()> {
    writeln!(w)?;
    write!(
        w,
        "public static func == (lhs: {name}, rhs: {name}) -> Bool "
    )?;
    let mut w = w.block(Newlines::BOTH)?;
    write!(w, "switch (lhs, rhs) ")?;
    {
        let mut w = w.block(Newlines::BOTH)?;
        w.unindent();
        for variant in variants {
            let variant_name = variant.name.to_lower_camel_case();
            match &variant.value {
                VariantFormat::Unit => {
                    writeln!(w, "case (.{variant_name}, .{variant_name}): return true")?;
                }
                VariantFormat::NewType(_) => {
                    writeln!(
                        w,
                        "case (.{variant_name}(let l), .{variant_name}(let r)): return l == r"
                    )?;
                }
                VariantFormat::Tuple(formats) => {
                    write!(w, "case (.{variant_name}(")?;
                    for (i, _) in formats.iter().enumerate() {
                        if i > 0 {
                            write!(w, ", ")?;
                        }
                        write!(w, "let l{i}")?;
                    }
                    write!(w, "), .{variant_name}(")?;
                    for (i, _) in formats.iter().enumerate() {
                        if i > 0 {
                            write!(w, ", ")?;
                        }
                        write!(w, "let r{i}")?;
                    }
                    write!(w, ")): return ")?;
                    for (i, _) in formats.iter().enumerate() {
                        if i > 0 {
                            write!(w, " && ")?;
                        }
                        write!(w, "l{i} == r{i}")?;
                    }
                    writeln!(w)?;
                }
                VariantFormat::Struct(nameds) => {
                    write!(w, "case (.{variant_name}(")?;
                    for (i, n) in nameds.iter().enumerate() {
                        if i > 0 {
                            write!(w, ", ")?;
                        }
                        let fname = n.name.to_lower_camel_case();
                        write!(w, "{fname}: let l{i}")?;
                    }
                    write!(w, "), .{variant_name}(")?;
                    for (i, n) in nameds.iter().enumerate() {
                        if i > 0 {
                            write!(w, ", ")?;
                        }
                        let fname = n.name.to_lower_camel_case();
                        write!(w, "{fname}: let r{i}")?;
                    }
                    write!(w, ")): return ")?;
                    for (i, _) in nameds.iter().enumerate() {
                        if i > 0 {
                            write!(w, " && ")?;
                        }
                        write!(w, "l{i} == r{i}")?;
                    }
                    writeln!(w)?;
                }
                VariantFormat::Variable(_) => {
                    unreachable!("placeholders should not get this far")
                }
            }
        }
        writeln!(w, "default: return false")?;
        w.indent();
    }
    Ok(())
}

// ---------------------------------------------------------------------------
// Utility
// ---------------------------------------------------------------------------

fn named<Format: Clone>(formats: &[Format], prefix: &str) -> Vec<Named<Format>> {
    formats
        .iter()
        .enumerate()
        .map(|(i, f)| Named::new(f, format!("{prefix}{i}")))
        .collect()
}

#[cfg(test)]
mod tests;
#[cfg(test)]
mod tests_bincode;
#[cfg(test)]
mod tests_json;