ruma_macros/serde/
deserialize_from_cow_str.rs

1use proc_macro2::{Ident, TokenStream};
2use quote::quote;
3
4use crate::util::import_ruma_common;
5
6pub fn expand_deserialize_from_cow_str(ident: &Ident) -> syn::Result<TokenStream> {
7    let ruma_common = import_ruma_common();
8
9    Ok(quote! {
10        #[automatically_derived]
11        #[allow(deprecated)]
12        impl<'de> #ruma_common::exports::serde::de::Deserialize<'de> for #ident {
13            fn deserialize<D>(deserializer: D) -> ::std::result::Result<Self, D::Error>
14            where
15                D: #ruma_common::exports::serde::de::Deserializer<'de>,
16            {
17                type CowStr<'a> = ::std::borrow::Cow<'a, ::std::primitive::str>;
18
19                let cow = #ruma_common::serde::deserialize_cow_str(deserializer)?;
20                Ok(::std::convert::From::<CowStr<'_>>::from(cow))
21            }
22        }
23    })
24}