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
///
/// ## Difference from `napi::Either`
///
/// `napi::Either` is designed for different type of values.
/// Under the hood, the type is differentiated by `napi_typeof`.
///
/// `tagged_union` is designed for different variants of the same type.
///
/// ## Example
/// ```ignore
/// #[tagged_union]
/// enum Foo {
/// V1(V1),
/// #[napi(ts_type = "Record<string, string>")]
/// V2(V2)
/// }
///
/// ⬇️⬇️⬇️
///
/// #[napi(js_name = "Foo")]
/// struct __rspack_napi_macros_Foo {
/// #[napi(ts_type = "FooTypes")]
/// r#type: String,
/// V1: Option<V1>,
/// #[napi(ts_type = "Record<string, string>")]
/// V2: Option<V2>
/// }
///
/// #[napi(string_enum)]
/// enum FooTypes {
/// V1,
/// V2
/// }
///
/// impl From<__rspack_napi_macros_Foo> for Foo {
/// fn from(value: __rspack_napi_macros_Foo) -> Self {
/// // ..
/// }
/// }
///
/// impl FromNapiValue for Foo {
/// fn from_napi_value(env, val) -> Self {
/// let item: __rspack_napi_macros_Foo = val.into();
/// item.into()
/// }
/// }
/// ```