craby_codegen 0.0.3

Craby code generator
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
use std::collections::HashMap;

use craby_common::{constants, env::Platform, utils::sanitize_str};
use log::error;
use serde::{Deserialize, Serialize};

use crate::utils::to_jni_fn_name;

use super::types::Type;

#[derive(Debug, Deserialize, Serialize)]
pub struct SchemaInfo {
    pub library: Library,
    #[serde(rename = "supportedApplePlatforms")]
    pub supported_apple_platforms: HashMap<String, String>,
    pub schema: SchemaMap,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct SchemaMap {
    pub modules: HashMap<String, Schema>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct Library {
    pub name: String,
    pub config: LibraryConfig,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct LibraryConfig {
    pub name: Option<String>,
    pub r#type: Option<String>,
    #[serde(rename = "jsSrcsDir")]
    pub js_srcs_dir: Option<String>,
    pub android: Option<AndroidConfig>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct AndroidConfig {
    #[serde(rename = "javaPackageName")]
    pub java_package_name: Option<String>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct Schema {
    #[serde(rename = "moduleName")]
    pub module_name: String,
    // NativeModule, Component
    pub r#type: String,
    #[serde(rename = "aliasMap")]
    pub alias_map: HashMap<String, String>,
    #[serde(rename = "enumMap")]
    pub enum_map: HashMap<String, String>,
    pub spec: Spec,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct Spec {
    #[serde(rename = "eventEmitters")]
    pub event_emitters: Vec<String>,
    pub methods: Vec<FunctionSpec>,
}

#[derive(Debug, Deserialize, Serialize)]
#[serde(tag = "type")]
pub enum TypeAnnotation {
    // Reserved types
    ReservedTypeAnnotation {
        name: String,
    },

    // String types
    StringTypeAnnotation,
    StringLiteralTypeAnnotation {
        value: String,
    },
    StringLiteralUnionTypeAnnotation {
        values: Vec<String>,
    },

    // Boolean type
    BooleanTypeAnnotation,

    // Number types
    NumberTypeAnnotation,
    FloatTypeAnnotation,
    DoubleTypeAnnotation,
    Int32TypeAnnotation,
    NumberLiteralTypeAnnotation {
        value: f64,
    },

    // Enum
    EnumDeclaration {
        #[serde(rename = "memberType")]
        member_type: String,
        members: Vec<EnumMember>,
    },

    // Array type
    ArrayTypeAnnotation {
        #[serde(rename = "elementType")]
        element_type: Box<TypeAnnotation>,
    },

    // Function type
    #[serde(rename = "FunctionTypeAnnotation")]
    FunctionTypeAnnotation {
        #[serde(rename = "returnTypeAnnotation")]
        return_type_annotation: Box<TypeAnnotation>,
        params: Vec<Parameter>,
    },

    // Object types
    GenericObjectTypeAnnotation,
    ObjectTypeAnnotation {
        properties: Option<Vec<ObjectProperty>>,
    },

    // Union type
    UnionTypeAnnotation {
        #[serde(rename = "memberType")]
        member_type: String,
        types: Vec<TypeAnnotation>,
    },

    // Mixed type
    MixedTypeAnnotation,

    // Void type
    VoidTypeAnnotation,

    // Nullable wrapper
    NullableTypeAnnotation {
        #[serde(rename = "typeAnnotation")]
        type_annotation: Box<TypeAnnotation>,
    },

    // Type alias
    TypeAliasTypeAnnotation {
        name: String,
    },
}

#[derive(Debug, Deserialize, Serialize)]
pub struct EnumMember {
    pub name: String,
    pub value: serde_json::Value,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct ObjectProperty {
    pub name: String,
    pub optional: bool,
    #[serde(rename = "typeAnnotation")]
    pub type_annotation: TypeAnnotation,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct Parameter {
    pub name: String,
    pub optional: bool,
    #[serde(rename = "typeAnnotation")]
    pub type_annotation: TypeAnnotation,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct FunctionSpec {
    pub name: String,
    pub optional: bool,
    #[serde(rename = "typeAnnotation")]
    pub type_annotation: TypeAnnotation,
}

impl TypeAnnotation {
    pub fn to_rs_type(&self) -> String {
        match self {
            // Boolean type
            TypeAnnotation::BooleanTypeAnnotation => Type::Boolean,

            // Number types
            TypeAnnotation::NumberTypeAnnotation => Type::Number,
            TypeAnnotation::FloatTypeAnnotation => Type::Number,
            TypeAnnotation::DoubleTypeAnnotation => Type::Number,
            TypeAnnotation::Int32TypeAnnotation => Type::Number,
            TypeAnnotation::NumberLiteralTypeAnnotation { .. } => Type::Number,

            // String types
            TypeAnnotation::StringTypeAnnotation => Type::String,
            TypeAnnotation::StringLiteralTypeAnnotation { .. } => Type::String,
            TypeAnnotation::StringLiteralUnionTypeAnnotation { .. } => Type::String,

            _ => {
                error!("Unsupported type annotation: {:?}", self);
                unimplemented!();
                // match unsuported_type_annotation {
                //     // Reserved types
                //     TypeAnnotation::ReservedTypeAnnotation { name } => match name.as_str() {
                //         "RootTag" => Type::Number,
                //         _ => unimplemented!("Unknown reserved type: {}", name),
                //     },

                //     // Enum
                //     TypeAnnotation::EnumDeclaration { member_type, .. } => {
                //         match member_type.as_str() {
                //             "NumberTypeAnnotation" => Type::Number,
                //             "StringTypeAnnotation" => Type::String,
                //             _ => unimplemented!("Unknown enum type: {}", member_type),
                //         }
                //     }

                //     // Array type
                //     TypeAnnotation::ArrayTypeAnnotation { element_type } => {
                //         Type::Array(element_type.to_rs_type())
                //     }

                //     // Function type
                //     TypeAnnotation::FunctionTypeAnnotation { .. } => {
                //         unimplemented!("FunctionTypeAnnotation")
                //     }

                //     // Object types
                //     TypeAnnotation::GenericObjectTypeAnnotation => {
                //         unimplemented!("GenericObjectTypeAnnotation");
                //     }
                //     TypeAnnotation::ObjectTypeAnnotation { .. } => {
                //         unimplemented!("ObjectTypeAnnotation");
                //     }

                //     // Union type
                //     TypeAnnotation::UnionTypeAnnotation { member_type, .. } => {
                //         match member_type.as_str() {
                //             // TODO: Enum type support
                //             "NumberTypeAnnotation" => Type::Number,
                //             "StringTypeAnnotation" => Type::String,
                //             "ObjectTypeAnnotation" => unimplemented!("ObjectTypeAnnotation"),
                //             _ => unimplemented!("Unknown union type: {}", member_type),
                //         }
                //     }

                //     // Mixed type
                //     TypeAnnotation::MixedTypeAnnotation => unimplemented!("MixedTypeAnnotation"),

                //     // Void type
                //     TypeAnnotation::VoidTypeAnnotation => Type::Void,

                //     // Nullable wrapper
                //     TypeAnnotation::NullableTypeAnnotation { type_annotation } => {
                //         Type::Nullable(type_annotation.to_rs_type())
                //     }

                //     // Type alias
                //     TypeAnnotation::TypeAliasTypeAnnotation { .. } => {
                //         unimplemented!("TypeAliasTypeAnnotation")
                //     }
                // }
            }
        }
        .to_string()
    }

    pub fn to_ffi_type(&self, platform: Platform) -> String {
        let ffi_type = match platform {
            Platform::Android => match self {
                // Boolean type
                TypeAnnotation::BooleanTypeAnnotation => "bool",

                // Number types
                TypeAnnotation::NumberTypeAnnotation
                | TypeAnnotation::FloatTypeAnnotation
                | TypeAnnotation::DoubleTypeAnnotation
                | TypeAnnotation::Int32TypeAnnotation
                | TypeAnnotation::NumberLiteralTypeAnnotation { .. } => "jdouble",

                // String types
                TypeAnnotation::StringTypeAnnotation
                | TypeAnnotation::StringLiteralTypeAnnotation { .. }
                | TypeAnnotation::StringLiteralUnionTypeAnnotation { .. } => "jstring",

                _ => {
                    error!("Unsupported type annotation: {:?}", self);
                    unimplemented!();
                }
            },
            Platform::Ios => match self {
                // Boolean type
                TypeAnnotation::BooleanTypeAnnotation => "bool",

                // Number types
                TypeAnnotation::NumberTypeAnnotation
                | TypeAnnotation::FloatTypeAnnotation
                | TypeAnnotation::DoubleTypeAnnotation
                | TypeAnnotation::Int32TypeAnnotation
                | TypeAnnotation::NumberLiteralTypeAnnotation { .. } => "c_double",

                // String types
                TypeAnnotation::StringTypeAnnotation
                | TypeAnnotation::StringLiteralTypeAnnotation { .. }
                | TypeAnnotation::StringLiteralUnionTypeAnnotation { .. } => "*const c_char",

                _ => {
                    error!("Unsupported type annotation: {:?}", self);
                    unimplemented!();
                }
            },
        };

        ffi_type.to_string()
    }

    /// Unwrap nullable type annotations to get the inner type and nullable flag
    pub fn unwrap_nullable(&self) -> (&TypeAnnotation, bool) {
        match self {
            TypeAnnotation::NullableTypeAnnotation { type_annotation } => {
                let (inner, _) = type_annotation.unwrap_nullable();
                (inner, true)
            }
            _ => (self, false),
        }
    }
}

impl Parameter {
    pub fn to_rs_param(&self) -> String {
        let (type_annotation, is_nullable) = self.type_annotation.unwrap_nullable();
        let rust_type = type_annotation.to_rs_type();

        let final_type = if self.optional && !is_nullable {
            format!("Option<{}>", rust_type)
        } else if is_nullable || self.optional {
            if rust_type.starts_with("Option<") {
                rust_type
            } else {
                format!("Option<{}>", rust_type)
            }
        } else {
            rust_type
        };

        format!("{}: {}", self.name, final_type)
    }

    pub fn to_ffi_param(&self, platform: Platform) -> String {
        // TODO: Handle nullable parameters
        let (type_annotation, _nullable) = self.type_annotation.unwrap_nullable();
        let ffi_type = type_annotation.to_ffi_type(platform);

        format!("{}: {}", self.name, ffi_type)
    }
}

impl FunctionSpec {
    pub fn to_rs_fn_sig(&self, sanitize: bool) -> String {
        match &self.type_annotation {
            TypeAnnotation::FunctionTypeAnnotation {
                return_type_annotation,
                params,
            } => {
                let return_type = return_type_annotation.to_rs_type();
                let params_sig = params
                    .iter()
                    .map(|p| p.to_rs_param())
                    .collect::<Vec<_>>()
                    .join(", ");
                let ret_annotation = if return_type == "()" {
                    String::new()
                } else {
                    format!(" -> {}", return_type)
                };
                format!(
                    "fn {}({}){}",
                    if sanitize {
                        sanitize_str(&self.name)
                    } else {
                        self.name.clone()
                    },
                    params_sig,
                    ret_annotation
                )
            }
            _ => unimplemented!("Unsupported type annotation for function: {}", self.name),
        }
    }

    pub fn to_rs_fn(&self, ident: usize, sanitize: bool) -> String {
        match &self.type_annotation {
            TypeAnnotation::FunctionTypeAnnotation { params, .. } => {
                let params = params
                    .iter()
                    .map(|p| p.name.clone())
                    .collect::<Vec<_>>()
                    .join(", ");

                let fn_sig = self.to_rs_fn_sig(sanitize);

                format!(
                    "{ident}pub {fn_sig} {{\n    {ident}{body}\n{ident}}}",
                    fn_sig = fn_sig,
                    body = format!(
                        "{}::{}({})",
                        constants::IMPL_MOD_NAME,
                        sanitize_str(&self.name),
                        params
                    ),
                    ident = " ".repeat(ident)
                )
            }
            _ => unimplemented!("Unsupported type annotation for function: {}", self.name),
        }
    }

    pub fn to_android_ffi_fn(
        &self,
        lib_name: &String,
        mod_name: &String,
        java_package_name: &String,
        class_name: &String,
    ) -> String {
        match &self.type_annotation {
            TypeAnnotation::FunctionTypeAnnotation {
                return_type_annotation,
                params,
            } => {
                let jni_fn_name = to_jni_fn_name(&self.name, java_package_name, class_name);
                let return_type = return_type_annotation.to_ffi_type(Platform::Android);
                let params_sig = params
                    .iter()
                    .map(|p| p.to_ffi_param(Platform::Android))
                    .collect::<Vec<_>>()
                    .join(", ");
                let params_sig = [
                    "_env: JNIEnv".to_string(),
                    "_class: JObject".to_string(),
                    params_sig,
                ]
                .join(", ");
                let params = params
                    .iter()
                    .map(|p| p.name.clone())
                    .collect::<Vec<_>>()
                    .join(", ");

                let ret_annotation = if return_type == "()" {
                    String::new()
                } else {
                    format!(" -> {}", return_type)
                };

                format!(
                    "#[no_mangle]\npub extern \"C\" fn {name}({params_sig}){ret_annotation} {{\n    {body}\n}}",
                    name = jni_fn_name,
                    params_sig = params_sig,
                    ret_annotation = ret_annotation,
                    body = format!("{}::{}::{}({})", lib_name, mod_name, sanitize_str(&self.name), params),
                )
            }
            _ => unimplemented!("Unsupported type annotation for function: {}", self.name),
        }
    }

    pub fn to_ios_ffi_fn(&self, lib_name: &String, mod_name: &String) -> String {
        match &self.type_annotation {
            TypeAnnotation::FunctionTypeAnnotation {
                return_type_annotation,
                params,
            } => {
                let sanitized_name: String = sanitize_str(&self.name);
                let return_type = return_type_annotation.to_ffi_type(Platform::Ios);
                let params_sig = params
                    .iter()
                    .map(|p| p.to_ffi_param(Platform::Ios))
                    .collect::<Vec<_>>()
                    .join(", ");

                let params = params
                    .iter()
                    .map(|p| p.name.clone())
                    .collect::<Vec<_>>()
                    .join(", ");

                let ret_annotation = if return_type == "()" {
                    String::new()
                } else {
                    format!(" -> {}", return_type)
                };

                format!(
                    "#[no_mangle]\npub extern \"C\" fn {name}({params_sig}){ret_annotation} {{\n    {body}\n}}",
                    name = self.name,
                    params_sig = params_sig,
                    ret_annotation = ret_annotation,
                    body = format!("{}::{}::{}({})", lib_name, mod_name, sanitized_name, params),
                )
            }
            _ => unimplemented!("Unsupported type annotation for function: {}", self.name),
        }
    }
}