Make all identifier macros usable from ruma-identifiers, even indirectly
This commit is contained in:
parent
0d0480e523
commit
2023616319
@ -5,15 +5,30 @@ use ruma_identifiers_validation::{
|
||||
device_key_id, event_id, room_alias_id, room_id, room_version_id, server_key_id, server_name,
|
||||
user_id,
|
||||
};
|
||||
use syn::{parse_macro_input, LitStr};
|
||||
use syn::{parse::Parse, parse_macro_input, LitStr, Path, Token};
|
||||
|
||||
struct Input {
|
||||
dollar_crate: Path,
|
||||
id: LitStr,
|
||||
}
|
||||
|
||||
impl Parse for Input {
|
||||
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
|
||||
let dollar_crate = input.parse()?;
|
||||
input.parse::<Token![,]>()?;
|
||||
let id = input.parse()?;
|
||||
|
||||
Ok(Self { dollar_crate, id })
|
||||
}
|
||||
}
|
||||
|
||||
#[proc_macro]
|
||||
pub fn device_key_id(input: TokenStream) -> TokenStream {
|
||||
let id = parse_macro_input!(input as LitStr);
|
||||
let Input { dollar_crate, id } = parse_macro_input!(input as Input);
|
||||
assert!(device_key_id::validate(&id.value()).is_ok(), "Invalid device key id");
|
||||
|
||||
let output = quote! {
|
||||
<::ruma::identifiers::DeviceKeyId as ::std::convert::TryFrom<&str>>::try_from(#id).unwrap()
|
||||
<#dollar_crate::DeviceKeyId as ::std::convert::TryFrom<&str>>::try_from(#id).unwrap()
|
||||
};
|
||||
|
||||
output.into()
|
||||
@ -21,11 +36,11 @@ pub fn device_key_id(input: TokenStream) -> TokenStream {
|
||||
|
||||
#[proc_macro]
|
||||
pub fn event_id(input: TokenStream) -> TokenStream {
|
||||
let id = parse_macro_input!(input as LitStr);
|
||||
let Input { dollar_crate, id } = parse_macro_input!(input as Input);
|
||||
assert!(event_id::validate(&id.value()).is_ok(), "Invalid event id");
|
||||
|
||||
let output = quote! {
|
||||
<::ruma::identifiers::EventId as ::std::convert::TryFrom<&str>>::try_from(#id).unwrap()
|
||||
<#dollar_crate::EventId as ::std::convert::TryFrom<&str>>::try_from(#id).unwrap()
|
||||
};
|
||||
|
||||
output.into()
|
||||
@ -33,11 +48,11 @@ pub fn event_id(input: TokenStream) -> TokenStream {
|
||||
|
||||
#[proc_macro]
|
||||
pub fn room_alias_id(input: TokenStream) -> TokenStream {
|
||||
let id = parse_macro_input!(input as LitStr);
|
||||
let Input { dollar_crate, id } = parse_macro_input!(input as Input);
|
||||
assert!(room_alias_id::validate(&id.value()).is_ok(), "Invalid room_alias_id");
|
||||
|
||||
let output = quote! {
|
||||
<::ruma::identifiers::RoomAliasId as ::std::convert::TryFrom<&str>>::try_from(#id).unwrap()
|
||||
<#dollar_crate::RoomAliasId as ::std::convert::TryFrom<&str>>::try_from(#id).unwrap()
|
||||
};
|
||||
|
||||
output.into()
|
||||
@ -45,11 +60,11 @@ pub fn room_alias_id(input: TokenStream) -> TokenStream {
|
||||
|
||||
#[proc_macro]
|
||||
pub fn room_id(input: TokenStream) -> TokenStream {
|
||||
let id = parse_macro_input!(input as LitStr);
|
||||
let Input { dollar_crate, id } = parse_macro_input!(input as Input);
|
||||
assert!(room_id::validate(&id.value()).is_ok(), "Invalid room_id");
|
||||
|
||||
let output = quote! {
|
||||
<::ruma::identifiers::RoomId as ::std::convert::TryFrom<&str>>::try_from(#id).unwrap()
|
||||
<#dollar_crate::RoomId as ::std::convert::TryFrom<&str>>::try_from(#id).unwrap()
|
||||
};
|
||||
|
||||
output.into()
|
||||
@ -57,11 +72,11 @@ pub fn room_id(input: TokenStream) -> TokenStream {
|
||||
|
||||
#[proc_macro]
|
||||
pub fn room_version_id(input: TokenStream) -> TokenStream {
|
||||
let id = parse_macro_input!(input as LitStr);
|
||||
let Input { dollar_crate, id } = parse_macro_input!(input as Input);
|
||||
assert!(room_version_id::validate(&id.value()).is_ok(), "Invalid room_version_id");
|
||||
|
||||
let output = quote! {
|
||||
<::ruma::identifiers::RoomVersionId as ::std::convert::TryFrom<&str>>::try_from(#id).unwrap()
|
||||
<#dollar_crate::RoomVersionId as ::std::convert::TryFrom<&str>>::try_from(#id).unwrap()
|
||||
};
|
||||
|
||||
output.into()
|
||||
@ -69,11 +84,11 @@ pub fn room_version_id(input: TokenStream) -> TokenStream {
|
||||
|
||||
#[proc_macro]
|
||||
pub fn server_key_id(input: TokenStream) -> TokenStream {
|
||||
let id = parse_macro_input!(input as LitStr);
|
||||
let Input { dollar_crate, id } = parse_macro_input!(input as Input);
|
||||
assert!(server_key_id::validate(&id.value()).is_ok(), "Invalid server_key_id");
|
||||
|
||||
let output = quote! {
|
||||
<::ruma::identifiers::ServerKeyId as ::std::convert::TryFrom<&str>>::try_from(#id).unwrap()
|
||||
<#dollar_crate::ServerKeyId as ::std::convert::TryFrom<&str>>::try_from(#id).unwrap()
|
||||
};
|
||||
|
||||
output.into()
|
||||
@ -81,11 +96,11 @@ pub fn server_key_id(input: TokenStream) -> TokenStream {
|
||||
|
||||
#[proc_macro]
|
||||
pub fn server_name(input: TokenStream) -> TokenStream {
|
||||
let id = parse_macro_input!(input as LitStr);
|
||||
let Input { dollar_crate, id } = parse_macro_input!(input as Input);
|
||||
assert!(server_name::validate(&id.value()).is_ok(), "Invalid server_name");
|
||||
|
||||
let output = quote! {
|
||||
<::std::boxed::Box::<::ruma::identifiers::ServerName> as ::std::convert::TryFrom<&str>>::try_from(#id).unwrap()
|
||||
<::std::boxed::Box::<#dollar_crate::ServerName> as ::std::convert::TryFrom<&str>>::try_from(#id).unwrap()
|
||||
};
|
||||
|
||||
output.into()
|
||||
@ -93,11 +108,11 @@ pub fn server_name(input: TokenStream) -> TokenStream {
|
||||
|
||||
#[proc_macro]
|
||||
pub fn user_id(input: TokenStream) -> TokenStream {
|
||||
let id = parse_macro_input!(input as LitStr);
|
||||
let Input { dollar_crate, id } = parse_macro_input!(input as Input);
|
||||
assert!(user_id::validate(&id.value()).is_ok(), "Invalid user_id");
|
||||
|
||||
let output = quote! {
|
||||
<::ruma::identifiers::UserId as ::std::convert::TryFrom<&str>>::try_from(#id).unwrap()
|
||||
<#dollar_crate::UserId as ::std::convert::TryFrom<&str>>::try_from(#id).unwrap()
|
||||
};
|
||||
|
||||
output.into()
|
||||
|
@ -25,6 +25,7 @@ default = ["serde"]
|
||||
[dependencies]
|
||||
either = { version = "1.5.3", optional = true }
|
||||
rand = { version = "0.7.3", optional = true }
|
||||
ruma-identifiers-macros = { version = "=0.17.1", path = "../ruma-identifiers-macros" }
|
||||
ruma-identifiers-validation = { version = "0.1.0", path = "../ruma-identifiers-validation" }
|
||||
serde = { version = "1.0.114", optional = true, features = ["derive"] }
|
||||
strum = { version = "0.18.0", features = ["derive"] }
|
||||
|
@ -81,3 +81,70 @@ macro_rules! device_id {
|
||||
::std::boxed::Box<$crate::DeviceId>::from($s)
|
||||
};
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
pub use ruma_identifiers_macros as _macros;
|
||||
|
||||
/// Compile-time checked `DeviceKeyId` construction.
|
||||
#[macro_export]
|
||||
macro_rules! device_key_id {
|
||||
($s:literal) => {
|
||||
$crate::_macros::device_key_id!($crate, $s)
|
||||
};
|
||||
}
|
||||
|
||||
/// Compile-time checked `EventId` construction.
|
||||
#[macro_export]
|
||||
macro_rules! event_id {
|
||||
($s:literal) => {
|
||||
$crate::_macros::event_id!($crate, $s)
|
||||
};
|
||||
}
|
||||
|
||||
/// Compile-time checked `RoomAliasId` construction.
|
||||
#[macro_export]
|
||||
macro_rules! room_alias_id {
|
||||
($s:literal) => {
|
||||
$crate::_macros::room_alias_id!($crate, $s)
|
||||
};
|
||||
}
|
||||
|
||||
/// Compile-time checked `RoomId` construction.
|
||||
#[macro_export]
|
||||
macro_rules! room_id {
|
||||
($s:literal) => {
|
||||
$crate::_macros::room_id!($crate, $s)
|
||||
};
|
||||
}
|
||||
|
||||
/// Compile-time checked `RoomVersionId` construction.
|
||||
#[macro_export]
|
||||
macro_rules! room_version_id {
|
||||
($s:literal) => {
|
||||
$crate::_macros::room_version_id!($crate, $s)
|
||||
};
|
||||
}
|
||||
|
||||
/// Compile-time checked `ServerKeyId` construction.
|
||||
#[macro_export]
|
||||
macro_rules! server_key_id {
|
||||
($s:literal) => {
|
||||
$crate::_macros::server_key_id!($crate, $s)
|
||||
};
|
||||
}
|
||||
|
||||
/// Compile-time checked `ServerName` construction.
|
||||
#[macro_export]
|
||||
macro_rules! server_name {
|
||||
($s:literal) => {
|
||||
$crate::_macros::server_name!($crate, $s)
|
||||
};
|
||||
}
|
||||
|
||||
/// Compile-time checked `UserId` construction.
|
||||
#[macro_export]
|
||||
macro_rules! user_id {
|
||||
($s:literal) => {
|
||||
$crate::_macros::user_id!($crate, $s)
|
||||
};
|
||||
}
|
||||
|
@ -25,7 +25,6 @@ federation-api = ["ruma-api", "ruma-federation-api", "ruma-signatures"]
|
||||
[dependencies]
|
||||
ruma-common = { version = "0.2.0", path = "../ruma-common" }
|
||||
ruma-identifiers = { version = "0.17.1", path = "../ruma-identifiers", features = ["serde"] }
|
||||
ruma-identifiers-macros = { version = "0.17.1", path = "../ruma-identifiers-macros" }
|
||||
|
||||
ruma-events = { version = "=0.22.0-alpha.1", path = "../ruma-events", optional = true }
|
||||
ruma-signatures = { version = "0.6.0-dev.1", path = "../ruma-signatures", optional = true }
|
||||
|
@ -16,11 +16,11 @@
|
||||
pub use ruma_common::*;
|
||||
#[doc(inline)]
|
||||
pub use ruma_identifiers as identifiers;
|
||||
pub use ruma_identifiers_macros::*;
|
||||
|
||||
pub use ruma_identifiers::{
|
||||
DeviceId, DeviceKeyAlgorithm, DeviceKeyId, EventId, RoomAliasId, RoomId, RoomIdOrAliasId,
|
||||
RoomVersionId, ServerKeyAlgorithm, ServerKeyId, ServerName, UserId,
|
||||
device_id, device_key_id, event_id, room_alias_id, room_id, room_version_id, server_key_id,
|
||||
server_name, user_id, DeviceId, DeviceKeyAlgorithm, DeviceKeyId, EventId, RoomAliasId, RoomId,
|
||||
RoomIdOrAliasId, RoomVersionId, ServerKeyAlgorithm, ServerKeyId, ServerName, UserId,
|
||||
};
|
||||
|
||||
#[cfg(feature = "ruma-events")]
|
||||
|
Loading…
x
Reference in New Issue
Block a user