feat (macro-rs): add macro_rs::export macro
Co-authored-by: naskya <m@naskya.net>
This commit is contained in:
parent
08bea415c9
commit
3e32fc7b04
6 changed files with 27 additions and 13 deletions
|
@ -1,4 +1,4 @@
|
|||
pub use macro_rs::napi as export;
|
||||
pub use macro_rs::export;
|
||||
|
||||
pub mod database;
|
||||
pub mod misc;
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
#[derive(Debug, PartialEq)]
|
||||
#[cfg_attr(feature = "napi", crate::export(object, use_nullable = true))]
|
||||
#[crate::export(object)]
|
||||
pub struct Acct {
|
||||
pub username: String,
|
||||
pub host: Option<String>,
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "napi", crate::export)]
|
||||
#[crate::export]
|
||||
pub fn string_to_acct(acct: &str) -> Acct {
|
||||
let split: Vec<&str> = if let Some(stripped) = acct.strip_prefix('@') {
|
||||
stripped
|
||||
|
@ -25,7 +25,7 @@ pub fn string_to_acct(acct: &str) -> Acct {
|
|||
}
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "napi", crate::export)]
|
||||
#[crate::export]
|
||||
pub fn acct_to_string(acct: &Acct) -> String {
|
||||
match &acct.host {
|
||||
Some(host) => format!("{}@{}", acct.username, host),
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
use once_cell::sync::Lazy;
|
||||
use regex::{Captures, Regex};
|
||||
|
||||
#[cfg_attr(feature = "napi", crate::export)]
|
||||
#[crate::export]
|
||||
pub fn nyaify(text: &str, lang: Option<&str>) -> String {
|
||||
let mut to_return = text.to_owned();
|
||||
|
||||
|
|
|
@ -17,7 +17,7 @@ const TIME_2000: i64 = 946_684_800_000;
|
|||
const TIMESTAMP_LENGTH: u16 = 8;
|
||||
|
||||
/// Initializes Cuid2 generator. Must be called before any [create_id].
|
||||
#[cfg_attr(feature = "napi", crate::export)]
|
||||
#[crate::export]
|
||||
pub fn init_id_generator(length: u16, fingerprint: &str) {
|
||||
FINGERPRINT.get_or_init(move || format!("{}{}", fingerprint, cuid2::create_id()));
|
||||
GENERATOR.get_or_init(move || {
|
||||
|
@ -44,7 +44,7 @@ pub fn create_id(datetime: &NaiveDateTime) -> Result<String, ErrorUninitialized>
|
|||
}
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "napi", crate::export)]
|
||||
#[crate::export]
|
||||
pub fn get_timestamp(id: &str) -> i64 {
|
||||
let n: Option<u64> = BASE36.decode_var_len(&id[0..8]);
|
||||
match n {
|
||||
|
|
|
@ -9,7 +9,7 @@ pub fn gen_string(length: u16) -> String {
|
|||
.collect()
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "napi", crate::export(js_name = "secureRndstr"))]
|
||||
#[crate::export(js_name = "secureRndstr")]
|
||||
pub fn native_random_str(length: Option<u16>) -> String {
|
||||
gen_string(length.unwrap_or(32))
|
||||
}
|
||||
|
|
|
@ -2,14 +2,18 @@ use convert_case::{Case, Casing};
|
|||
use proc_macro2::{TokenStream, TokenTree};
|
||||
use quote::{quote, ToTokens};
|
||||
|
||||
// FIXME
|
||||
/// For doctest only
|
||||
#[proc_macro_attribute]
|
||||
pub fn dummy_macro(
|
||||
_attr: proc_macro::TokenStream,
|
||||
pub fn export(
|
||||
attr: proc_macro::TokenStream,
|
||||
item: proc_macro::TokenStream,
|
||||
) -> proc_macro::TokenStream {
|
||||
item
|
||||
let attr: TokenStream = attr.into();
|
||||
let item: TokenStream = item.into();
|
||||
|
||||
quote! {
|
||||
#[cfg_attr(feature = "napi", macro_rs::napi(#attr))]
|
||||
#item
|
||||
}.into()
|
||||
}
|
||||
|
||||
/// Creates extra wrapper function for napi.
|
||||
|
@ -343,6 +347,16 @@ fn napi_impl(macro_attr: TokenStream, item: TokenStream) -> TokenStream {
|
|||
}
|
||||
}
|
||||
|
||||
// FIXME
|
||||
/// For doctest only
|
||||
#[proc_macro_attribute]
|
||||
pub fn dummy_macro(
|
||||
_attr: proc_macro::TokenStream,
|
||||
item: proc_macro::TokenStream,
|
||||
) -> proc_macro::TokenStream {
|
||||
item
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use proc_macro2::TokenStream;
|
||||
|
|
Loading…
Reference in a new issue