This commit is contained in:
2022-07-10 05:24:55 +00:00
committed by GitHub
parent 22185554c1
commit 3e796eee62
2 changed files with 60 additions and 4 deletions

View File

@@ -9,6 +9,7 @@ use syn::token;
use syn::Block;
use syn::Expr;
use syn::Ident;
use syn::ItemEnum;
use syn::ItemFn;
use syn::Lit;
use syn::LitBool;
@@ -580,3 +581,42 @@ pub fn check_json_keys(item: TokenStream) -> TokenStream {
);
stream.into()
}
#[proc_macro_derive(CheckUnkown)]
pub fn derive_check_unknown(item: TokenStream) -> TokenStream {
let ItemEnum {
attrs: _,
vis: _,
enum_token: _,
ident,
generics: _,
brace_token: _,
variants,
} = parse_macro_input!(item as ItemEnum);
let mut streams = Vec::new();
for i in variants.iter() {
let ident = i.ident.clone();
let s = ident.to_string();
if s == "Unknown" {
streams.push(
quote!(Self::#ident(data) => Err(crate::fanbox::error::FanboxAPIError::from(format!(
"{} {}",
crate::gettext("Failed to recognize type:"),
data
))),),
);
} else {
streams.push(quote!(Self::#ident(tmp) => crate::fanbox::check::CheckUnkown::check_unknown(tmp),));
}
}
let stream = quote!(
impl crate::fanbox::check::CheckUnkown for #ident {
fn check_unknown(&self) -> Result<(), crate::fanbox::error::FanboxAPIError> {
match self {
#(#streams)*
}
}
}
);
stream.into()
}