napi_derive_backend/
ast.rs

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