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