|
| 1 | +use proc_macro2::{Span, TokenStream}; |
| 2 | +use quote::quote_spanned; |
| 3 | +use syn::{ |
| 4 | + Attribute, Expr, ExprLit, Ident, Lit, Meta, MetaNameValue, Path, PathArguments, PathSegment, |
| 5 | + Token, Type, |
| 6 | +}; |
| 7 | + |
| 8 | +use crate::utils::extract_meta_from_lists; |
| 9 | + |
| 10 | +struct Context<'a, 'b> { |
| 11 | + name: &'a str, |
| 12 | + span: Span, |
| 13 | + target: &'a Type, |
| 14 | + origin: &'a Type, |
| 15 | + attrs: &'a [Attribute], |
| 16 | + impls: &'b mut TokenStream, |
| 17 | +} |
| 18 | + |
| 19 | +impl Context<'_, '_> { |
| 20 | + fn debug(self) { |
| 21 | + let Self { |
| 22 | + span, |
| 23 | + target, |
| 24 | + origin, |
| 25 | + impls, |
| 26 | + .. |
| 27 | + } = self; |
| 28 | + |
| 29 | + impls.extend(quote_spanned! { span => |
| 30 | + impl ::core::fmt::Debug for #target { |
| 31 | + #[inline(always)] |
| 32 | + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { |
| 33 | + if let Ok(origin) = <#origin as ::core::convert::TryFrom<#target>>::try_from(*self) { |
| 34 | + <#origin as ::core::fmt::Debug>::fmt(&origin, f)?; |
| 35 | + write!(f, "({})", self.repr) |
| 36 | + } else { |
| 37 | + write!(f, "<Unknown>({})", self.repr) |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + }) |
| 42 | + } |
| 43 | + |
| 44 | + fn serialize(self) { |
| 45 | + let Self { |
| 46 | + name, |
| 47 | + target, |
| 48 | + origin, |
| 49 | + span, |
| 50 | + attrs, |
| 51 | + impls, |
| 52 | + .. |
| 53 | + } = self; |
| 54 | + |
| 55 | + let serde = locate_serde(attrs); |
| 56 | + |
| 57 | + impls.extend(quote_spanned! { span => |
| 58 | + impl #serde::Serialize for #target { |
| 59 | + #[inline(always)] |
| 60 | + fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> |
| 61 | + where |
| 62 | + S: #serde::Serializer, |
| 63 | + { |
| 64 | + if let Ok(origin) = <#origin as ::core::convert::TryFrom<#target>>::try_from(*self) { |
| 65 | + <#origin as #serde::Serialize>::serialize(&origin, serializer) |
| 66 | + } else { |
| 67 | + <S as #serde::Serializer>::serialize_unit_variant(serializer, #name, <#target as ::ffi_enum::FfiEnum>::UNKNOWN.repr as _, "<Unknown>") |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + }); |
| 72 | + } |
| 73 | + |
| 74 | + fn deserialize(self) { |
| 75 | + let Self { |
| 76 | + target, |
| 77 | + origin, |
| 78 | + span, |
| 79 | + attrs, |
| 80 | + impls, |
| 81 | + .. |
| 82 | + } = self; |
| 83 | + let serde = locate_serde(attrs); |
| 84 | + |
| 85 | + impls.extend(quote_spanned! { span => |
| 86 | + impl<'de> #serde::Deserialize<'de> for #target { |
| 87 | + #[inline(always)] |
| 88 | + fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 89 | + where |
| 90 | + D: #serde::Deserializer<'de>, |
| 91 | + { |
| 92 | + Ok(<#origin as #serde::Deserialize>::deserialize(deserializer).map(Into::into).unwrap_or_else(|_|<#target as ::ffi_enum::FfiEnum>::UNKNOWN)) |
| 93 | + } |
| 94 | + } |
| 95 | + }); |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +fn locate_serde(attrs: &[Attribute]) -> Path { |
| 100 | + extract_meta_from_lists(attrs, "serde") |
| 101 | + .find_map(|meta| { |
| 102 | + let MetaNameValue { path, value, .. } = meta.require_name_value().ok()?; |
| 103 | + if !path.is_ident("crate") { |
| 104 | + return None; |
| 105 | + } |
| 106 | + let Expr::Lit(ExprLit { |
| 107 | + lit: Lit::Str(s), .. |
| 108 | + }) = value |
| 109 | + else { |
| 110 | + return None; |
| 111 | + }; |
| 112 | + s.parse::<Path>().ok() |
| 113 | + }) |
| 114 | + .unwrap_or_else(|| Path { |
| 115 | + leading_colon: Some(Token)), |
| 116 | + segments: [PathSegment { |
| 117 | + ident: Ident::new("serde", Span::mixed_site()), |
| 118 | + arguments: PathArguments::None, |
| 119 | + }] |
| 120 | + .into_iter() |
| 121 | + .collect(), |
| 122 | + }) |
| 123 | +} |
| 124 | + |
| 125 | +pub fn delegate<'a, 'b>( |
| 126 | + name: &'a str, |
| 127 | + target: &'a Type, |
| 128 | + origin: &'a Type, |
| 129 | + attrs: &'a [Attribute], |
| 130 | + impls: &'b mut TokenStream, |
| 131 | +) -> impl FnMut(Meta) + use<'a, 'b> { |
| 132 | + |meta| { |
| 133 | + let Meta::Path(path) = meta else { |
| 134 | + return; |
| 135 | + }; |
| 136 | + |
| 137 | + let Some(ident) = path.get_ident() else { |
| 138 | + return; |
| 139 | + }; |
| 140 | + |
| 141 | + let context = Context { |
| 142 | + name, |
| 143 | + span: ident.span().resolved_at(Span::mixed_site()), |
| 144 | + target, |
| 145 | + origin, |
| 146 | + attrs, |
| 147 | + impls, |
| 148 | + }; |
| 149 | + |
| 150 | + match ident.to_string().as_str() { |
| 151 | + "Debug" => context.debug(), |
| 152 | + "Serialize" => context.serialize(), |
| 153 | + "Deserialize" => context.deserialize(), |
| 154 | + _ => return, |
| 155 | + }; |
| 156 | + } |
| 157 | +} |
0 commit comments