serde: Add PartialOrdAsRefStr, OrdAsRefStr and PartialEqAsRefStr derives

This commit is contained in:
Kévin Commaille 2021-05-11 09:10:57 +02:00 committed by Jonas Platte
parent 6a2c028cfb
commit 32e80987e1
4 changed files with 65 additions and 0 deletions

View File

@ -0,0 +1,14 @@
use proc_macro2::{Ident, TokenStream};
use quote::quote;
pub fn expand_partial_eq_as_ref_str(ident: &Ident) -> syn::Result<TokenStream> {
Ok(quote! {
#[automatically_derived]
impl ::std::cmp::PartialEq for #ident {
fn eq(&self, other: &Self) -> bool {
let other = ::std::convert::AsRef::<::std::primitive::str>::as_ref(other);
::std::convert::AsRef::<::std::primitive::str>::as_ref(self) == other
}
}
})
}

View File

@ -9,6 +9,8 @@ use deserialize_from_cow_str::expand_deserialize_from_cow_str;
use display_as_ref_str::expand_display_as_ref_str;
use enum_as_ref_str::expand_enum_as_ref_str;
use enum_from_string::expand_enum_from_string;
use eq_as_ref_str::expand_partial_eq_as_ref_str;
use ord_as_ref_str::{expand_ord_as_ref_str, expand_partial_ord_as_ref_str};
use outgoing::expand_derive_outgoing;
use serialize_as_ref_str::expand_serialize_as_ref_str;
@ -18,6 +20,8 @@ mod deserialize_from_cow_str;
mod display_as_ref_str;
mod enum_as_ref_str;
mod enum_from_string;
mod eq_as_ref_str;
mod ord_as_ref_str;
mod outgoing;
mod serialize_as_ref_str;
mod util;
@ -105,6 +109,26 @@ pub fn derive_deserialize_from_cow_str(input: TokenStream) -> TokenStream {
.into()
}
#[proc_macro_derive(PartialOrdAsRefStr)]
pub fn derive_partial_ord_as_ref_str(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
expand_partial_ord_as_ref_str(&input.ident)
.unwrap_or_else(syn::Error::into_compile_error)
.into()
}
#[proc_macro_derive(OrdAsRefStr)]
pub fn derive_ord_as_ref_str(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
expand_ord_as_ref_str(&input.ident).unwrap_or_else(syn::Error::into_compile_error).into()
}
#[proc_macro_derive(PartialEqAsRefStr)]
pub fn derive_partial_eq_as_ref_str(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
expand_partial_eq_as_ref_str(&input.ident).unwrap_or_else(syn::Error::into_compile_error).into()
}
/// Shorthand for the derives `AsRefStr`, `FromString`, `DisplayAsRefStr`, `SerializeAsRefStr` and
/// `DeserializeFromCowStr`.
#[proc_macro_derive(StringEnum, attributes(ruma_enum))]

View File

@ -0,0 +1,26 @@
use proc_macro2::{Ident, TokenStream};
use quote::quote;
pub fn expand_partial_ord_as_ref_str(ident: &Ident) -> syn::Result<TokenStream> {
Ok(quote! {
#[automatically_derived]
impl ::std::cmp::PartialOrd for #ident {
fn partial_cmp(&self, other: &Self) -> ::std::option::Option<::std::cmp::Ordering> {
let other = ::std::convert::AsRef::<::std::primitive::str>::as_ref(other);
::std::convert::AsRef::<::std::primitive::str>::as_ref(self).partial_cmp(other)
}
}
})
}
pub fn expand_ord_as_ref_str(ident: &Ident) -> syn::Result<TokenStream> {
Ok(quote! {
#[automatically_derived]
impl ::std::cmp::Ord for #ident {
fn cmp(&self, other: &Self) -> ::std::cmp::Ordering {
let other = ::std::convert::AsRef::<::std::primitive::str>::as_ref(other);
::std::convert::AsRef::<::std::primitive::str>::as_ref(self).cmp(other)
}
}
})
}

View File

@ -7,6 +7,7 @@ Breaking changes:
Improvements:
* Add serialization decorator `none_as_empty_string` to serialize `None`s as empty strings
* Add `PartialOrdAsRefStr`, `OrdAsRefStr` and `PartialEqAsRefStr` derives
# 0.3.1