macros: Merge ruma-identifiers-macros into ruma-macros
This commit is contained in:
		
							parent
							
								
									c52e51c016
								
							
						
					
					
						commit
						60eb51ee0e
					
				| @ -1,17 +0,0 @@ | ||||
| [package] | ||||
| description = "Procedural macros for creating Matrix identifiers." | ||||
| homepage = "https://www.ruma.io/" | ||||
| keywords = ["matrix", "chat", "messaging", "ruma"] | ||||
| license = "MIT" | ||||
| name = "ruma-identifiers-macros" | ||||
| repository = "https://github.com/ruma/ruma" | ||||
| version = "0.22.0" | ||||
| edition = "2018" | ||||
| 
 | ||||
| [lib] | ||||
| proc-macro = true | ||||
| 
 | ||||
| [dependencies] | ||||
| quote = "1.0.8" | ||||
| ruma-identifiers-validation = { version = "0.7.0", path = "../ruma-identifiers-validation", default-features = false } | ||||
| syn = "1.0.55" | ||||
| @ -1,133 +0,0 @@ | ||||
| #![doc(html_favicon_url = "https://www.ruma.io/favicon.ico")] | ||||
| #![doc(html_logo_url = "https://www.ruma.io/images/logo.png")] | ||||
| 
 | ||||
| use proc_macro::TokenStream; | ||||
| use quote::quote; | ||||
| use ruma_identifiers_validation::{ | ||||
|     device_key_id, event_id, key_id, mxc_uri, room_alias_id, room_id, room_version_id, server_name, | ||||
|     user_id, | ||||
| }; | ||||
| 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()?; | ||||
|         let _: Token![,] = input.parse()?; | ||||
|         let id = input.parse()?; | ||||
| 
 | ||||
|         Ok(Self { dollar_crate, id }) | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| #[proc_macro] | ||||
| pub fn device_key_id(input: TokenStream) -> TokenStream { | ||||
|     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! { | ||||
|         <&#dollar_crate::DeviceKeyId as ::std::convert::TryFrom<&str>>::try_from(#id).unwrap() | ||||
|     }; | ||||
| 
 | ||||
|     output.into() | ||||
| } | ||||
| 
 | ||||
| #[proc_macro] | ||||
| pub fn event_id(input: TokenStream) -> TokenStream { | ||||
|     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! { | ||||
|         <&#dollar_crate::EventId as ::std::convert::TryFrom<&str>>::try_from(#id).unwrap() | ||||
|     }; | ||||
| 
 | ||||
|     output.into() | ||||
| } | ||||
| 
 | ||||
| #[proc_macro] | ||||
| pub fn room_alias_id(input: TokenStream) -> TokenStream { | ||||
|     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! { | ||||
|         <&#dollar_crate::RoomAliasId as ::std::convert::TryFrom<&str>>::try_from(#id).unwrap() | ||||
|     }; | ||||
| 
 | ||||
|     output.into() | ||||
| } | ||||
| 
 | ||||
| #[proc_macro] | ||||
| pub fn room_id(input: TokenStream) -> TokenStream { | ||||
|     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! { | ||||
|         <&#dollar_crate::RoomId as ::std::convert::TryFrom<&str>>::try_from(#id).unwrap() | ||||
|     }; | ||||
| 
 | ||||
|     output.into() | ||||
| } | ||||
| 
 | ||||
| #[proc_macro] | ||||
| pub fn room_version_id(input: TokenStream) -> TokenStream { | ||||
|     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! { | ||||
|         <#dollar_crate::RoomVersionId as ::std::convert::TryFrom<&str>>::try_from(#id).unwrap() | ||||
|     }; | ||||
| 
 | ||||
|     output.into() | ||||
| } | ||||
| 
 | ||||
| #[proc_macro] | ||||
| pub fn server_signing_key_id(input: TokenStream) -> TokenStream { | ||||
|     let Input { dollar_crate, id } = parse_macro_input!(input as Input); | ||||
|     assert!(key_id::validate(&id.value()).is_ok(), "Invalid server_signing_key_id"); | ||||
| 
 | ||||
|     let output = quote! { | ||||
|         <&#dollar_crate::ServerSigningKeyId as ::std::convert::TryFrom<&str>>::try_from(#id).unwrap() | ||||
|     }; | ||||
| 
 | ||||
|     output.into() | ||||
| } | ||||
| 
 | ||||
