1use std::path::{Path, PathBuf};
4
5use serde::{Deserialize, Serialize};
6
7use crate::error::CodegenError;
8
9#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
11#[serde(tag = "kind", rename_all = "snake_case")]
12pub enum PropType {
13 Bool,
14 Int32,
15 Float,
16 Double,
17 String,
18 Color,
19 ImageSource,
20 Point,
21 EdgeInsets,
22 Dimension,
23 StringEnum { variants: Vec<String> },
24 IntEnum { variants: Vec<i32> },
25 Array { item: Box<PropType> },
26 Object { fields: Vec<ObjectField> },
27 Mixed,
28 Void,
29 Promise { inner: Box<PropType> },
30 Callback { params: Vec<ParamSpec> },
31}
32
33#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
35pub struct ObjectField {
36 pub name: String,
37 pub field_type: PropType,
38 #[serde(default)]
39 pub optional: bool,
40 #[serde(default)]
41 pub nullable: bool,
42}
43
44#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
46#[serde(tag = "kind", content = "value", rename_all = "snake_case")]
47pub enum DefaultValue {
48 Null,
49 Bool(bool),
50 Int(i64),
51 Float(f64),
52 String(String),
53}
54
55#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
57pub struct PropSpec {
58 pub name: String,
59 pub prop_type: PropType,
60 #[serde(default)]
61 pub optional: bool,
62 #[serde(default)]
63 pub nullable: bool,
64 #[serde(default, skip_serializing_if = "Option::is_none")]
65 pub default_value: Option<DefaultValue>,
66 #[serde(default, skip_serializing_if = "Option::is_none")]
67 pub doc: Option<String>,
68}
69
70#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
72#[serde(rename_all = "snake_case")]
73pub enum EventBubbling {
74 #[default]
75 Direct,
76 Bubbling,
77}
78
79#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
81pub struct EventSpec {
82 pub name: String,
83 #[serde(default)]
84 pub bubbling: EventBubbling,
85 #[serde(default = "default_true")]
86 pub optional: bool,
87 #[serde(default, skip_serializing_if = "Option::is_none")]
88 pub paper_top_level_name: Option<String>,
89 #[serde(default)]
90 pub payload: Vec<ObjectField>,
91 #[serde(default, skip_serializing_if = "Option::is_none")]
92 pub payload_type_name: Option<String>,
93 #[serde(default, skip_serializing_if = "Option::is_none")]
94 pub doc: Option<String>,
95}
96
97fn default_true() -> bool {
98 true
99}
100
101#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
103pub struct ParamSpec {
104 pub name: String,
105 pub param_type: PropType,
106 #[serde(default)]
107 pub optional: bool,
108}
109
110#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
112pub struct CommandSpec {
113 pub name: String,
114 #[serde(default)]
115 pub params: Vec<ParamSpec>,
116}
117
118#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
120pub struct ComponentOptions {
121 #[serde(default, skip_serializing_if = "Option::is_none")]
122 pub paper_component_name: Option<String>,
123 #[serde(default, skip_serializing_if = "Vec::is_empty")]
124 pub excluded_platforms: Vec<String>,
125 #[serde(default)]
126 pub interface_only: bool,
127}
128
129#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
131pub struct RnComponentSpec {
132 pub component_name: String,
133 pub props_type_name: String,
134 pub extends_view_props: bool,
135 pub props: Vec<PropSpec>,
136 pub events: Vec<EventSpec>,
137 pub commands: Vec<CommandSpec>,
138 pub options: ComponentOptions,
139}
140
141#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
143pub struct TurboModuleMethod {
144 pub name: String,
145 pub params: Vec<ParamSpec>,
146 pub return_type: PropType,
147 #[serde(default, skip_serializing_if = "Option::is_none")]
148 pub doc: Option<String>,
149}
150
151#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
153pub struct TurboModuleSpec {
154 pub module_name: String,
155 pub enforcing: bool,
156 pub methods: Vec<TurboModuleMethod>,
157}
158
159#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
161#[serde(tag = "spec_kind", rename_all = "snake_case")]
162pub enum ParsedRnSpec {
163 Component(RnComponentSpec),
164 TurboModule(TurboModuleSpec),
165}
166
167impl ParsedRnSpec {
168 pub fn name(&self) -> &str {
169 match self {
170 Self::Component(c) => &c.component_name,
171 Self::TurboModule(m) => &m.module_name,
172 }
173 }
174}
175
176
177#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
179pub struct GeneratedFile {
180 pub relative_path: String,
181 pub content: String,
182}
183
184#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
186pub struct ImportRnOutput {
187 pub spec: ParsedRnSpec,
188 pub rust_module: GeneratedFile,
189 pub cpp_turbomodule_host: Option<GeneratedFile>,
191}
192
193impl ImportRnOutput {
194 pub fn files(&self) -> Vec<&GeneratedFile> {
195 std::iter::once(&self.rust_module)
196 .chain(self.cpp_turbomodule_host.as_ref())
197 .collect()
198 }
199
200 pub fn write_to_dir(&self, out_dir: &Path) -> Result<Vec<PathBuf>, CodegenError> {
201 write_generated_files(out_dir, &self.files())
202 }
203}
204
205#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
207#[serde(rename_all = "kebab-case")]
208pub enum ExportTarget {
209 ReactNative,
210 NitroUi,
211 Flutter,
212 SwiftUi,
213 Compose,
214 WebComponent,
215}
216
217impl ExportTarget {
218 pub const ALL: &'static [ExportTarget] = &[
219 ExportTarget::ReactNative,
220 ExportTarget::NitroUi,
221 ExportTarget::Flutter,
222 ExportTarget::SwiftUi,
223 ExportTarget::Compose,
224 ExportTarget::WebComponent,
225 ];
226
227 pub fn dir_name(self) -> &'static str {
228 match self {
229 Self::ReactNative => "react-native",
230 Self::NitroUi => "nitro-ui",
231 Self::Flutter => "flutter",
232 Self::SwiftUi => "swiftui",
233 Self::Compose => "compose",
234 Self::WebComponent => "web-component",
235 }
236 }
237
238 pub fn from_name(raw: &str) -> Result<Self, CodegenError> {
239 let s = raw.trim();
240 if s.eq_ignore_ascii_case("rn")
241 || s.eq_ignore_ascii_case("react-native")
242 || s.eq_ignore_ascii_case("react_native")
243 || s.eq_ignore_ascii_case("fabric")
244 {
245 Ok(Self::ReactNative)
246 } else if s.eq_ignore_ascii_case("nitro-ui")
247 || s.eq_ignore_ascii_case("nitro_ui")
248 || s.eq_ignore_ascii_case("nitroui")
249 || s.eq_ignore_ascii_case("nitro")
250 {
251 Ok(Self::NitroUi)
252 } else if s.eq_ignore_ascii_case("flutter") || s.eq_ignore_ascii_case("dart") {
253 Ok(Self::Flutter)
254 } else if s.eq_ignore_ascii_case("swiftui")
255 || s.eq_ignore_ascii_case("swift-ui")
256 || s.eq_ignore_ascii_case("swift")
257 || s.eq_ignore_ascii_case("ios")
258 {
259 Ok(Self::SwiftUi)
260 } else if s.eq_ignore_ascii_case("compose")
261 || s.eq_ignore_ascii_case("jetpack-compose")
262 || s.eq_ignore_ascii_case("kotlin")
263 || s.eq_ignore_ascii_case("android")
264 {
265 Ok(Self::Compose)
266 } else if s.eq_ignore_ascii_case("web")
267 || s.eq_ignore_ascii_case("web-component")
268 || s.eq_ignore_ascii_case("web_component")
269 || s.eq_ignore_ascii_case("wasm")
270 || s.eq_ignore_ascii_case("custom-element")
271 {
272 Ok(Self::WebComponent)
273 } else {
274 Err(CodegenError::validation(format!(
275 "unknown export target '{s}'; expected one of: rn, nitro-ui, web, swiftui, compose, flutter"
276 )))
277 }
278 }
279}
280
281#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
283pub struct ExportRnOutput {
284 pub view_name: String,
285 pub native_component_ts: GeneratedFile,
286 pub ios_component_view_mm: GeneratedFile,
287 pub android_view_manager_kt: GeneratedFile,
288 pub android_react_package_kt: GeneratedFile,
289 pub windows_component_view_hpp: GeneratedFile,
290}
291
292impl ExportRnOutput {
293 pub fn files(&self) -> [&GeneratedFile; 5] {
294 [
295 &self.native_component_ts,
296 &self.ios_component_view_mm,
297 &self.android_view_manager_kt,
298 &self.android_react_package_kt,
299 &self.windows_component_view_hpp,
300 ]
301 }
302
303 pub fn write_to_dir(&self, out_dir: &Path) -> Result<Vec<PathBuf>, CodegenError> {
304 write_generated_files(out_dir, &self.files())
305 }
306}
307
308#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
310pub struct ExportNitroUiOutput {
311 pub view_name: String,
312 pub nitro_spec_ts: GeneratedFile,
313 pub view_ts: GeneratedFile,
314 pub ios_hybrid_swift: GeneratedFile,
315 pub android_hybrid_kt: GeneratedFile,
316 pub nitro_json: GeneratedFile,
317}
318
319impl ExportNitroUiOutput {
320 pub fn files(&self) -> [&GeneratedFile; 5] {
321 [
322 &self.nitro_spec_ts,
323 &self.view_ts,
324 &self.ios_hybrid_swift,
325 &self.android_hybrid_kt,
326 &self.nitro_json,
327 ]
328 }
329
330 pub fn write_to_dir(&self, out_dir: &Path) -> Result<Vec<PathBuf>, CodegenError> {
331 write_generated_files(out_dir, &self.files())
332 }
333}
334
335#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
338pub struct ExportFlutterOutput {
339 pub view_name: String,
340 pub dart_widget: GeneratedFile,
341}
342
343impl ExportFlutterOutput {
344 pub fn files(&self) -> [&GeneratedFile; 1] {
345 [&self.dart_widget]
346 }
347
348 pub fn write_to_dir(&self, out_dir: &Path) -> Result<Vec<PathBuf>, CodegenError> {
349 write_generated_files(out_dir, &self.files())
350 }
351}
352
353#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
355pub struct ExportSwiftUiOutput {
356 pub view_name: String,
357 pub swift_view: GeneratedFile,
358}
359
360impl ExportSwiftUiOutput {
361 pub fn files(&self) -> [&GeneratedFile; 1] {
362 [&self.swift_view]
363 }
364
365 pub fn write_to_dir(&self, out_dir: &Path) -> Result<Vec<PathBuf>, CodegenError> {
366 write_generated_files(out_dir, &self.files())
367 }
368}
369
370#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
372pub struct ExportComposeOutput {
373 pub view_name: String,
374 pub compose_kt: GeneratedFile,
375}
376
377impl ExportComposeOutput {
378 pub fn files(&self) -> [&GeneratedFile; 1] {
379 [&self.compose_kt]
380 }
381
382 pub fn write_to_dir(&self, out_dir: &Path) -> Result<Vec<PathBuf>, CodegenError> {
383 write_generated_files(out_dir, &self.files())
384 }
385}
386
387#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
389pub struct ExportWebComponentOutput {
390 pub view_name: String,
391 pub tag_name: String,
392 pub element_js: GeneratedFile,
393 pub types_dts: GeneratedFile,
394 pub react_tsx: GeneratedFile,
395}
396
397impl ExportWebComponentOutput {
398 pub fn files(&self) -> [&GeneratedFile; 3] {
399 [&self.element_js, &self.types_dts, &self.react_tsx]
400 }
401
402 pub fn write_to_dir(&self, out_dir: &Path) -> Result<Vec<PathBuf>, CodegenError> {
403 write_generated_files(out_dir, &self.files())
404 }
405}
406
407#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
409pub struct MultiTargetExportOutput {
410 pub target: ExportTarget,
411 pub files: Vec<GeneratedFile>,
412}
413
414impl MultiTargetExportOutput {
415 pub fn write_to_dir(&self, out_dir: &Path) -> Result<Vec<PathBuf>, CodegenError> {
416 let refs: Vec<&GeneratedFile> = self.files.iter().collect();
417 write_generated_files(out_dir, &refs)
418 }
419}
420
421pub(crate) fn write_generated_files(
422 out_dir: &Path,
423 files: &[&GeneratedFile],
424) -> Result<Vec<PathBuf>, CodegenError> {
425 let mut written = Vec::with_capacity(files.len());
426 for file in files {
427 let dest = out_dir.join(&file.relative_path);
428 if let Some(parent) = dest.parent() {
429 std::fs::create_dir_all(parent)
430 .map_err(|e| CodegenError::io(parent.display().to_string(), e))?;
431 }
432 std::fs::write(&dest, &file.content)
433 .map_err(|e| CodegenError::io(dest.display().to_string(), e))?;
434 written.push(dest);
435 }
436 Ok(written)
437}