fix (macro-rs): use cfg
instead of cfg_attr
This commit is contained in:
parent
8b6e300d4e
commit
5d2238a266
1 changed files with 65 additions and 66 deletions
|
@ -13,40 +13,36 @@ use quote::{quote, ToTokens};
|
||||||
/// ## Example with `i32` argument
|
/// ## Example with `i32` argument
|
||||||
/// ```rust
|
/// ```rust
|
||||||
/// #[macro_rs::napi]
|
/// #[macro_rs::napi]
|
||||||
/// fn add_one(x: i32) -> i32 {
|
/// pub fn add_one(x: i32) -> i32 {
|
||||||
/// x + 1
|
/// x + 1
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
/// becomes
|
/// generates
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```rust
|
||||||
/// fn add_one(x: i32) -> i32 {
|
/// #[cfg(feature = "napi")]
|
||||||
/// x + 1
|
/// #[napi_derive::napi(js_name = "addOne")]
|
||||||
/// }
|
/// pub fn add_one_napi(x: i32) -> i32 {
|
||||||
/// #[cfg_attr(feature = "napi", napi_derive::napi(js_name = "addOne"))]
|
/// add_one(x)
|
||||||
/// fn add_one_napi(x: i32) -> i32 {
|
|
||||||
/// add_one(x)
|
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
/// ## Example with `&str` argument
|
/// ## Example with `&str` argument
|
||||||
/// ```rust
|
/// ```rust
|
||||||
/// #[macro_rs::napi]
|
/// #[macro_rs::napi]
|
||||||
/// fn concatenate_string(str1: &str, str2: &str) -> String {
|
/// pub fn concatenate_string(str1: &str, str2: &str) -> String {
|
||||||
/// str1.to_owned() + str2
|
/// str1.to_owned() + str2
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
/// becomes
|
/// generates
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```rust
|
||||||
/// fn concatenate_string(str1: &str, str2: &str) -> String {
|
/// #[cfg(feature = "napi")]
|
||||||
/// str1.to_owned() + str2
|
/// #[napi_derive::napi(js_name = "concatenateString")]
|
||||||
/// }
|
/// pub fn concatenate_string_napi(str1: String, str2: String) -> String {
|
||||||
/// #[cfg_attr(feature = "napi", napi_derive::napi(js_name = "concatenateString"))]
|
/// concatenate_string(&str1, &str2)
|
||||||
/// fn concatenate_string_napi(str1: String, str2: String) -> String {
|
|
||||||
/// concatenate_string(&str1, &str2)
|
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
|
@ -142,7 +138,8 @@ fn napi_impl(attr: TokenStream, item: TokenStream) -> TokenStream {
|
||||||
quote! {
|
quote! {
|
||||||
#item_fn
|
#item_fn
|
||||||
|
|
||||||
#[cfg_attr(feature = "napi", napi_derive::napi(js_name = #js_name))]
|
#[cfg(feature = "napi")]
|
||||||
|
#[napi_derive::napi(js_name = #js_name)]
|
||||||
#(#item_fn_attrs)*
|
#(#item_fn_attrs)*
|
||||||
#item_fn_vis #item_fn_sig {
|
#item_fn_vis #item_fn_sig {
|
||||||
#ident(#(#called_args),*)
|
#ident(#(#called_args),*)
|
||||||
|
@ -155,71 +152,73 @@ mod tests {
|
||||||
use proc_macro2::TokenStream;
|
use proc_macro2::TokenStream;
|
||||||
use quote::quote;
|
use quote::quote;
|
||||||
|
|
||||||
|
macro_rules! test_macro {
|
||||||
|
($source:expr, $generated:expr) => {
|
||||||
|
assert_eq!(
|
||||||
|
super::napi_impl(TokenStream::new(), $source).to_string(),
|
||||||
|
format!("{} {}", $source, $generated),
|
||||||
|
)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn primitive_argument() {
|
fn primitive_argument() {
|
||||||
let generated = super::napi_impl(
|
test_macro!(
|
||||||
TokenStream::new(),
|
|
||||||
quote! {
|
quote! {
|
||||||
fn add_one(x: i32) -> i32 {
|
pub fn add_one(x: i32) -> i32 {
|
||||||
x + 1
|
x + 1
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
quote! {
|
||||||
|
#[cfg(feature = "napi")]
|
||||||
|
#[napi_derive::napi(js_name = "addOne")]
|
||||||
|
pub fn add_one_napi(x: i32) -> i32 {
|
||||||
|
add_one(x)
|
||||||
|
}
|
||||||
|
}
|
||||||
);
|
);
|
||||||
let expected = quote! {
|
|
||||||
fn add_one(x: i32) -> i32 {
|
|
||||||
x + 1
|
|
||||||
}
|
|
||||||
#[cfg_attr(feature = "napi", napi_derive::napi(js_name = "addOne"))]
|
|
||||||
fn add_one_napi(x: i32) -> i32 {
|
|
||||||
add_one(x)
|
|
||||||
}
|
|
||||||
};
|
|
||||||
assert_eq!(generated.to_string(), expected.to_string());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn str_ref_argument() {
|
fn str_ref_argument() {
|
||||||
let generated = super::napi_impl(
|
test_macro!(
|
||||||
TokenStream::new(),
|
|
||||||
quote! {
|
quote! {
|
||||||
fn concatenate_string(str1: &str, str2: &str) -> String {
|
pub fn concatenate_string(str1: &str, str2: &str) -> String {
|
||||||
str1.to_owned() + str2
|
str1.to_owned() + str2
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
quote! {
|
||||||
|
#[cfg(feature = "napi")]
|
||||||
|
#[napi_derive::napi(js_name = "concatenateString")]
|
||||||
|
pub fn concatenate_string_napi(str1: String, str2: String) -> String {
|
||||||
|
concatenate_string(&str1, &str2)
|
||||||
|
}
|
||||||
|
}
|
||||||
);
|
);
|
||||||
let expected = quote! {
|
|
||||||
fn concatenate_string(str1: &str, str2: &str) -> String {
|
|
||||||
str1.to_owned() + str2
|
|
||||||
}
|
|
||||||
#[cfg_attr(feature = "napi", napi_derive::napi(js_name = "concatenateString"))]
|
|
||||||
fn concatenate_string_napi(str1: String, str2: String) -> String {
|
|
||||||
concatenate_string(&str1, &str2)
|
|
||||||
}
|
|
||||||
};
|
|
||||||
assert_eq!(generated.to_string(), expected.to_string());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn mut_ref_argument() {
|
fn mut_ref_argument() {
|
||||||
let generated = super::napi_impl(
|
test_macro!(
|
||||||
TokenStream::new(),
|
|
||||||
quote! {
|
quote! {
|
||||||
fn append_string_and_clone(base_str: &mut String, appended_str: &str) -> String {
|
pub fn append_string_and_clone(
|
||||||
base_str.push_str(appended_str);
|
base_str: &mut String,
|
||||||
base_str.to_owned()
|
appended_str: &str,
|
||||||
}
|
) -> String {
|
||||||
|
base_str.push_str(appended_str);
|
||||||
|
base_str.to_owned()
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
quote! {
|
||||||
|
#[cfg(feature = "napi")]
|
||||||
|
#[napi_derive::napi(js_name = "appendStringAndClone")]
|
||||||
|
pub fn append_string_and_clone_napi(
|
||||||
|
mut base_str: String,
|
||||||
|
appended_str: String,
|
||||||
|
) -> String {
|
||||||
|
append_string_and_clone(&mut base_str, &appended_str)
|
||||||
|
}
|
||||||
|
}
|
||||||
);
|
);
|
||||||
let expected = quote! {
|
|
||||||
fn append_string_and_clone(base_str: &mut String, appended_str: &str) -> String {
|
|
||||||
base_str.push_str(appended_str);
|
|
||||||
base_str.to_owned()
|
|
||||||
}
|
|
||||||
#[cfg_attr(feature = "napi", napi_derive::napi(js_name = "appendStringAndClone"))]
|
|
||||||
fn append_string_and_clone_napi(mut base_str: String, appended_str: String) -> String {
|
|
||||||
append_string_and_clone(&mut base_str, &appended_str)
|
|
||||||
}
|
|
||||||
};
|
|
||||||
assert_eq!(generated.to_string(), expected.to_string());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue