bomboni_wasm_core 0.2.0

Internal WASM part of Bomboni library.
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
use std::collections::BTreeMap;

use convert_case::Boundary;
use darling::{FromDeriveInput, FromField, FromMeta, FromVariant, ast::Fields};
use proc_macro2::Ident;
use serde_derive_internals::{
    Ctxt,
    ast::{self, Container as SerdeContainer},
    attr,
};
use syn::{self, DeriveInput, Generics, Member, Path};

use crate::ts_type::TsType;

/// Configuration options for the Wasm derive macro.
pub struct WasmOptions<'a> {
    /// The serde container information from the input type.
    pub serde_container: SerdeContainer<'a>,

    /// Custom path to the wasm-bindgen crate.
    pub wasm_bindgen_crate: Option<Path>,

    /// Custom path to the js-sys crate.
    pub js_sys_crate: Option<Path>,

    /// Custom path to the bomboni crate.
    pub bomboni_crate: Option<Path>,

    /// Custom path to the `bomboni_wasm` crate.
    pub bomboni_wasm_crate: Option<Path>,

    /// Generate `IntoWasmAbi` implementation.
    pub into_wasm_abi: bool,

    /// Generate `FromWasmAbi` implementation.
    pub from_wasm_abi: bool,

    /// Generate enum value object.
    pub enum_value: bool,

    /// Custom `JsValue` conversion configuration.
    pub js_value: Option<JsValueWasm>,

    /// Proxy type configuration.
    pub proxy: Option<ProxyWasm>,

    /// Reference type mapping configuration.
    pub reference_change: ReferenceChangeMap,

    /// Custom name for the type in TypeScript.
    pub rename: Option<String>,

    /// Control wrapper type renaming.
    pub rename_wrapper: Option<bool>,

    /// Rename rule for all fields and variants.
    pub rename_all: Option<attr::RenameRule>,

    /// Word boundaries for renaming.
    pub rename_boundary: Vec<Boundary>,

    /// Override the generated TypeScript type.
    pub override_type: Option<String>,

    /// Field-specific WASM options.
    pub fields: Vec<FieldWasm>,

    /// Variant-specific WASM options.
    pub variants: Vec<VariantWasm>,
}

/// WASM options for a struct field.
pub struct FieldWasm {
    /// The field member (name or index).
    pub member: Member,

    /// Whether the field is optional.
    pub optional: bool,

    /// Reference type mapping for this field.
    pub reference_change: ReferenceChangeMap,

    /// Override the TypeScript type for this field.
    pub override_type: Option<String>,

    /// Control wrapper type renaming for this field.
    pub rename_wrapper: Option<bool>,

    /// Force the field to always be present in TypeScript.
    pub always_some: Option<bool>,

    /// Custom name for the field in TypeScript.
    pub rename: Option<String>,
}

/// WASM options for an enum variant.
pub struct VariantWasm {
    /// The identifier of the variant.
    pub ident: Ident,

    /// Reference type mapping for this variant.
    pub reference_change: ReferenceChangeMap,

    /// Override the TypeScript type for this variant.
    pub override_type: Option<String>,

    /// Control wrapper type renaming for this variant.
    pub rename_wrapper: Option<bool>,

    /// Field-specific WASM options for this variant's fields.
    pub fields: Vec<FieldWasm>,

    /// Custom name for the variant in TypeScript.
    pub rename: Option<String>,
}

/// Maps Rust reference types to TypeScript types.
#[derive(Debug, Clone, Default)]
pub struct ReferenceChangeMap {
    /// Simple name mapping for the reference type.
    pub name: Option<String>,

    /// Complex type mappings for multiple reference types.
    pub types: BTreeMap<String, TsType>,
}

/// Configuration for custom `JsValue` conversions.
#[derive(Debug)]
pub struct JsValueWasm {
    /// Custom conversion function from Rust type to `JsValue`.
    pub into: Option<Path>,

    /// Custom conversion function from `JsValue` to Rust type.
    pub try_from: Option<Path>,

    /// Convert the type to/from JavaScript strings.
    pub convert_string: bool,
}

/// Configuration for proxy type WASM bindings.
#[derive(Debug)]
pub struct ProxyWasm {
    /// The proxy type to use for WASM bindings.
    pub proxy: Path,

    /// Custom conversion function from original type to proxy type.
    pub into: Option<Path>,

    /// Custom conversion function from proxy type to original type.
    pub try_from: Option<Path>,
}

