facet_generate 0.17.1

Generate Swift, Kotlin, TypeScript, and C# 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
//! Top-level orchestrator for Swift code generation.
//!
//! [`SwiftCodeGenerator`] implements [`CodeGenerator`] and is the entry point for
//! producing a single Swift source file from a [`Registry`].

use std::{
    collections::BTreeSet,
    io::{Result, Write},
    sync::Arc,
};

use crate::{
    Registry,
    generation::{
        CodeGenerator, CodeGeneratorConfig, Container, Emitter, indent::IndentedWriter,
        module::Module, plugin::EmitterPlugin, swift::emitter::Swift,
    },
    reflection::format::{
        ContainerFormat, Format, FormatHolder, Namespace, QualifiedTypeName, VariantFormat,
    },
};

/// Main configuration object for Swift code generation.
///
/// Wraps a [`CodeGeneratorConfig`] and implements [`CodeGenerator`] so it can be
/// used by the installer pipeline.
pub struct SwiftCodeGenerator<'a> {
    /// Language-independent configuration.
    pub(crate) config: &'a CodeGeneratorConfig,
    /// Pre-built plugins supplied by the caller (e.g. from the installer).
    pub(crate) plugins: Vec<Arc<dyn EmitterPlugin<Swift>>>,
}

impl<'a> CodeGenerator<'a> for SwiftCodeGenerator<'a> {
    fn new(config: &'a CodeGeneratorConfig) -> Self {
        Self {
            config,
            plugins: vec![],
        }
    }

    fn write_output<W: std::io::Write>(
        &mut self,
        writer: &mut W,
        registry: &Registry,
    ) -> Result<()> {
        self.output(writer, registry)
    }
}

impl<'a> SwiftCodeGenerator<'a> {
    /// Create a Swift code generator with no encoding (plain types only).
    ///
    /// Call [`with_plugins`](Self::with_plugins) to enable serialization.
    #[must_use]
    pub fn new(config: &'a CodeGeneratorConfig) -> Self {
        Self {
            config,
            plugins: vec![],
        }
    }

    /// Set the pre-built plugin list, returning the modified generator.
    #[must_use]
    pub fn with_plugins(mut self, plugins: Vec<Arc<dyn EmitterPlugin<Swift>>>) -> Self {
        self.plugins = plugins;
        self
    }

    /// Produce a complete Swift source file for `registry`.
    ///
    /// # Errors
    ///
    /// Returns an error if writing to `out` fails.
    pub fn output(&self, out: &mut impl Write, registry: &Registry) -> Result<()> {
        let w = &mut IndentedWriter::new(out, self.config.indent);

        let mut config = self.config.clone();
        config.update_from(registry);

        let mut lang = Swift::new(&config, registry);
        for p in &self.plugins {
            lang = lang.with_plugin(p.clone());
        }

        Module::new(&config).write(w, &lang)?;

        let updated_registry = Self::update_qualified_names(&config, registry);
        for container in updated_registry.iter().map(Container::from) {
            writeln!(w)?;
            container.write(w, &lang)?;
        }

        Ok(())
    }

    /// Updates `QualifiedTypeName` instances so external types include their namespace prefix.
    /// For example, a type `Tree` in namespace `foo` becomes `Foo.Tree` in the output.
    fn update_qualified_names(config: &CodeGeneratorConfig, registry: &Registry) -> Registry {
        let mut updated_registry = registry.clone();

        for container_format in updated_registry.values_mut() {
            let _ = container_format.visit_mut(&mut |format| {
                if let Format::TypeName(qualified_name) = format
                    && let Namespace::Named(namespace) = &qualified_name.namespace
                {
                    if namespace == config.module_name() {
                        // Same-module type: strip namespace so it renders as a bare name
                        *qualified_name = QualifiedTypeName::root(qualified_name.name.clone());
                    } else if config.external_definitions.contains_key(namespace) {
                        *qualified_name = QualifiedTypeName::namespaced(
                            namespace.clone(),
                            qualified_name.name.clone(),
                        );
                    }
                }
                Ok(())
            });
        }

        updated_registry
    }
}

