serde: Add PartialOrdAsRefStr, OrdAsRefStr and PartialEqAsRefStr derives
This commit is contained in:
parent
6a2c028cfb
commit
32e80987e1
14
crates/ruma-serde-macros/src/eq_as_ref_str.rs
Normal file
14
crates/ruma-serde-macros/src/eq_as_ref_str.rs
Normal 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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
@ -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 display_as_ref_str::expand_display_as_ref_str;
|
||||||
use enum_as_ref_str::expand_enum_as_ref_str;
|
use enum_as_ref_str::expand_enum_as_ref_str;
|
||||||
use enum_from_string::expand_enum_from_string;
|
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 outgoing::expand_derive_outgoing;
|
||||||
use serialize_as_ref_str::expand_serialize_as_ref_str;
|
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 display_as_ref_str;
|
||||||
mod enum_as_ref_str;
|
mod enum_as_ref_str;
|
||||||
mod enum_from_string;
|
mod enum_from_string;
|
||||||
|
mod eq_as_ref_str;
|
||||||
|
mod ord_as_ref_str;
|
||||||
mod outgoing;
|
mod outgoing;
|
||||||
mod serialize_as_ref_str;
|
mod serialize_as_ref_str;
|
||||||
mod util;
|
mod util;
|
||||||
@ -105,6 +109,26 @@ pub fn derive_deserialize_from_cow_str(input: TokenStream) -> TokenStream {
|
|||||||
.into()
|
.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
|
/// Shorthand for the derives `AsRefStr`, `FromString`, `DisplayAsRefStr`, `SerializeAsRefStr` and
|
||||||
/// `DeserializeFromCowStr`.
|
/// `DeserializeFromCowStr`.
|
||||||
#[proc_macro_derive(StringEnum, attributes(ruma_enum))]
|
#[proc_macro_derive(StringEnum, attributes(ruma_enum))]
|
||||||
|
26
crates/ruma-serde-macros/src/ord_as_ref_str.rs
Normal file
26
crates/ruma-serde-macros/src/ord_as_ref_str.rs
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
@ -7,6 +7,7 @@ Breaking changes:
|
|||||||
Improvements:
|
Improvements:
|
||||||
|
|
||||||
* Add serialization decorator `none_as_empty_string` to serialize `None`s as empty strings
|
* Add serialization decorator `none_as_empty_string` to serialize `None`s as empty strings
|
||||||
|
* Add `PartialOrdAsRefStr`, `OrdAsRefStr` and `PartialEqAsRefStr` derives
|
||||||
|
|
||||||
# 0.3.1
|
# 0.3.1
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user