#[derive(Debug, FromDeriveInput)]
#[darling(attributes(wasm))]
struct Attributes {
    wasm_bindgen_crate: Option<Path>,
    js_sys_crate: Option<Path>,
    bomboni_crate: Option<Path>,
    bomboni_wasm_crate: Option<Path>,
    wasm_abi: Option<bool>,
    into_wasm_abi: Option<bool>,
    from_wasm_abi: Option<bool>,
    enum_value: Option<bool>,
    js_value: Option<JsValueWasm>,
    proxy: Option<ProxyWasm>,
    rename: Option<String>,
    change_ref: Option<ReferenceChangeMap>,
    change_refs: Option<ReferenceChangeMap>,
    rename_wrapper: Option<bool>,
    rename_all: Option<String>,
    rename_boundary: Option<String>,
    override_type: Option<String>,
    data: darling::ast::Data<VariantAttributes, FieldAttributes>,
}

#[derive(Debug, FromField)]
#[darling(attributes(wasm))]
struct FieldAttributes {
    ident: Option<Ident>,
    change_ref: Option<ReferenceChangeMap>,
    change_refs: Option<ReferenceChangeMap>,
    override_type: Option<String>,
    rename_wrapper: Option<bool>,
    always_some: Option<bool>,
    rename: Option<String>,
}

#[derive(Debug, FromVariant)]
#[darling(attributes(wasm))]
struct VariantAttributes {
    ident: Ident,
    change_ref: Option<ReferenceChangeMap>,
    change_refs: Option<ReferenceChangeMap>,
    override_type: Option<String>,
    rename_wrapper: Option<bool>,
    fields: Fields<FieldAttributes>,
    rename: Option<String>,
}

impl<'a> WasmOptions<'a> {
    /// Creates `WasmOptions` from a `DeriveInput`.
    ///
    /// # Errors
    ///
    /// Will return an error if the input is not a valid struct or enum for WASM,
    /// if serde attributes are invalid, or if incompatible attribute combinations are used.
    pub fn from_derive_input(input: &'a DeriveInput) -> syn::Result<Self> {
        let ctx = Ctxt::new();
        let serde_container = match SerdeContainer::from_ast(
            &ctx,
            input,
            serde_derive_internals::Derive::Serialize,
        ) {
            Some(container) => {
                ctx.check()?;
                container
            }
            None => {
                return Err(ctx.check().expect_err("serde_container is None"));
            }
        };
        let attributes = match Attributes::from_derive_input(input) {
            Ok(v) => v,
            Err(err) => {
                return Err(err.into());
            }
        };

        let (fields, variants) = match (&serde_container.data, attributes.data) {
            (ast::Data::Struct(_, serde_fields), darling::ast::Data::Struct(field_attributes)) => {
                let fields = get_fields(serde_fields, &field_attributes);
                (fields, Vec::new())
            }
            (ast::Data::Enum(serde_variants), darling::ast::Data::Enum(variant_attributes)) => {
                let variants = get_variants(serde_variants, &variant_attributes);
                (Vec::new(), variants)
            }
            _ => {
                return Err(syn::Error::new_spanned(
                    input,
                    "invalid struct or enum for WASM",
                ));
            }
        };

        let wasm_abi = attributes.wasm_abi.unwrap_or_default();

        let rename_all = if let Some(rename_all) = attributes.rename_all {
            Some(
                attr::RenameRule::from_str(&rename_all)
                    .map_err(|err| syn::Error::new_spanned(input, err))?,
            )
        } else {
            None
        };
        let rename_boundary = attributes
            .rename_boundary
            .as_ref()
            .map_or_else(Vec::new, |rename_boundary| {
                Boundary::defaults_from(rename_boundary)
            });

        if attributes.enum_value.unwrap_or_default()
            && (attributes.js_value.is_some() || attributes.proxy.is_some())
        {
            return Err(syn::Error::new_spanned(
                input,
                "`enum_value` cannot be used with `js_value` or `proxy`",
            ));
        }
        if attributes.js_value.is_some()
            && (attributes.enum_value.unwrap_or_default() || attributes.proxy.is_some())
        {
            return Err(syn::Error::new_spanned(
                input,
                "`js_value` cannot be used with `enum_value` or `proxy`",
            ));
        }
        if attributes.proxy.is_some()
            && (attributes.enum_value.unwrap_or_default() || attributes.js_value.is_some())
        {
            return Err(syn::Error::new_spanned(
                input,
                "`proxy` cannot be used with `enum_value` or `js_value`",
            ));
        }

        Ok(Self {
            serde_container,
            wasm_bindgen_crate: attributes.wasm_bindgen_crate,
            js_sys_crate: attributes.js_sys_crate,
            bomboni_crate: attributes.bomboni_crate,
            bomboni_wasm_crate: attributes.bomboni_wasm_crate,
            into_wasm_abi: attributes.into_wasm_abi.unwrap_or(wasm_abi),
            from_wasm_abi: attributes.from_wasm_abi.unwrap_or(wasm_abi),
            enum_value: attributes.enum_value.unwrap_or_default(),
            js_value: attributes.js_value,
            proxy: attributes.proxy,
            rename: attributes.rename,
            reference_change: attributes
                .change_ref
                .as_ref()
                .or(attributes.change_refs.as_ref())
                .cloned()
                .unwrap_or_default(),
            rename_wrapper: attributes.rename_wrapper,
            rename_all,
            rename_boundary,
            override_type: attributes.override_type,
            fields,
            variants,
        })
    }