| #[proc_macro] | ||||
| pub fn server_name(input: TokenStream) -> TokenStream { | ||||
|     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! { | ||||
|         <&#dollar_crate::ServerName as ::std::convert::TryFrom<&str>>::try_from(#id).unwrap() | ||||
|     }; | ||||
| 
 | ||||
|     output.into() | ||||
| } | ||||
| 
 | ||||
| #[proc_macro] | ||||
| pub fn mxc_uri(input: TokenStream) -> TokenStream { | ||||
|     let Input { dollar_crate, id } = parse_macro_input!(input as Input); | ||||
|     assert!(mxc_uri::validate(&id.value()).is_ok(), "Invalid mxc://"); | ||||
| 
 | ||||
|     let output = quote! { | ||||
|         <&#dollar_crate::MxcUri as ::std::convert::From<&str>>::from(#id) | ||||
|     }; | ||||
| 
 | ||||
|     output.into() | ||||
| } | ||||
| 
 | ||||
| #[proc_macro] | ||||
| pub fn user_id(input: TokenStream) -> TokenStream { | ||||
|     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! { | ||||
|         <&#dollar_crate::UserId as ::std::convert::TryFrom<&str>>::try_from(#id).unwrap() | ||||
|     }; | ||||
| 
 | ||||
|     output.into() | ||||
| } | ||||
| @ -1,6 +1,6 @@ | ||||
| [package] | ||||
| name = "ruma-identifiers-validation" | ||||
| description = "Validation logic for ruma-identifiers and ruma-identifiers-macros" | ||||
| description = "Validation logic for ruma-identifiers and ruma-macros" | ||||
| homepage = "https://www.ruma.io/" | ||||
| repository = "https://github.com/ruma/ruma" | ||||
| license = "MIT" | ||||
|  | ||||
| @ -24,8 +24,8 @@ serde = ["ruma-serde", "serde1"] | ||||
| either = { version = "1.6.1", optional = true } | ||||
| percent-encoding = "2.1.0" | ||||
| rand_crate = { package = "rand", version = "0.8.3", optional = true } | ||||
| ruma-identifiers-macros = { version = "=0.22.0", path = "../ruma-identifiers-macros" } | ||||
| ruma-identifiers-validation = { version = "0.7.0", path = "../ruma-identifiers-validation", default-features = false } | ||||
| ruma-macros = { version = "=0.1.0", path = "../ruma-macros" } | ||||
| ruma-serde = { version = "0.6.0", path = "../ruma-serde", optional = true } | ||||
| ruma-serde-macros = { version = "0.6.0", path = "../ruma-serde-macros" } | ||||
| # Renamed so we can have a serde feature. | ||||
|  | ||||
| @ -110,7 +110,10 @@ macro_rules! device_id { | ||||
| // Bug report: https://github.com/rust-lang/rust/issues/83939
 | ||||
| #[doc(hidden)] | ||||
| pub mod _macros { | ||||
|     pub use ruma_identifiers_macros::*; | ||||
|     pub use ruma_macros::{ | ||||
|         device_key_id, event_id, mxc_uri, room_alias_id, room_id, room_version_id, server_name, | ||||
|         server_signing_key_id, user_id, | ||||
|     }; | ||||
| } | ||||
| 
 | ||||
| /// Compile-time checked `DeviceKeyId` construction.
 | ||||
|  | ||||
| @ -20,4 +20,5 @@ compat = [] | ||||
| proc-macro-crate = "1.0.0" | ||||
| proc-macro2 = "1.0.24" | ||||
| quote = "1.0.8" | ||||
| ruma-identifiers-validation = { version = "0.7.0", path = "../ruma-identifiers-validation", default-features = false } | ||||
| syn = { version = "1.0.57", features = ["extra-traits", "full"] } | ||||
|  | ||||
							
								
								
									
										20
									
								
								crates/ruma-macros/src/identifiers.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								crates/ruma-macros/src/identifiers.rs
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,20 @@ | ||||
| //! Methods and types for generating [ruma-identifiers].
 | ||||
| //!
 | ||||
| //! [ruma-identifiers]: https://github.com/ruma/ruma/tree/main/ruma-identifiers
 | ||||
| 
 | ||||
| use syn::{parse::Parse, LitStr, Path, Token}; | ||||
| 
 | ||||
| pub struct IdentifierInput { | ||||
|     pub dollar_crate: Path, | ||||
|     pub id: LitStr, | ||||
| } | ||||
| 
 | ||||
| impl Parse for IdentifierInput { | ||||
|     fn parse(input: syn::parse::ParseStream<'_>) -> syn::Result<Self> { | ||||
|         let dollar_crate = input.parse()?; | ||||
|         let _: Token![,] = input.parse()?; | ||||
|         let id = input.parse()?; | ||||
| 
 | ||||
|         Ok(Self { dollar_crate, id }) | ||||
|     } | ||||
| } | ||||
| @ -10,17 +10,25 @@ use proc_macro::TokenStream; | ||||
| use proc_macro2 as pm2; | ||||
| use proc_macro_crate::{crate_name, FoundCrate}; | ||||
| use quote::{format_ident, quote}; | ||||
| use ruma_identifiers_validation::{ | ||||
|     device_key_id, event_id, key_id, mxc_uri, room_alias_id, room_id, room_version_id, server_name, | ||||
|     user_id, | ||||
| }; | ||||
| use syn::{parse_macro_input, DeriveInput}; | ||||
| 
 | ||||
| use self::events::{ | ||||
|     event::expand_event, | ||||
|     event_content::expand_event_content, | ||||
|     event_enum::{expand_event_enums, expand_from_impls_derived}, | ||||
|     event_parse::EventEnumInput, | ||||
|     event_type::expand_event_type_enum, | ||||
| use self::{ | ||||
|     events::{ | ||||
|         event::expand_event, | ||||
|         event_content::expand_event_content, | ||||
|         event_enum::{expand_event_enums, expand_from_impls_derived}, | ||||
|         event_parse::EventEnumInput, | ||||
|         event_type::expand_event_type_enum, | ||||
|     }, | ||||
|     identifiers::IdentifierInput, | ||||
| }; | ||||
| 
 | ||||
| mod events; | ||||
| mod identifiers; | ||||
| 
 | ||||
| /// Generates an enum to represent the various Matrix event types.
 | ||||
| ///
 | ||||
| @ -110,3 +118,120 @@ pub fn derive_from_event_to_enum(input: TokenStream) -> TokenStream { | ||||
|     let input = parse_macro_input!(input as DeriveInput); | ||||
|     expand_from_impls_derived(input).into() | ||||
| } | ||||
| 
 | ||||
| /// Compile-time checked `DeviceKeyId` construction.
 | ||||
| #[proc_macro] | ||||
| pub fn device_key_id(input: TokenStream) -> TokenStream { | ||||
|     let IdentifierInput { dollar_crate, id } = parse_macro_input!(input as IdentifierInput); | ||||
|     assert!(device_key_id::validate(&id.value()).is_ok(), "Invalid device key id"); | ||||
| 
 | ||||
|     let output = quote! { | ||||
|         <&#dollar_crate::DeviceKeyId as ::std::convert::TryFrom<&str>>::try_from(#id).unwrap() | ||||
|     }; | ||||
| 
 | ||||
|     output.into() | ||||
| } | ||||
| 
 | ||||
| /// Compile-time checked `EventId` construction.
 | ||||
| #[proc_macro] | ||||
| pub fn event_id(input: TokenStream) -> TokenStream { | ||||
|     let IdentifierInput { dollar_crate, id } = parse_macro_input!(input as IdentifierInput); | ||||
|     assert!(event_id::validate(&id.value()).is_ok(), "Invalid event id"); | ||||
| 
 | ||||
|     let output = quote! { | ||||
|         <&#dollar_crate::EventId as ::std::convert::TryFrom<&str>>::try_from(#id).unwrap() | ||||
|     }; | ||||
| 
 | ||||
|     output.into() | ||||
| } | ||||
| 
 | ||||
| /// Compile-time checked `RoomAliasId` construction.
 | ||||
| #[proc_macro] | ||||
| pub fn room_alias_id(input: TokenStream) -> TokenStream { | ||||
|     let IdentifierInput { dollar_crate, id } = parse_macro_input!(input as IdentifierInput); | ||||
|     assert!(room_alias_id::validate(&id.value()).is_ok(), "Invalid room_alias_id"); | ||||
| 
 | ||||
|     let output = quote! { | ||||
|         <&#dollar_crate::RoomAliasId as ::std::convert::TryFrom<&str>>::try_from(#id).unwrap() | ||||
|     }; | ||||
| 
 | ||||
|     output.into() | ||||
| } | ||||
| 
 | ||||
| /// Compile-time checked `RoomId` construction.
 | ||||
| #[proc_macro] | ||||
| pub fn room_id(input: TokenStream) -> TokenStream { | ||||
|     let IdentifierInput { dollar_crate, id } = parse_macro_input!(input as IdentifierInput); | ||||
|     assert!(room_id::validate(&id.value()).is_ok(), "Invalid room_id"); | ||||
| 
 | ||||
|     let output = quote! { | ||||
|         <&#dollar_crate::RoomId as ::std::convert::TryFrom<&str>>::try_from(#id).unwrap() | ||||
|     }; | ||||
| 
 | ||||
|     output.into() | ||||
| } | ||||
| 
 | ||||
| /// Compile-time checked `RoomVersionId` construction.
 | ||||
| #[proc_macro] | ||||
| pub fn room_version_id(input: TokenStream) -> TokenStream { | ||||
|     let IdentifierInput { dollar_crate, id } = parse_macro_input!(input as IdentifierInput); | ||||
|     assert!(room_version_id::validate(&id.value()).is_ok(), "Invalid room_version_id"); | ||||
| 
 | ||||
|     let output = quote! { | ||||
|         <#dollar_crate::RoomVersionId as ::std::convert::TryFrom<&str>>::try_from(#id).unwrap() | ||||
|     }; | ||||
| 
 | ||||
|     output.into() | ||||
| } | ||||
| 
 | ||||
| /// Compile-time checked `ServerSigningKeyId` construction.
 | ||||
| #[proc_macro] | ||||
| pub fn server_signing_key_id(input: TokenStream) -> TokenStream { | ||||
|     let IdentifierInput { dollar_crate, id } = parse_macro_input!(input as IdentifierInput); | ||||
|     assert!(key_id::validate(&id.value()).is_ok(), "Invalid server_signing_key_id"); | ||||
| 
 | ||||
|     let output = quote! { | ||||
|         <&#dollar_crate::ServerSigningKeyId as ::std::convert::TryFrom<&str>>::try_from(#id).unwrap() | ||||
|     }; | ||||
| 
 | ||||
|     output.into() | ||||
| } | ||||
| 
 | ||||
| /// Compile-time checked `ServerName` construction.
 | ||||
| #[proc_macro] | ||||
| pub fn server_name(input: TokenStream) -> TokenStream { | ||||
|     let IdentifierInput { dollar_crate, id } = parse_macro_input!(input as IdentifierInput); | ||||
|     assert!(server_name::validate(&id.value()).is_ok(), "Invalid server_name"); | ||||
| 
 | ||||
|     let output = quote! { | ||||
|         <&#dollar_crate::ServerName as ::std::convert::TryFrom<&str>>::try_from(#id).unwrap() | ||||
|     }; | ||||
| 
 | ||||
|     output.into() | ||||
| } | ||||
| 
 | ||||
| /// Compile-time checked `MxcUri` construction.
 | ||||
| #[proc_macro] | ||||
| pub fn mxc_uri(input: TokenStream) -> TokenStream { | ||||
|     let IdentifierInput { dollar_crate, id } = parse_macro_input!(input as IdentifierInput); | ||||
|     assert!(mxc_uri::validate(&id.value()).is_ok(), "Invalid mxc://"); | ||||
| 
 | ||||
|     let output = quote! { | ||||
|         <&#dollar_crate::MxcUri as ::std::convert::From<&str>>::from(#id) | ||||
|     }; | ||||
| 
 | ||||
|     output.into() | ||||
| } | ||||
| 
 | ||||
| /// Compile-time checked `UserId` construction.
 | ||||
| #[proc_macro] | ||||
| pub fn user_id(input: TokenStream) -> TokenStream { | ||||
|     let IdentifierInput { dollar_crate, id } = parse_macro_input!(input as IdentifierInput); | ||||
|     assert!(user_id::validate(&id.value()).is_ok(), "Invalid user_id"); | ||||
| 
 | ||||
|     let output = quote! { | ||||
|         <&#dollar_crate::UserId as ::std::convert::TryFrom<&str>>::try_from(#id).unwrap() | ||||
|     }; | ||||
| 
 | ||||
|     output.into() | ||||
| } | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user