common: Add DebugAsRefStr derive macro

This commit is contained in:
Jonas Platte 2023-01-13 13:15:07 +01:00
parent 44d4a34d9c
commit 625d5a0782
No known key found for this signature in database
GPG Key ID: AAA7A61F696C3E0C
5 changed files with 29 additions and 2 deletions

View File

@ -79,6 +79,7 @@ Improvements:
* Add support for bundled reference relations (MSC3267 / Matrix 1.5) * Add support for bundled reference relations (MSC3267 / Matrix 1.5)
* Add the `formatted` field on `KeyVerificationRequestEventContent` (Matrix 1.5) * Add the `formatted` field on `KeyVerificationRequestEventContent` (Matrix 1.5)
* Add `content` accessors for `Any*StateEvent` enums * Add `content` accessors for `Any*StateEvent` enums
* Add the `DebugAsRefStr` derive macro to `ruma_common::serde`
# 0.10.5 # 0.10.5

View File

@ -73,6 +73,6 @@ where
} }
pub use ruma_macros::{ pub use ruma_macros::{
AsRefStr, DeserializeFromCowStr, DisplayAsRefStr, FromString, OrdAsRefStr, PartialEqAsRefStr, AsRefStr, DebugAsRefStr, DeserializeFromCowStr, DisplayAsRefStr, FromString, OrdAsRefStr,
PartialOrdAsRefStr, SerializeAsRefStr, StringEnum, _FakeDeriveSerde, PartialEqAsRefStr, PartialOrdAsRefStr, SerializeAsRefStr, StringEnum, _FakeDeriveSerde,
}; };

View File

@ -39,6 +39,7 @@ use self::{
identifiers::IdentifierInput, identifiers::IdentifierInput,
serde::{ serde::{
as_str_as_ref_str::expand_as_str_as_ref_str, as_str_as_ref_str::expand_as_str_as_ref_str,
debug_as_ref_str::expand_debug_as_ref_str,
deserialize_from_cow_str::expand_deserialize_from_cow_str, deserialize_from_cow_str::expand_deserialize_from_cow_str,
display_as_ref_str::expand_display_as_ref_str, display_as_ref_str::expand_display_as_ref_str,
enum_as_ref_str::expand_enum_as_ref_str, enum_as_ref_str::expand_enum_as_ref_str,
@ -291,6 +292,13 @@ pub fn derive_display_as_ref_str(input: TokenStream) -> TokenStream {
expand_display_as_ref_str(&input.ident).unwrap_or_else(syn::Error::into_compile_error).into() expand_display_as_ref_str(&input.ident).unwrap_or_else(syn::Error::into_compile_error).into()
} }
/// Derive the `fmt::Debug` trait using the `AsRef<str>` implementation of the type.
#[proc_macro_derive(DebugAsRefStr)]
pub fn derive_debug_as_ref_str(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
expand_debug_as_ref_str(&input.ident).unwrap_or_else(syn::Error::into_compile_error).into()
}
/// Derive the `Serialize` trait using the `AsRef<str>` implementation of the type. /// Derive the `Serialize` trait using the `AsRef<str>` implementation of the type.
#[proc_macro_derive(SerializeAsRefStr)] #[proc_macro_derive(SerializeAsRefStr)]
pub fn derive_serialize_as_ref_str(input: TokenStream) -> TokenStream { pub fn derive_serialize_as_ref_str(input: TokenStream) -> TokenStream {

View File

@ -3,6 +3,7 @@
pub mod as_str_as_ref_str; pub mod as_str_as_ref_str;
pub mod attr; pub mod attr;
pub mod case; pub mod case;
pub mod debug_as_ref_str;
pub mod deserialize_from_cow_str; pub mod deserialize_from_cow_str;
pub mod display_as_ref_str; pub mod display_as_ref_str;
pub mod enum_as_ref_str; pub mod enum_as_ref_str;

View File

@ -0,0 +1,17 @@
use proc_macro2::{Ident, TokenStream};
use quote::quote;
pub fn expand_debug_as_ref_str(ident: &Ident) -> syn::Result<TokenStream> {
Ok(quote! {
#[automatically_derived]
#[allow(deprecated)]
impl ::std::fmt::Debug for #ident {
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
<::std::primitive::str as ::std::fmt::Debug>::fmt(
::std::convert::AsRef::<::std::primitive::str>::as_ref(self),
f,
)
}
}
})
}