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 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}
96
97#[derive(Debug, Clone)]
98pub enum NapiStructKind {
99 Transparent(NapiTransparent),
100 Class(NapiClass),
101 Object(NapiObject),
102 StructuredEnum(NapiStructuredEnum),
103 Array(NapiArray),
104}
105
106#[derive(Debug, Clone)]
107pub struct NapiTransparent {
108 pub ty: Type,
109 pub object_from_js: bool,
110 pub object_to_js: bool,
111}
112
113#[derive(Debug, Clone)]
114pub struct NapiClass {
115 pub fields: Vec<NapiStructField>,
116 pub ctor: bool,
117 pub implement_iterator: bool,
118 pub implement_async_iterator: bool,
119 pub is_tuple: bool,
120 pub use_custom_finalize: bool,
121}
122
123#[derive(Debug, Clone)]
124pub struct NapiObject {
125 pub fields: Vec<NapiStructField>,
126 pub object_from_js: bool,
127 pub object_to_js: bool,
128 pub is_tuple: bool,
129}
130
131#[derive(Debug, Clone)]
132pub struct NapiArray {
133 pub fields: Vec<NapiStructField>,
134 pub object_from_js: bool,
135 pub object_to_js: bool,
136}
137
138#[derive(Debug, Clone)]
139pub struct NapiStructuredEnum {
140 pub variants: Vec<NapiStructuredEnumVariant>,
141 pub object_from_js: bool,
142 pub object_to_js: bool,
143 pub discriminant: String,
144 pub discriminant_case: Option<Case<'static>>,
145}
146
147#[derive(Debug, Clone)]
148pub struct NapiStructuredEnumVariant {
149 pub name: Ident,
150 pub fields: Vec<NapiStructField>,
151 pub is_tuple: bool,
152}
153
154#[derive(Debug, Clone)]
155pub struct NapiStructField {
156 pub name: syn::Member,
157 pub js_name: String,
158 pub ty: syn::Type,
159 pub getter: bool,
160 pub setter: bool,
161 pub writable: bool,
162 pub enumerable: bool,
163 pub configurable: bool,
164 pub comments: Vec<String>,
165 pub skip_typescript: bool,
166 pub ts_type: Option<String>,
167 pub has_lifetime: bool,
168}
169
170#[derive(Debug, Clone)]
171pub struct NapiImpl {
172 pub name: Ident,
173 pub js_name: String,
174 pub has_lifetime: bool,
175 pub items: Vec<NapiFn>,
176 pub task_output_type: Option<Type>,
177 pub iterator_yield_type: Option<Type>,
178 pub iterator_next_type: Option<Type>,
179 pub iterator_return_type: Option<Type>,
180 pub async_iterator_yield_type: Option<Type>,
181 pub async_iterator_next_type: Option<Type>,
182 pub async_iterator_return_type: Option<Type>,
183 pub js_mod: Option<String>,
184 pub comments: Vec<String>,
185 pub register_name: Ident,
186}
187
188#[derive(Debug, Clone)]
189pub struct NapiEnum {
190 pub name: Ident,
191 pub js_name: String,
192 pub variants: Vec<NapiEnumVariant>,
193 pub js_mod: Option<String>,
194 pub comments: Vec<String>,
195 pub skip_typescript: bool,
196 pub register_name: Ident,
197 pub is_string_enum: bool,
198 pub object_from_js: bool,
199 pub object_to_js: bool,
200}
201
202#[derive(Debug, Clone)]
203pub enum NapiEnumValue {
204 String(String),
205 Number(i32),
206}
207
208impl From<&NapiEnumValue> for Literal {
209 fn from(val: &NapiEnumValue) -> Self {
210 match val {
211 NapiEnumValue::String(string) => Literal::string(string),
212 NapiEnumValue::Number(number) => Literal::i32_unsuffixed(number.to_owned()),
213 }
214 }
215}
216
217#[derive(Debug, Clone)]
218pub struct NapiEnumVariant {
219 pub name: Ident,
220 pub val: NapiEnumValue,
221 pub comments: Vec<String>,
222}
223
224#[derive(Debug, Clone)]
225pub struct NapiConst {
226 pub name: Ident,
227 pub js_name: String,
228 pub type_name: Type,
229 pub value: Expr,
230 pub js_mod: Option<String>,
231 pub comments: Vec<String>,
232 pub skip_typescript: bool,
233 pub register_name: Ident,
234}
235
236#[derive(Debug, Clone)]
237pub struct NapiMod {
238 pub name: Ident,
239 pub js_name: String,
240}
241
242#[derive(Debug, Clone)]
243pub struct NapiType {
244 pub name: Ident,
245 pub js_name: String,
246 pub value: Type,
247 pub register_name: Ident,
248 pub skip_typescript: bool,
249 pub js_mod: Option<String>,
250 pub comments: Vec<String>,
251}