/// Computes the set of type names (within this module) that can synthesize
/// Swift `Hashable` conformance.
///
/// Uses depth-first search with optimistic cycle handling: if a type appears
/// on the current evaluation stack it is assumed hashable. This correctly
/// supports self-referential and mutually-recursive types because Swift
/// permits `Hashable` synthesis for recursive value types as long as all
/// non-recursive stored properties are themselves `Hashable`.
///
/// External types (not present in the registry) are assumed to be hashable.
pub fn compute_hashable_types(registry: &Registry) -> BTreeSet<String> {
    let local_names: BTreeSet<&str> = registry
        .keys()
        .filter(|k| matches!(k.namespace, Namespace::Root))
        .map(|k| k.name.as_str())
        .collect();

    let mut known: BTreeSet<String> = BTreeSet::new();
    let mut visiting: BTreeSet<String> = BTreeSet::new();

    for qtn in registry.keys() {
        if local_names.contains(qtn.name.as_str()) && !known.contains(&qtn.name) {
            check_type_hashable(registry, qtn, &local_names, &mut known, &mut visiting);
        }
    }

    known
}

/// Checks whether a type (and transitively all types it references) can
/// conform to `Hashable`.
///
/// On success the type name is inserted into `known` so that subsequent
/// checks short-circuit.
fn check_type_hashable(
    registry: &Registry,
    qtn: &QualifiedTypeName,
    local_names: &BTreeSet<&str>,
    known: &mut BTreeSet<String>,
    visiting: &mut BTreeSet<String>,
) -> bool {
    let name = &qtn.name;

    if known.contains(name) {
        return true;
    }
    if visiting.contains(name) {
        // Cycle detected — strongly-connected components are either all
        // hashable or all non-hashable, so optimism is safe here.
        return true;
    }
    if !local_names.contains(name.as_str()) {
        return true; // external type
    }

    let Some(container) = registry.get(qtn) else {
        return false;
    };

    visiting.insert(name.clone());

    let result = match container {
        ContainerFormat::UnitStruct(_) => true,
        ContainerFormat::NewTypeStruct(fmt, _) => {
            fmt_is_hashable(registry, fmt, local_names, known, visiting)
        }
        ContainerFormat::TupleStruct(fmts, _) => fmts
            .iter()
            .all(|f| fmt_is_hashable(registry, f, local_names, known, visiting)),
        ContainerFormat::Struct(fields, _) => fields
            .iter()
            .all(|f| fmt_is_hashable(registry, &f.value, local_names, known, visiting)),
        ContainerFormat::Enum(variants, _) => variants
            .values()
            .all(|v| variant_is_hashable(registry, &v.value, local_names, known, visiting)),
    };

    visiting.remove(name);

    if result {
        known.insert(name.clone());
    }

    result
}

fn variant_is_hashable(
    registry: &Registry,
    format: &VariantFormat,
    local_names: &BTreeSet<&str>,
    known: &mut BTreeSet<String>,
    visiting: &mut BTreeSet<String>,
) -> bool {
    match format {
        VariantFormat::Variable(_) => false,
        VariantFormat::Unit => true,
        VariantFormat::NewType(fmt) => fmt_is_hashable(registry, fmt, local_names, known, visiting),
        VariantFormat::Tuple(fmts) => fmts
            .iter()
            .all(|f| fmt_is_hashable(registry, f, local_names, known, visiting)),
        VariantFormat::Struct(fields) => fields
            .iter()
            .all(|f| fmt_is_hashable(registry, &f.value, local_names, known, visiting)),
    }
}

fn fmt_is_hashable(
    registry: &Registry,
    format: &Format,
    local_names: &BTreeSet<&str>,
    known: &mut BTreeSet<String>,
    visiting: &mut BTreeSet<String>,
) -> bool {
    match format {
        Format::TypeName(qtn) => check_type_hashable(registry, qtn, local_names, known, visiting),
        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
        | Format::Uuid => true,
        Format::Variable(_) | Format::Unit => false,
        Format::Option(inner)
        | Format::Set(inner)
        | Format::Seq(inner)
        | Format::TupleArray { content: inner, .. } => {
            fmt_is_hashable(registry, inner, local_names, known, visiting)
        }
        Format::Map { key, value } => {
            fmt_is_hashable(registry, key, local_names, known, visiting)
                && fmt_is_hashable(registry, value, local_names, known, visiting)
        }
        Format::Tuple(formats) => {
            formats.len() == 1
                && fmt_is_hashable(registry, &formats[0], local_names, known, visiting)
        }
    }
}

