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