Skip to main content

napi_derive_backend/
ast.rs

1use convert_case::Case;
2use proc_macro2::{Ident, Literal};
3use syn::{Attribute, Expr, Type};
4
5#[derive(Debug, Clone)]
6pub struct NapiFn {
7  pub name: Ident,
8  pub js_name: String,
9  pub module_exports: bool,
10  pub attrs: Vec<Attribute>,
11  pub args: Vec<NapiFnArg>,
12  pub ret: Option<syn::Type>,
13  pub is_ret_result: bool,
14  pub is_async: bool,
15  pub within_async_runtime: bool,
16  pub fn_self: Option<FnSelf>,
17  pub kind: FnKind,
18  pub vis: syn::Visibility,
19  pub parent: Option<Ident>,
20  pub parent_js_name: Option<String>,
21  pub strict: bool,
22  pub return_if_invalid: bool,
23  pub js_mod: Option<String>,
24  pub ts_generic_types: Option<String>,
25  pub ts_type: Option<String>,
26  pub ts_args_type: Option<String>,
27  pub ts_return_type: Option<String>,
28  pub skip_typescript: bool,
29  pub comments: Vec<String>,
30  pub parent_is_generator: bool,
31  pub parent_is_async_generator: bool,
32  pub writable: bool,
33  pub enumerable: bool,
34  pub configurable: bool,
35  pub catch_unwind: bool,
36  pub unsafe_: bool,
37  pub register_name: Ident,
38  pub no_export: bool,
39}
40
41#[derive(Debug, Clone)]
42pub struct CallbackArg {
43  pub pat: Box<syn::Pat>,
44  pub args: Vec<syn::Type>,
45  pub ret: Option<syn::Type>,
46}
47
48#[derive(Debug, Clone)]
49pub struct NapiFnArg {
50  pub kind: NapiFnArgKind,
51  pub ts_arg_type: Option<String>,
52}
53
54impl NapiFnArg {
55  /// if type was overridden with `#[napi(ts_arg_type = "...")]` use that instead
56  pub fn use_overridden_type_or(&self, default: impl FnOnce() -> String) -> String {
57    self.ts_arg_type.as_ref().cloned().unwrap_or_else(default)
58  }
59}
60
61#[derive(Debug, Clone)]
62pub enum NapiFnArgKind {
63  PatType(Box<syn::PatType>),
64  Callback(Box<CallbackArg>),
65}
66
67#[derive(Debug, Clone, PartialEq, Eq)]
68pub enum FnKind {
69  Normal,
70  Constructor,
71  Factory,
72  Getter,
73  Setter,
74}
75
76#[derive(Debug, Clone, PartialEq, Eq)]
77pub enum FnSelf {
78  Value,
79  Ref,
80  MutRef,
81}
82
83#[derive(Debug, Clone)]
84pub struct NapiStruct {
85  pub name: Ident,
86  pub js_name: String,
87  pub comments: Vec<String>,
88  pub js_mod: Option<String>,
89  pub use_nullable: bool,
90  pub register_name: Ident,
91  pub kind: NapiStructKind,
92  pub has_lifetime: bool,
93  pub is_generator: bool,
94  pub is_async_generator: bool,
95  /// Optional crate-unique salt from `#[napi(type_tag = "...")]`. When set on a
96  /// class it REPLACES the default `crate@version` identity component of the
97  /// content-derived class type tag (module_path + ClassName still apply), so a
98  /// class's tag cannot collide with an unrelated addon that happens to share
99  /// the same crate name@version + module path + class name. `None` keeps the
100  /// default `crate@version::module_path::ClassName` derivation. Runtime-only;
101  /// never emitted into TypeScript.
102  pub type_tag: Option<String>,
103}
104
105#[derive(Debug, Clone)]
106pub enum NapiStructKind {
107  Transparent(NapiTransparent),
108  Class(NapiClass),
109  Object(NapiObject),
110  StructuredEnum(NapiStructuredEnum),
111  Array(NapiArray),
112}
113
114#[derive(Debug, Clone)]
115pub struct NapiTransparent {
116  pub ty: Type,
117  pub object_from_js: bool,
118  pub object_to_js: bool,
119}
120
121#[derive(Debug, Clone)]
122pub struct NapiClass {
123  pub fields: Vec<NapiStructField>,
124  pub ctor: bool,
125  pub implement_iterator: bool,
126  pub implement_async_iterator: bool,
127  pub is_tuple: bool,
128  pub use_custom_finalize: bool,
129}
130
131#[derive(Debug, Clone)]
132pub struct NapiObject {
133  pub fields: Vec<NapiStructField>,
134  pub object_from_js: bool,
135  pub object_to_js: bool,
136  pub is_tuple: bool,
137}
138
139#[derive(Debug, Clone)]
140pub struct NapiArray {
141  pub fields: Vec<NapiStructField>,
142  pub object_from_js: bool,
143  pub object_to_js: bool,
144}
145
146#[derive(Debug, Clone)]
147pub struct NapiStructuredEnum {
148  pub variants: Vec<NapiStructuredEnumVariant>,
149  pub object_from_js: bool,
150  pub object_to_js: bool,
151  pub discriminant: String,
152  pub discriminant_case: Option<Case<'static>>,
153}
154
155#[derive(Debug, Clone)]
156pub struct NapiStructuredEnumVariant {
157  pub name: Ident,
158  pub fields: Vec<NapiStructField>,
159  pub is_tuple: bool,
160}
161
162#[derive(Debug, Clone)]
163pub struct NapiStructField {
164  pub name: syn::Member,
165  pub js_name: String,
166  pub ty: syn::Type,
167  pub getter: bool,
168  pub setter: bool,
169  pub writable: bool,
170  pub enumerable: bool,
171  pub configurable: bool,
172  pub comments: Vec<String>,
173  pub skip_typescript: bool,
174  pub ts_type: Option<String>,
175  pub has_lifetime: bool,
176}
177
178#[derive(Debug, Clone)]
179pub struct NapiImpl {
180  pub name: Ident,
181  pub js_name: String,
182  pub has_lifetime: bool,
183  pub items: Vec<NapiFn>,
184  pub task_output_type: Option<Type>,
185  pub iterator_yield_type: Option<Type>,
186  pub iterator_next_type: Option<Type>,
187  pub iterator_return_type: Option<Type>,
188  pub async_iterator_yield_type: Option<Type>,
189  pub async_iterator_next_type: Option<Type>,
190  pub async_iterator_return_type: Option<Type>,
191  pub js_mod: Option<String>,
192  pub comments: Vec<String>,
193  pub register_name: Ident,
194}
195
196#[derive(Debug, Clone)]
197pub struct NapiEnum {
198  pub name: Ident,
199  pub js_name: String,
200  pub variants: Vec<NapiEnumVariant>,
201  pub js_mod: Option<String>,
202  pub comments: Vec<String>,
203  pub skip_typescript: bool,
204  pub register_name: Ident,
205  pub is_string_enum: bool,
206  pub object_from_js: bool,
207  pub object_to_js: bool,
208}
209
210#[derive(Debug, Clone)]
211pub enum NapiEnumValue {
212  String(String),
213  Number(i32),
214}
215
216impl From<&NapiEnumValue> for Literal {
217  fn from(val: &NapiEnumValue) -> Self {
218    match val {
219      NapiEnumValue::String(string) => Literal::string(string),
220      NapiEnumValue::Number(number) => Literal::i32_unsuffixed(number.to_owned()),
221    }
222  }
223}
224
225#[derive(Debug, Clone)]
226pub struct NapiEnumVariant {
227  pub name: Ident,
228  pub val: NapiEnumValue,
229  pub comments: Vec<String>,
230}
231
232#[derive(Debug, Clone)]
233pub struct NapiConst {
234  pub name: Ident,
235  pub js_name: String,
236  pub type_name: Type,
237  pub value: Expr,
238  pub js_mod: Option<String>,
239  pub comments: Vec<String>,
240  pub skip_typescript: bool,
241  pub register_name: Ident,
242}
243
244#[derive(Debug, Clone)]
245pub struct NapiMod {
246  pub name: Ident,
247  pub js_name: String,
248}
249
250#[derive(Debug, Clone)]
251pub struct NapiType {
252  pub name: Ident,
253  pub js_name: String,
254  pub value: Type,
255  pub register_name: Ident,
256  pub skip_typescript: bool,
257  pub js_mod: Option<String>,
258  pub comments: Vec<String>,
259}