napi-rs/crates/backend/src/lib.rs
forehalo 2467b7139b
Introduce #[napi] procedural macro to automation development boilerplate (#696)
* napi procedural macro for basic rust/JavaScript types
* introduce the `compat-mode` for `napi` and `napi-derive` crates for backward compatible
* remove #[inline] and let compiler to decide the inline behavior
* cli now can produce the `.d.ts` file for native binding
* many tests and example for the new procedural macro

Co-authored-by: LongYinan <lynweklm@gmail.com>
2021-09-23 01:29:09 +08:00

56 lines
1 KiB
Rust

#[macro_use]
extern crate quote;
use proc_macro2::TokenStream;
#[macro_use]
pub mod error;
pub mod ast;
pub mod codegen;
#[cfg(feature = "type-def")]
pub mod typegen;
pub use ast::*;
pub use codegen::*;
pub use error::{BindgenResult, Diagnostic};
#[cfg(feature = "type-def")]
pub use typegen::*;
#[derive(Debug)]
pub struct Napi {
pub comments: Vec<String>,
pub item: NapiItem,
}
macro_rules! napi_ast_impl {
( $( ($v:ident, $ast:ident), )* ) => {
#[derive(Debug)]
pub enum NapiItem {
$($v($ast)),*
}
impl TryToTokens for Napi {
fn try_to_tokens(&self, tokens: &mut TokenStream) -> BindgenResult<()> {
match self.item {
$( NapiItem::$v(ref ast) => ast.try_to_tokens(tokens) ),*
}
}
}
#[cfg(feature = "type-def")]
impl ToTypeDef for Napi {
fn to_type_def(&self) -> TypeDef {
match self.item {
$( NapiItem::$v(ref ast) => ast.to_type_def() ),*
}
}
}
};
}
napi_ast_impl! {
(Fn, NapiFn),
(Struct, NapiStruct),
(Impl, NapiImpl),
(Enum, NapiEnum),
}