This commit is contained in:
2022-07-10 12:11:34 +00:00
committed by GitHub
parent 54e382db90
commit 22fb473a6c
4 changed files with 76 additions and 24 deletions

1
Cargo.lock generated
View File

@@ -1338,6 +1338,7 @@ version = "0.0.1"
dependencies = [
"convert_case 0.5.0",
"parse_duration",
"proc-macro2",
"quote",
"syn",
]

View File

@@ -13,5 +13,6 @@ proc-macro = true
[dependencies]
convert_case = "0.5"
parse_duration = "2"
proc-macro2 = "1"
quote = "1"
syn = { version = "1", features = ["full"] }

View File

@@ -516,7 +516,7 @@ pub fn filter_http_methods(item: TokenStream) -> TokenStream {
}
struct CheckJsonKeys {
pub keys: Vec<(LitStr, bool)>,
pub keys: Vec<(LitStr, bool, Option<CheckJsonKeys>)>,
}
impl Parse for CheckJsonKeys {
@@ -525,11 +525,19 @@ impl Parse for CheckJsonKeys {
let first: LitStr = input.parse()?;
match input.parse::<token::Add>() {
Ok(_) => {
keys.push((first, true));
}
Err(_) => {
keys.push((first, false));
keys.push((first, true, None));
}
Err(_) => match input.parse::<token::Colon>() {
Ok(_) => {
let content;
bracketed!(content in input);
let childrens = CheckJsonKeys::parse(&content)?;
keys.push((first, false, Some(childrens)));
}
Err(_) => {
keys.push((first, false, None));
}
},
}
while !input.is_empty() {
let _: token::Comma = input.parse()?;
@@ -539,42 +547,75 @@ impl Parse for CheckJsonKeys {
let key: LitStr = input.parse()?;
match input.parse::<token::Add>() {
Ok(_) => {
keys.push((key, true));
}
Err(_) => {
keys.push((key, false));
keys.push((key, true, None));
}
Err(_) => match input.parse::<token::Colon>() {
Ok(_) => {
let content;
bracketed!(content in input);
let childrens = CheckJsonKeys::parse(&content)?;
keys.push((key, false, Some(childrens)));
}
Err(_) => {
keys.push((key, false, None));
}
},
}
}
Ok(Self { keys })
}
}
fn get_check_json_keys_streams(
keys: Vec<(LitStr, bool, Option<CheckJsonKeys>)>,
) -> Vec<proc_macro2::TokenStream> {
let mut streams = Vec::new();
for (key, check, childrens) in keys {
match childrens {
Some(childrens) => {
let streams2 = get_check_json_keys_streams(childrens.keys);
streams.push(quote!(#key => {
let obj = sobj;
obj.is_object().try_err(format!("{} {}", gettext("Data is not a object:"), obj))?;
for (key, sobj) in obj.entries() {
match key {
#(#streams2)*
_ => { Err(format!("{} {}", gettext("Key <key> is handled:").replace("<key>", key).as_str(), obj))?; }
}
}
}))
}
None => {
if check {
let k = key.value();
let k = k.to_case(Case::Snake);
let fun = Ident::new(&k, key.span());
streams.push(quote!(#key => {
self.#fun().try_err(format!("{} {}", gettext("The value of the key <key> is missing:").replace("<key>", key).as_str(), obj))?;
}));
} else {
streams.push(quote!(#key => {}));
}
}
}
}
streams
}
#[proc_macro]
pub fn check_json_keys(item: TokenStream) -> TokenStream {
let CheckJsonKeys { keys } = parse_macro_input!(item as CheckJsonKeys);
let mut streams = Vec::new();
for (key, check) in keys {
if check {
let k = key.value();
let k = k.to_case(Case::Snake);
let fun = Ident::new(&k, key.span());
streams.push(quote!(#key => {
self.#fun().try_err(format!("{} {}", gettext("The value of the key <key> is missing:").replace("<key>", key).as_str(), self.data))?;
}));
} else {
streams.push(quote!(#key => {}));
}
}
let streams = get_check_json_keys_streams(keys);
let stream = quote!(
{
use crate::ext::try_err::TryErr;
use crate::gettext;
self.data.is_object().try_err(format!("{} {}", gettext("Data is not a object:"), self.data))?;
for (key, _) in self.data.entries() {
let obj = &self.data;
for (key, sobj) in self.data.entries() {
match key {
#(#streams)*
_ => { Err(format!("{} {}", gettext("Key <key> is handled:").replace("<key>", key).as_str(), self.data))?; }
_ => { Err(format!("{} {}", gettext("Key <key> is handled:").replace("<key>", key).as_str(), obj))?; }
}
}
}

View File

@@ -1,5 +1,7 @@
use super::check::CheckUnkown;
use crate::parser::json::parse_u64;
use json::JsonValue;
use proc_macros::check_json_keys;
use std::fmt::Debug;
pub struct FanboxComment {
@@ -83,6 +85,13 @@ impl FanboxComment {
}
}
impl CheckUnkown for FanboxComment {
fn check_unknown(&self) -> Result<(), super::error::FanboxAPIError> {
check_json_keys!("id"+, "body"+, "createdDatetime"+, "isLiked"+, "isOwn"+, "likeCount"+, "parentCommentId", "replies", "rootCommentId", "user": ["userId", "name", "iconUrl"]);
Ok(())
}
}
impl Debug for FanboxComment {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("FanboxComment")