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
//! Generates idiomatic source code in Swift, Kotlin, TypeScript, and C# from Rust types
//! annotated with `#[derive(Facet)]`.
//!
//! When Rust is the source of truth for a data model shared across platforms (e.g. a mobile
//! app talking to a Rust core over FFI), every target language needs matching type definitions
//! — and, optionally, serialization code to move data across the boundary. Writing these by
//! hand is tedious and error-prone; this crate automates it.
//!
//! Optionally, when an [`Encoding`](generation::Encoding) such as Bincode or JSON is
//! configured, the generated types include `serialize` / `deserialize` methods and the
//! appropriate runtime library is installed alongside the generated code.
//!
//! # Modules
//!
//! - [`reflection`] — walks Rust type metadata (via the [`facet`] crate) and builds a
//! language-neutral [`Registry`]: a flat map from qualified type names to their
//! [`ContainerFormat`](reflection::format::ContainerFormat) descriptions.
//! - [`generation`] — transforms a registry into source code for a target language.
//! Each language (`kotlin`, `csharp`, `swift`, `typescript`) lives behind a feature flag and
//! follows a three-layer pipeline: **Installer** (project scaffolding and manifests) →
//! **Generator** (file-level output with imports and namespaces) → **Emitter** (per-type
//! code emission).
//!
//! # Example
//!
//! Define your shared data model in Rust:
//!
//! ```rust,ignore
//! use facet::Facet;
//!
//! #[derive(Facet)]
//! struct HttpResponse {
//! status: u16,
//! headers: Vec<HttpHeader>,
//! #[facet(bytes)]
//! body: Vec<u8>,
//! }
//!
//! #[derive(Facet)]
//! struct HttpHeader {
//! name: String,
//! value: String,
//! }
//! ```
//!
//! Build a registry from the types, then generate a complete project for one or more target
//! languages:
//!
//! ```rust,ignore
//! use facet_generate::{reflection::RegistryBuilder, generation::{Encoding, kotlin, swift}};
//!
//! let registry = RegistryBuilder::new()
//! .add_type::<HttpResponse>()?
//! .build()?;
//!
//! // Swift package with Bincode serialization
//! swift::Installer::new("MyPackage", &out_dir)
//! .encoding(Encoding::Bincode)
//! .generate(®istry)?;
//!
//! // Kotlin package (type definitions only, no serialization)
//! kotlin::Installer::new("com.example", &out_dir)
//! .generate(®istry)?;
//! ```
//!
//! # Testing
//!
//! Tests are organised in four layers, from fast and narrow to slow and broad:
//!
//! ## Unit tests (snapshot)
//!
//! Each language has snapshot-based tests that assert on generated **text** without touching the
//! filesystem.
//!
//! | Layer | Location | What it covers |
//! |-------|----------|----------------|
//! | Emitter | `generation/<lang>/emitter/tests.rs` (+ `tests_bincode.rs`, `tests_json.rs`) | Output for individual types — no file headers, no imports. Uses the [`emit!`] macro. |
//! | Generator | `generation/<lang>/generator/tests.rs` | Full file output including package declarations, imports, and namespace-qualified names. |
//! | Installer | `generation/<lang>/installer/tests.rs` | Generated manifest strings (`.csproj`, `build.gradle.kts`, `package.json`, `Package.swift`). Still pure string assertions — no files written. |
//!
//! All three use the [`insta`](https://docs.rs/insta) crate for snapshot assertions.
//!
//! ## Cross-language expect-file tests ([`tests`] module, `src/tests/`)
//!
//! Each sub-module defines one or more Rust types and invokes the [`test!`] macro, which reflects
//! the types and runs the full [`CodeGen`](generation::CodeGen) pipeline for every listed language
//! (e.g. `for kotlin, swift`). The output is compared against checked-in expect files
//! (`output.kt`, `output.swift`, …) sitting alongside each `mod.rs`, using the
//! [`expect_test`](https://docs.rs/expect_test) crate. These tests are fast (no compiler
//! invocation) but exercise the complete generator path — including package declarations, imports,
//! and multi-type ordering — across multiple languages in a single test case. Every test should
//! support all languages, except for a few that exercise language-specific features like
//! `#[facet(swift = "Equatable")]` or `#[facet(kotlin = "Parcelable")]`.
//!
//! Gated on `#[cfg(all(test, feature = "generate"))]`.
//!
//! ## Compilation tests (`tests/<lang>_generation.rs`)
//!
//! Integration tests that generate code **and** a project scaffold into a temporary directory,
//! then invoke the real compiler (`dotnet build`, `gradle build`, `swift build`, `deno check`).
//! They verify that the generated code is syntactically and type-correct in the target language.
//! Each file is feature-gated (e.g. `#![cfg(feature = "kotlin")]`) so tests only run when the
//! corresponding toolchain is available.
//!
//! ## Runtime tests (`tests/<lang>_runtime.rs`)
//!
//! End-to-end tests that go one step further: they serialize sample data in Rust (typically with
//! bincode), generate target-language code that deserializes the same bytes, compile and **run**
//! the resulting program, and assert that the round-trip is correct. These catch subtle encoding
//! bugs that snapshot and compilation tests cannot.
// Re-export attribute macros from facet-generate-attrs.
// This allows users to write e.g. `#[facet(facet_generate::bytes)]`
// or `use facet_generate as fg; #[facet(fg::bytes)]`
pub use *;
use BTreeMap;
use crate::;
pub type Result<T, E = Error> = Result;
/// The registry of reflected types — a flat map from qualified type names to their container formats.
///
/// Built by [`reflection::RegistryBuilder`] (typically via the [`reflect!`] macro) and consumed by
/// language-specific code generators in [`generation`].
///
/// Only named container types (structs and enums) get top-level entries. Primitives, `Option`,
/// `Vec`, `Map`, etc. are represented inline as [`Format`](reflection::format::Format) variants
/// within the containers that use them. Cross-type references are expressed as
/// `Format::TypeName(QualifiedTypeName)` — symbolic lookups back into this same map.
///
/// Keys are namespace-qualified, so a type `Foo` in the root namespace and a type `Foo` in
/// namespace `Bar` are separate entries. For example, in Kotlin these would generate as `Foo`
/// and `Bar.Foo` respectively.
pub type Registry = ;
/// Test/convenience macro: reflects the given types and emits code for each
/// container using the specified language tag and encoding.
///
/// Returns `anyhow::Result<String>` containing the generated source.
///
/// ```ignore
/// let code = emit!(MyStruct, MyEnum as Kotlin with Encoding::Json)?;
/// ```
///
/// This skips the [`Module`](generation::module::Module) header (no `package`
/// or `import` statements) — it only emits the type declarations. Useful in
/// tests to assert on individual type output without the file-level boilerplate.
/// **Deprecated since 0.16.0:** The Java generator is deprecated. Use the Kotlin generator instead.
/// Reflects one or more types into a [`Registry`], recursively capturing all reachable types.
///
/// This is a convenience wrapper around [`RegistryBuilder`](reflection::RegistryBuilder) —
/// used directly by the [`emit!`] macro and available for cases where you need the registry
/// without code generation.
///
/// ```ignore
/// let registry = reflect!(MyStruct, MyEnum)?;
/// ```
/// Test-only macro for multi-namespace generation tests.
///
/// Reflects `$facet`, splits the resulting registry by namespace (expecting
/// exactly two namespaces), and runs the full [`CodeGen`](generation::CodeGen)
/// pipeline for each. Returns `(String, String)` — the generated source for
/// the non-root module and the root module, sorted alphabetically by module
/// name.
///
/// This exercises the complete generator path *including* the
/// [`Module`](generation::module::Module) header (package declaration,
/// imports), unlike [`emit!`] which skips it.