1#[macro_use]
2extern crate quote;
3
4use std::collections::HashMap;
5
6use once_cell::sync::Lazy;
7use proc_macro2::TokenStream;
8
9#[macro_use]
10pub mod error;
11pub mod ast;
12pub mod codegen;
13#[cfg(feature = "type-def")]
14pub mod typegen;
15
16pub use ast::*;
17pub use codegen::*;
18pub use error::{BindgenResult, Diagnostic};
19#[cfg(feature = "type-def")]
20pub use semver;
21#[cfg(feature = "type-def")]
22pub use typegen::*;
23
24#[derive(Debug)]
25pub struct Napi {
26 pub item: NapiItem,
27}
28
29macro_rules! napi_ast_impl {
30 ( $( ($v:ident, $ast:ident), )* ) => {
31 #[derive(Debug)]
32 #[allow(clippy::large_enum_variant)]
33 pub enum NapiItem {
34 $($v($ast)),*
35 }
36
37 impl TryToTokens for Napi {
38 fn try_to_tokens(&self, tokens: &mut TokenStream) -> BindgenResult<()> {
39 match self.item {
40 $( NapiItem::$v(ref ast) => ast.try_to_tokens(tokens) ),*
41 }
42 }
43 }
44
45 #[cfg(feature = "type-def")]
46 impl ToTypeDef for Napi {
47 fn to_type_def(&self) -> Option<TypeDef> {
48 match self.item {
49 $( NapiItem::$v(ref ast) => ast.to_type_def() ),*
50 }
51 }
52 }
53
54 impl Napi {
55 pub fn register_name(&self) -> String {
56 match self.item {
57 $( NapiItem::$v(ref ast) => ast.register_name.to_string() ),*
58 }
59 }
60 }
61 };
62}
63
64napi_ast_impl! {
65 (Fn, NapiFn),
66 (Struct, NapiStruct),
67 (Impl, NapiImpl),
68 (Enum, NapiEnum),
69 (Const, NapiConst),
70}
71
72pub(crate) static PRIMITIVE_TYPES: &[(&str, (&str, bool, bool))] = &[
73 ("JsUndefined", ("undefined", false, false)),
74 ("()", ("undefined", false, false)),
75 ("Undefined", ("undefined", false, false)),
76 ("JsNumber", ("number", false, false)),
77 ("i8", ("number", false, false)),
78 ("i16", ("number", false, false)),
79 ("i32", ("number", false, false)),
80 ("i64", ("number", false, false)),
81 ("f32", ("number", false, false)),
82 ("f64", ("number", false, false)),
83 ("u8", ("number", false, false)),
84 ("u16", ("number", false, false)),
85 ("u32", ("number", false, false)),
86 // serde `Number`
87 ("Number", ("number", false, false)),
88 ("u64", ("bigint", false, false)),
89 ("i64n", ("bigint", false, false)),
90 ("u128", ("bigint", false, false)),
91 ("i128", ("bigint", false, false)),
92 ("usize", ("bigint", false, false)),
93 ("isize", ("bigint", false, false)),
94 ("JsBigInt", ("bigint", false, false)),
95 ("BigInt", ("bigint", false, false)),
96 ("JsBoolean", ("boolean", false, false)),
97 ("bool", ("boolean", false, false)),
98 ("JsString", ("string", false, false)),
99 ("String", ("string", false, false)),
100 ("str", ("string", false, false)),
101 ("Latin1String", ("string", false, false)),
102 ("Utf16String", ("string", false, false)),
103 ("char", ("string", false, false)),
104 ("Null", ("null", false, false)),
105 ("JsNull", ("null", false, false)),
106 ("null", ("null", false, false)),
107 ("Symbol", ("symbol", false, false)),
108 ("JsSymbol", ("symbol", false, false)),
109 ("JsFunction", ("(...args: any[]) => any", true, false)),
110];
111
112pub(crate) static TYPEDARRAY_SLICE_TYPES: Lazy<HashMap<&str, &str>> = Lazy::new(|| {
113 HashMap::from([
114 ("u8", "Uint8Array"),
115 ("i8", "Int8Array"),
116 ("u16", "Uint16Array"),
117 ("i16", "Int16Array"),
118 ("u32", "Uint32Array"),
119 ("i32", "Int32Array"),
120 ("f32", "Float32Array"),
121 ("f64", "Float64Array"),
122 ("u64", "BigUint64Array"),
123 ("i64", "BigInt64Array"),
124 ])
125});
126