chore (macro-rs): ts_only_warn -> ts_export

This commit is contained in:
naskya 2024-05-21 16:12:11 +09:00
parent 4bb6f6e0e3
commit 1f082bfb56
No known key found for this signature in database
GPG key ID: 712D413B3A9FED5C
5 changed files with 13 additions and 73 deletions

View file

@ -48,14 +48,12 @@ impl From<Acct> for String {
} }
} }
#[crate::ts_only_warn("Use `acct.parse().unwrap()` or `Acct::from_str(acct).unwrap()` instead.")] #[crate::ts_export]
#[crate::export]
pub fn string_to_acct(acct: &str) -> Acct { pub fn string_to_acct(acct: &str) -> Acct {
Acct::from_str(acct).unwrap() Acct::from_str(acct).unwrap()
} }
#[crate::ts_only_warn("Use `acct.to_string()` instead.")] #[crate::ts_export]
#[crate::export]
pub fn acct_to_string(acct: &Acct) -> String { pub fn acct_to_string(acct: &Acct) -> String {
acct.to_string() acct.to_string()
} }

View file

@ -1,4 +1,4 @@
pub use macro_rs::{export, ts_only_warn}; pub use macro_rs::{export, ts_export};
pub mod config; pub mod config;
pub mod database; pub mod database;

View file

@ -1,33 +1,4 @@
#[crate::ts_only_warn("Use `emojis::get(str).is_some()` instead.")] #[crate::ts_export]
#[crate::export]
pub fn is_unicode_emoji(s: &str) -> bool { pub fn is_unicode_emoji(s: &str) -> bool {
emojis::get(s).is_some() emojis::get(s).is_some()
} }
#[cfg(test)]
mod unit_test {
#[allow(deprecated)]
use super::is_unicode_emoji;
#[test]
#[allow(deprecated)]
fn test_unicode_emoji_check() {
assert!(is_unicode_emoji(""));
assert!(is_unicode_emoji("👍"));
assert!(is_unicode_emoji(""));
assert!(is_unicode_emoji("♥️"));
assert!(is_unicode_emoji("❤️"));
assert!(is_unicode_emoji("💙"));
assert!(is_unicode_emoji("🩷"));
assert!(is_unicode_emoji("🖖🏿"));
assert!(is_unicode_emoji("🏃‍➡️"));
assert!(is_unicode_emoji("👩‍❤️‍👨"));
assert!(is_unicode_emoji("👩‍👦‍👦"));
assert!(is_unicode_emoji("🏳️‍🌈"));
assert!(!is_unicode_emoji("⭐⭐"));
assert!(!is_unicode_emoji("x"));
assert!(!is_unicode_emoji("\t"));
assert!(!is_unicode_emoji(":meow_aww:"));
}
}

View file

@ -130,12 +130,12 @@ pub async fn nodeinfo_2_0() -> Result<Nodeinfo20, Error> {
Ok(nodeinfo_2_1().await?.into()) Ok(nodeinfo_2_1().await?.into())
} }
#[crate::export(js_name = "nodeinfo_2_1")] #[crate::ts_export(js_name = "nodeinfo_2_1")]
pub async fn nodeinfo_2_1_as_json() -> Result<serde_json::Value, Error> { pub async fn nodeinfo_2_1_as_json() -> Result<serde_json::Value, Error> {
Ok(serde_json::to_value(nodeinfo_2_1().await?)?) Ok(serde_json::to_value(nodeinfo_2_1().await?)?)
} }
#[crate::export(js_name = "nodeinfo_2_0")] #[crate::ts_export(js_name = "nodeinfo_2_0")]
pub async fn nodeinfo_2_0_as_json() -> Result<serde_json::Value, Error> { pub async fn nodeinfo_2_0_as_json() -> Result<serde_json::Value, Error> {
Ok(serde_json::to_value(nodeinfo_2_0().await?)?) Ok(serde_json::to_value(nodeinfo_2_0().await?)?)
} }

View file

@ -33,49 +33,20 @@ pub fn export(
.into() .into()
} }
/// Denotes that this function should only be used in TypeScript. /// Export this function, struct, enum, const, etc. to TypeScript
/// /// and make it unable to use in Rust
/// # Example
/// ```
/// # use macro_rs::ts_only_warn;
/// # use std::fmt::{Display, Formatter, Result};
/// # pub struct Thing {}
/// # impl Display for Thing { fn fmt(&self, fmt: &mut Formatter) -> Result { Ok(()) } } // dummy
/// #[ts_only_warn("Use `thing.to_string()` instead.")]
/// pub fn thing_to_string(thing: Thing) -> String {
/// thing.to_string()
/// }
/// ```
/// generates
/// ```
/// # use macro_rs::ts_only_warn;
/// # use std::fmt::{Display, Formatter, Result};
/// # pub struct Thing {}
/// # impl Display for Thing { fn fmt(&self, fmt: &mut Formatter) -> Result { Ok(()) } } // dummy
/// #[cfg_attr(not(feature = "napi"), deprecated = "This function is only for TypeScript export. Use `thing.to_string()` instead.")]
/// pub fn thing_to_string(thing: Thing) -> String {
/// thing.to_string()
/// }
/// ```
#[proc_macro_attribute] #[proc_macro_attribute]
pub fn ts_only_warn( pub fn ts_export(
attr: proc_macro::TokenStream, attr: proc_macro::TokenStream,
item: proc_macro::TokenStream, item: proc_macro::TokenStream,
) -> proc_macro::TokenStream { ) -> proc_macro::TokenStream {
let attr: TokenStream = attr.into();
let item: TokenStream = item.into(); let item: TokenStream = item.into();
let attr_str = Into::<TokenStream>::into(attr).to_string();
let msg = {
let mut chars = attr_str.as_str().chars();
chars.next();
chars.next_back();
chars.as_str()
};
let prefixed_msg = format!("This function is only for TypeScript export. {}", msg);
quote! { quote! {
#[cfg_attr(not(feature = "napi"), deprecated = #prefixed_msg)] #[cfg(feature = "napi")]
#item #[macro_rs::napi(#attr)]
#item
} }
.into() .into()
} }