diff --git a/crates/ruma-common/CHANGELOG.md b/crates/ruma-common/CHANGELOG.md index ead76bbf..5c463ef8 100644 --- a/crates/ruma-common/CHANGELOG.md +++ b/crates/ruma-common/CHANGELOG.md @@ -79,6 +79,7 @@ Improvements: * Add support for bundled reference relations (MSC3267 / Matrix 1.5) * Add the `formatted` field on `KeyVerificationRequestEventContent` (Matrix 1.5) * Add `content` accessors for `Any*StateEvent` enums +* Add the `DebugAsRefStr` derive macro to `ruma_common::serde` # 0.10.5 diff --git a/crates/ruma-common/src/serde.rs b/crates/ruma-common/src/serde.rs index 0dda75c4..740222f8 100644 --- a/crates/ruma-common/src/serde.rs +++ b/crates/ruma-common/src/serde.rs @@ -73,6 +73,6 @@ where } pub use ruma_macros::{ - AsRefStr, DeserializeFromCowStr, DisplayAsRefStr, FromString, OrdAsRefStr, PartialEqAsRefStr, - PartialOrdAsRefStr, SerializeAsRefStr, StringEnum, _FakeDeriveSerde, + AsRefStr, DebugAsRefStr, DeserializeFromCowStr, DisplayAsRefStr, FromString, OrdAsRefStr, + PartialEqAsRefStr, PartialOrdAsRefStr, SerializeAsRefStr, StringEnum, _FakeDeriveSerde, }; diff --git a/crates/ruma-macros/src/lib.rs b/crates/ruma-macros/src/lib.rs index 9eb4f931..e8fe3b0a 100644 --- a/crates/ruma-macros/src/lib.rs +++ b/crates/ruma-macros/src/lib.rs @@ -39,6 +39,7 @@ use self::{ identifiers::IdentifierInput, serde::{ 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, display_as_ref_str::expand_display_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() } +/// Derive the `fmt::Debug` trait using the `AsRef` 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` implementation of the type. #[proc_macro_derive(SerializeAsRefStr)] pub fn derive_serialize_as_ref_str(input: TokenStream) -> TokenStream { diff --git a/crates/ruma-macros/src/serde.rs b/crates/ruma-macros/src/serde.rs index 708461f7..4b0bd018 100644 --- a/crates/ruma-macros/src/serde.rs +++ b/crates/ruma-macros/src/serde.rs @@ -3,6 +3,7 @@ pub mod as_str_as_ref_str; pub mod attr; pub mod case; +pub mod debug_as_ref_str; pub mod deserialize_from_cow_str; pub mod display_as_ref_str; pub mod enum_as_ref_str; diff --git a/crates/ruma-macros/src/serde/debug_as_ref_str.rs b/crates/ruma-macros/src/serde/debug_as_ref_str.rs new file mode 100644 index 00000000..d74eef5c --- /dev/null +++ b/crates/ruma-macros/src/serde/debug_as_ref_str.rs @@ -0,0 +1,17 @@ +use proc_macro2::{Ident, TokenStream}; +use quote::quote; + +pub fn expand_debug_as_ref_str(ident: &Ident) -> syn::Result { + 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, + ) + } + } + }) +}