|
| 1 | +use proc_macro::TokenStream; |
| 2 | +use proc_macro2::TokenStream as TokenStream2; |
| 3 | +use quote::quote; |
| 4 | +use syn::{Attribute, Data, DeriveInput, Fields, Meta}; |
| 5 | + |
| 6 | +pub(crate) fn derive_throwable(input: DeriveInput) -> syn::Result<TokenStream> { |
| 7 | + let crate_ident = parse_throwable_crate_ident(&input); |
| 8 | + let exception = parse_throwable_attrs(&input)?; |
| 9 | + parse_throwable_input(&input, crate_ident, exception) |
| 10 | +} |
| 11 | + |
| 12 | +fn parse_throwable_crate_ident(input: &DeriveInput) -> TokenStream2 { |
| 13 | + let has_throwable_crate = attributes_find_ident(&input.attrs, "throwable_crate"); |
| 14 | + let crate_ident = if has_throwable_crate.is_some() { |
| 15 | + quote! { crate } |
| 16 | + } else { |
| 17 | + quote! { phper } |
| 18 | + }; |
| 19 | + crate_ident |
| 20 | +} |
| 21 | + |
| 22 | +fn parse_throwable_attrs(input: &DeriveInput) -> syn::Result<TokenStream2> { |
| 23 | + let attr = attributes_find_ident(&input.attrs, "throwable"); |
| 24 | + attr.map(|attr| { |
| 25 | + attr.parse_args::<Meta>().and_then(|meta| match meta { |
| 26 | + Meta::NameValue(name_value) => { |
| 27 | + if !name_value.path.is_ident("class") { |
| 28 | + Err(syn::Error::new_spanned( |
| 29 | + &attr, |
| 30 | + "now only support #[throwable(error = ?)] for enum", |
| 31 | + )) |
| 32 | + } else { |
| 33 | + let lit = name_value.lit; |
| 34 | + Ok(quote! { #lit }) |
| 35 | + } |
| 36 | + } |
| 37 | + _ => Err(syn::Error::new_spanned( |
| 38 | + &attr, |
| 39 | + "now only support #[throwable(error = ?)] for enum", |
| 40 | + )), |
| 41 | + }) |
| 42 | + }) |
| 43 | + .unwrap_or_else(|| Ok(quote! { "Exception" })) |
| 44 | +} |
| 45 | + |
| 46 | +fn parse_throwable_input( |
| 47 | + input: &DeriveInput, |
| 48 | + crate_ident: TokenStream2, |
| 49 | + exception: TokenStream2, |
| 50 | +) -> syn::Result<TokenStream> { |
| 51 | + let input_ident = &input.ident; |
| 52 | + |
| 53 | + match &input.data { |
| 54 | + Data::Enum(e) => { |
| 55 | + let mut transparent_idents = Vec::new(); |
| 56 | + |
| 57 | + for variant in &e.variants { |
| 58 | + let attr = attributes_find_ident(&variant.attrs, "throwable"); |
| 59 | + match attr { |
| 60 | + Some(attr) => { |
| 61 | + if attr.tokens.to_string() != "(transparent)" { |
| 62 | + return Err(syn::Error::new_spanned( |
| 63 | + &attr, |
| 64 | + "now only support #[throwable(transparent)] for variant", |
| 65 | + )); |
| 66 | + } |
| 67 | + match &variant.fields { |
| 68 | + Fields::Unnamed(f) if f.unnamed.len() == 1 => { |
| 69 | + transparent_idents.push(variant.ident.clone()); |
| 70 | + } |
| 71 | + _ => { |
| 72 | + return Err(syn::Error::new_spanned( |
| 73 | + &variant, |
| 74 | + "now only support unnamed field with one item mark attribute #[throwable]", |
| 75 | + )); |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + None => continue, |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + let mut class_entry_arms = transparent_idents |
| 84 | + .iter() |
| 85 | + .map(|i| { |
| 86 | + quote! { Self::#i(e) => #crate_ident::errors::Throwable::class_entry(e), } |
| 87 | + }) |
| 88 | + .collect::<Vec<_>>(); |
| 89 | + class_entry_arms.push(quote! { _ => ClassEntry::from_globals(#exception).unwrap(), }); |
| 90 | + |
| 91 | + let mut code_arms = transparent_idents |
| 92 | + .iter() |
| 93 | + .map(|i| { |
| 94 | + quote! { Self::#i(e) => #crate_ident::errors::Throwable::code(e), } |
| 95 | + }) |
| 96 | + .collect::<Vec<_>>(); |
| 97 | + code_arms.push(quote! { _ => 0, }); |
| 98 | + |
| 99 | + let mut message_arms = transparent_idents |
| 100 | + .iter() |
| 101 | + .map(|i| { |
| 102 | + quote! { Self::#i(e) => #crate_ident::errors::Throwable::message(e), } |
| 103 | + }) |
| 104 | + .collect::<Vec<_>>(); |
| 105 | + message_arms.push(quote! { _ => std::string::ToString::to_string(&self), }); |
| 106 | + |
| 107 | + Ok((quote! { |
| 108 | + impl #crate_ident::errors::Throwable for #input_ident { |
| 109 | + fn class_entry(&self) -> &#crate_ident::classes::StatelessClassEntry { |
| 110 | + match self { |
| 111 | + #(#class_entry_arms)* |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + fn code(&self) -> u64 { |
| 116 | + match self { |
| 117 | + #(#code_arms)* |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + fn message(&self) -> std::string::String { |
| 122 | + match self { |
| 123 | + #(#message_arms)* |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + }) |
| 128 | + .into()) |
| 129 | + } |
| 130 | + Data::Struct(_) => Err(syn::Error::new_spanned( |
| 131 | + &input, |
| 132 | + "struct auto derive Throwable is not supported", |
| 133 | + )), |
| 134 | + Data::Union(_) => Err(syn::Error::new_spanned( |
| 135 | + &input, |
| 136 | + "union auto derive Throwable is not supported", |
| 137 | + )), |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +fn attributes_find_ident<'a>(attrs: &'a [Attribute], ident: &'a str) -> Option<&'a Attribute> { |
| 142 | + attrs.iter().find(|attr| attr.path.is_ident(ident)) |
| 143 | +} |
0 commit comments