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 fn_self: Option<FnSelf>,
14  pub kind: FnKind,
15  pub vis: syn::Visibility,
16  pub parent: Option<Ident>,
17  pub strict: bool,
18  pub return_if_invalid: bool,
19  pub js_mod: Option<String>,
20  pub ts_generic_types: Option<String>,
21  pub ts_args_type: Option<String>,
22  pub ts_return_type: Option<String>,
23  pub skip_typescript: bool,
24  pub comments: Vec<String>,
25  pub parent_is_generator: bool,
26  pub writable: bool,
27  pub enumerable: bool,
28  pub configurable: bool,
29  pub catch_unwind: bool,
30  pub unsafe_: bool,
31  pub register_name: Ident,
32}
33
34#[derive(Debug, Clone)]
35pub struct CallbackArg {
36  pub pat: Box<syn::Pat>,
37  pub args: Vec<syn::Type>,
38  pub ret: Option<syn::Type>,
39}
40
41#[derive(Debug, Clone)]
42pub struct NapiFnArg {
43  pub kind: NapiFnArgKind,
44  pub ts_arg_type: Option<String>,
45}
46
47impl NapiFnArg {
48  /// if type was overridden with `#[napi(ts_arg_type = "...")]` use that instead
49  pub fn use_overridden_type_or(&self, default: impl FnOnce() -> String) -> String {
50    self.ts_arg_type.as_ref().cloned().unwrap_or_else(default)
51  }
52}
53
54#[derive(Debug, Clone)]
55pub enum NapiFnArgKind {
56  PatType(Box<syn::PatType>),
57  Callback(Box<CallbackArg>),
58}
59
60#[derive(Debug, Clone, PartialEq, Eq)]
61pub enum FnKind {
62  Normal,
63  Constructor,
64  Factory,
65  Getter,
66  Setter,
67}
68
69#[derive(Debug, Clone, PartialEq, Eq)]
70pub enum FnSelf {
71  Value,
72  Ref,
73  MutRef,
74}
75
76#[derive(Debug, Clone)]
77pub struct NapiStruct {
78  pub name: Ident,
79  pub js_name: String,
80  pub vis: syn::Visibility,
81  pub fields: Vec<NapiStructField>,
82  pub is_tuple: bool,
83  pub kind: NapiStructKind,
84  pub object_from_js: bool,
85  pub object_to_js: bool,
86  pub js_mod: Option<String>,
87  pub comments: Vec<String>,
88  pub implement_iterator: bool,
89  pub use_custom_finalize: bool,
90  pub register_name: Ident,
91  pub use_nullable: bool,
92}
93
94#[derive(Debug, Clone, PartialEq, Eq)]
95pub enum NapiStructKind {
96  None,
97  Constructor,
98  Object,
99}
100
101#[derive(Debug, Clone)]
102pub struct NapiStructField {
103  pub name: syn::Member,
104  pub js_name: String,
105  pub ty: syn::Type,
106  pub getter: bool,
107  pub setter: bool,
108  pub writable: bool,
109  pub enumerable: bool,
110  pub configurable: bool,
111  pub comments: Vec<String>,
112  pub skip_typescript: bool,
113  pub ts_type: Option<String>,
114}
115
116#[derive(Debug, Clone)]
117pub struct NapiImpl {
118  pub name: Ident,
119  pub js_name: String,
120  pub items: Vec<NapiFn>,
121  pub task_output_type: Option<Type>,
122  pub iterator_yield_type: Option<Type>,
123  pub iterator_next_type: Option<Type>,
124  pub iterator_return_type: Option<Type>,
125  pub js_mod: Option<String>,
126  pub comments: Vec<String>,
127  pub register_name: Ident,
128}
129
130#[derive(Debug, Clone)]
131pub struct NapiEnum {
132  pub name: Ident,
133  pub js_name: String,
134  pub variants: Vec<NapiEnumVariant>,
135  pub js_mod: Option<String>,
136  pub comments: Vec<String>,
137  pub skip_typescript: bool,
138  pub register_name: Ident,
139}
140
141#[derive(Debug, Clone)]
142pub enum NapiEnumValue {
143  String(String),
144  Number(i32),
145}
146
147impl From<&NapiEnumValue> for Literal {
148  fn from(val: &NapiEnumValue) -> Self {
149    match val {
150      NapiEnumValue::String(string) => Literal::string(string),
151      NapiEnumValue::Number(number) => Literal::i32_unsuffixed(number.to_owned()),
152    }
153  }
154}
155
156#[derive(Debug, Clone)]
157pub struct NapiEnumVariant {
158  pub name: Ident,
159  pub val: NapiEnumValue,
160  pub comments: Vec<String>,
161}
162
163#[derive(Debug, Clone)]
164pub struct NapiConst {
165  pub name: Ident,
166  pub js_name: String,
167  pub type_name: Type,
168  pub value: Expr,
169  pub js_mod: Option<String>,
170  pub comments: Vec<String>,
171  pub skip_typescript: bool,
172  pub register_name: Ident,
173}
174
175#[derive(Debug, Clone)]
176pub struct NapiMod {
177  pub name: Ident,
178  pub js_name: String,
179}