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