/// Computes the set of type names (within this module) that can synthesize
/// Swift `Equatable` conformance.
///
/// Uses depth-first search with optimistic cycle handling: if a type appears
/// on the current evaluation stack it is assumed equatable. This correctly
/// supports self-referential and mutually-recursive types because Swift
/// permits `Equatable` synthesis (or manual `==` emission) for recursive
/// value types as long as all non-recursive stored properties are themselves
/// `Equatable`.
///
/// Multi-element tuples are included because the emitter generates a manual
/// `==` operator using Swift's built-in tuple `==`.
///
/// External types (not present in the registry) are assumed to be equatable.
pub fn compute_equatable_types(registry: &Registry) -> BTreeSet<String> {
    let local_names: BTreeSet<&str> = registry
        .keys()
        .filter(|k| matches!(k.namespace, Namespace::Root))
        .map(|k| k.name.as_str())
        .collect();

    let mut known: BTreeSet<String> = BTreeSet::new();
    let mut visiting: BTreeSet<String> = BTreeSet::new();

    for qtn in registry.keys() {
        if local_names.contains(qtn.name.as_str()) && !known.contains(&qtn.name) {
            check_type_equatable(registry, qtn, &local_names, &mut known, &mut visiting);
        }
    }

    known
}

/// Checks whether a type (and transitively all types it references) can
/// conform to `Equatable`.
///
/// On success the type name is inserted into `known` so that subsequent
/// checks short-circuit.
fn check_type_equatable(
    registry: &Registry,
    qtn: &QualifiedTypeName,
    local_names: &BTreeSet<&str>,
    known: &mut BTreeSet<String>,
    visiting: &mut BTreeSet<String>,
) -> bool {
    let name = &qtn.name;

    if known.contains(name) {
        return true;
    }
    if visiting.contains(name) {
        // Cycle detected — strongly-connected components are either all
        // equatable or all non-equatable, so optimism is safe here.
        return true;
    }
    if !local_names.contains(name.as_str()) {
        return true; // external type
    }

    let Some(container) = registry.get(qtn) else {
        return false;
    };

    visiting.insert(name.clone());

    let result = match container {
        ContainerFormat::UnitStruct(_) => true,
        ContainerFormat::NewTypeStruct(fmt, _) => {
            fmt_is_equatable(registry, fmt, local_names, known, visiting)
        }
        ContainerFormat::TupleStruct(fmts, _) => fmts
            .iter()
            .all(|f| fmt_is_equatable(registry, f, local_names, known, visiting)),
        ContainerFormat::Struct(fields, _) => fields
            .iter()
            .all(|f| fmt_is_equatable(registry, &f.value, local_names, known, visiting)),
        ContainerFormat::Enum(variants, _) => variants
            .values()
            .all(|v| variant_is_equatable(registry, &v.value, local_names, known, visiting)),
    };

    visiting.remove(name);

    if result {
        known.insert(name.clone());
    }

    result
}

fn variant_is_equatable(
    registry: &Registry,
    format: &VariantFormat,
    local_names: &BTreeSet<&str>,
    known: &mut BTreeSet<String>,
    visiting: &mut BTreeSet<String>,
) -> bool {
    match format {
        VariantFormat::Variable(_) => false,
        VariantFormat::Unit => true,
        VariantFormat::NewType(fmt) => {
            fmt_is_equatable(registry, fmt, local_names, known, visiting)
        }
        VariantFormat::Tuple(fmts) => fmts
            .iter()
            .all(|f| fmt_is_equatable(registry, f, local_names, known, visiting)),
        VariantFormat::Struct(fields) => fields
            .iter()
            .all(|f| fmt_is_equatable(registry, &f.value, local_names, known, visiting)),
    }
}

fn fmt_is_equatable(
    registry: &Registry,
    format: &Format,
    local_names: &BTreeSet<&str>,
    known: &mut BTreeSet<String>,
    visiting: &mut BTreeSet<String>,
) -> bool {
    match format {
        Format::TypeName(qtn) => check_type_equatable(registry, qtn, local_names, known, visiting),
        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
        | Format::Uuid => true,
        Format::Variable(_) | Format::Unit => false,
        Format::Option(inner)
        | Format::Set(inner)
        | Format::Seq(inner)
        | Format::TupleArray { content: inner, .. } => {
            fmt_is_equatable(registry, inner, local_names, known, visiting)
        }
        Format::Map { key, value } => {
            fmt_is_equatable(registry, key, local_names, known, visiting)
                && fmt_is_equatable(registry, value, local_names, known, visiting)
        }
        Format::Tuple(formats) => formats
            .iter()
            .all(|f| fmt_is_equatable(registry, f, local_names, known, visiting)),
    }
}

#[cfg(test)]
mod tests;