    /// Gets the identifier of the type.
    pub const fn ident(&self) -> &Ident {
        &self.serde_container.ident
    }

    /// Gets the name of the type.
    pub fn name(&self) -> &str {
        self.rename.as_ref().map_or_else(
            || self.serde_attrs().name().serialize_name(),
            String::as_str,
        )
    }

    /// Gets the serde data for the type.
    pub const fn serde_data(&self) -> &ast::Data<'_> {
        &self.serde_container.data
    }

    /// Gets the generic parameters for the type.
    pub const fn generics(&self) -> &Generics {
        self.serde_container.generics
    }

    /// Gets the serde attributes for the type.
    pub const fn serde_attrs(&self) -> &attr::Container {
        &self.serde_container.attrs
    }
}

impl FromMeta for ReferenceChangeMap {
    fn from_expr(expr: &syn::Expr) -> darling::Result<Self> {
        match expr {
            syn::Expr::Lit(syn::ExprLit {
                lit: syn::Lit::Str(name),
                ..
            }) => Ok(Self {
                name: Some(name.value()),
                types: BTreeMap::default(),
            }),
            syn::Expr::Array(syn::ExprArray { elems, .. }) => {
                let mut types = BTreeMap::new();
                for elem in elems {
                    if let syn::Expr::Tuple(syn::ExprTuple { elems, .. }) = elem {
                        if elems.len() != 2 {
                            return Err(darling::Error::custom(
                                "expected tuple of length 2 containing source and target names",
                            )
                            .with_span(elem));
                        }
                        if let (
                            syn::Expr::Lit(syn::ExprLit {
                                lit: syn::Lit::Str(source),
                                ..
                            }),
                            syn::Expr::Lit(syn::ExprLit {
                                lit: syn::Lit::Str(target),
                                ..
                            }),
                        ) = (&elems[0], &elems[1])
                        {
                            types.insert(
                                source.value(),
                                TsType::Reference {
                                    name: target.value(),
                                    type_params: Vec::new(),
                                },
                            );
                        } else {
                            return Err(darling::Error::custom(
                                "expected tuple of length 2 containing source and target names",
                            ));
                        }
                    } else {
                        return Err(darling::Error::custom(
                            "expected tuple of length 2 containing source and target names",
                        )
                        .with_span(elem));
                    }
                }
                Ok(Self { name: None, types })
            }
            _ => Err(darling::Error::custom("expected string literal")),
        }
    }
}

impl FromMeta for ProxyWasm {
    fn from_expr(expr: &syn::Expr) -> darling::Result<Self> {
        match expr {
            syn::Expr::Path(syn::ExprPath { path, .. }) => Ok(Self {
                proxy: path.clone(),
                into: None,
                try_from: None,
            }),
            _ => Err(darling::Error::custom("expected proxy path").with_span(expr)),
        }
    }

    fn from_list(items: &[darling::ast::NestedMeta]) -> darling::Result<Self> {
        let mut proxy = None;
        let mut into = None;
        let mut try_from = None;
        for item in items {
            match item {
                darling::ast::NestedMeta::Meta(syn::Meta::NameValue(syn::MetaNameValue {
                    path,
                    value: syn::Expr::Path(value),
                    ..
                })) => {
                    if path.is_ident("source") {
                        if proxy.is_some() {
                            return Err(darling::Error::custom("proxy `source` already specified")
                                .with_span(item));
                        }
                        proxy = Some(value.path.clone());
                    } else if path.is_ident("into") {
                        if into.is_some() {
                            return Err(
                                darling::Error::custom("`into` already specified").with_span(item)
                            );
                        }
                        into = Some(value.path.clone());
                    } else if path.is_ident("try_from") {
                        if try_from.is_some() {
                            return Err(darling::Error::custom("`try_from` already specified")
                                .with_span(item));
                        }
                        try_from = Some(value.path.clone());
                    } else {
                        return Err(darling::Error::custom("invalid option").with_span(item));
                    }
                }
                _ => {
                    return Err(darling::Error::custom("invalid options").with_span(item));
                }
            }
        }
        Ok(Self {
            proxy: proxy.ok_or_else(|| darling::Error::custom("proxy `source` not specified"))?,
            into,
            try_from,
        })
    }
}

impl FromMeta for JsValueWasm {
    fn from_list(items: &[darling::ast::NestedMeta]) -> darling::Result<Self> {
        let mut into = None;
        let mut try_from = None;
        let mut convert_string = false;
        for item in items {
            match item {
                darling::ast::NestedMeta::Meta(syn::Meta::NameValue(syn::MetaNameValue {
                    path,
                    value: syn::Expr::Path(value),
                    ..
                })) => {
                    if path.is_ident("into") {
                        if into.is_some() {
                            return Err(
                                darling::Error::custom("`into` already specified").with_span(item)
                            );
                        }
                        into = Some(value.path.clone());
                    } else if path.is_ident("try_from") {
                        if try_from.is_some() {
                            return Err(darling::Error::custom("`try_from` already specified")
                                .with_span(item));
                        }
                        try_from = Some(value.path.clone());
                    } else {
                        return Err(
                            darling::Error::custom("expected `into` or `try_from`").with_span(item)
                        );
                    }
                }
                darling::ast::NestedMeta::Meta(syn::Meta::Path(path)) => {
                    if path.is_ident("convert_string") {
                        convert_string = true;
                    } else {
                        return Err(darling::Error::custom("invalid option").with_span(item));
                    }
                }
                _ => {
                    return Err(darling::Error::custom("invalid options").with_span(item));
                }
            }
        }
        Ok(Self {
            into,
            try_from,
            convert_string,
        })
    }

    fn from_word() -> darling::Result<Self> {
        Ok(Self {
            into: None,
            try_from: None,
            convert_string: false,
        })
    }
}

fn get_fields(
    serde_fields: &[ast::Field],
    field_attributes: &Fields<FieldAttributes>,
) -> Vec<FieldWasm> {
    let mut fields = Vec::new();

    for serde_field in serde_fields {
        let mut optional = false;
        if let Some(expr) = serde_field.attrs.skip_serializing_if() {
            let last_step = expr.path.segments.iter().rev().nth(1);
            optional |= matches!(
                last_step,
                Some(
                    syn::PathSegment { ident, .. }
                ) if ident == "is_none"
            );
            optional |= matches!(
                last_step,
                Some(
                    syn::PathSegment { ident, .. }
                ) if ident == "is_default"
            );
        }

        let Some((_, field)) =
            field_attributes
                .iter()
                .enumerate()
                .find(|(i, field)| match &serde_field.member {
                    Member::Named(serde_ident) => Some(serde_ident) == field.ident.as_ref(),
                    Member::Unnamed(serde_index) => serde_index.index as usize == *i,
                })
        else {
            continue;
        };
        let reference_change = field
            .change_ref
            .as_ref()
            .or(field.change_refs.as_ref())
            .cloned()
            .unwrap_or_default();
        let rename_wrapper = field.rename_wrapper;

        fields.push(FieldWasm {
            member: serde_field.member.clone(),
            optional,
            reference_change,
            override_type: field.override_type.clone(),
            rename_wrapper,
            always_some: field.always_some,
            rename: field.rename.clone(),
        });
    }

    fields
}

fn get_variants(
    serde_variants: &[ast::Variant],
    variant_attributes: &[VariantAttributes],
) -> Vec<VariantWasm> {
    let mut variants = Vec::new();

    for serde_variant in serde_variants {
        let Some(variant) = variant_attributes
            .iter()
            .find(|variant| variant.ident == serde_variant.ident)
        else {
            continue;
        };
        let reference_change = variant
            .change_ref
            .as_ref()
            .or(variant.change_refs.as_ref())
            .cloned()
            .unwrap_or_default();
        let rename_wrapper = variant.rename_wrapper;

        variants.push(VariantWasm {
            ident: serde_variant.ident.clone(),
            reference_change,
            override_type: variant.override_type.clone(),
            rename_wrapper,
            fields: get_fields(&serde_variant.fields, &variant.fields),
            rename: variant.rename.clone(),
        });
    }

    variants
}