This commit is contained in:
2022-07-10 14:07:25 +00:00
committed by GitHub
parent ff2c93ee8f
commit 60222a499b
9 changed files with 146 additions and 25 deletions

View File

@@ -637,7 +637,7 @@ pub fn check_json_keys(item: TokenStream) -> TokenStream {
for (key, sobj) in self.data.entries() {
match key {
#(#streams)*
_ => { Err(format!("{} {}", gettext("Key <key> is handled:").replace("<key>", key).as_str(), obj))?; }
_ => { Err(format!("{} {}", gettext("Key <key> is not handled:").replace("<key>", key).as_str(), obj))?; }
}
}
}
@@ -645,7 +645,7 @@ pub fn check_json_keys(item: TokenStream) -> TokenStream {
stream.into()
}
#[proc_macro_derive(CheckUnkown)]
#[proc_macro_derive(CheckUnknown)]
pub fn derive_check_unknown(item: TokenStream) -> TokenStream {
let ItemEnum {
attrs: _,
@@ -670,12 +670,12 @@ pub fn derive_check_unknown(item: TokenStream) -> TokenStream {
);
} else {
streams.push(
quote!(Self::#ident(tmp) => crate::fanbox::check::CheckUnkown::check_unknown(tmp),),
quote!(Self::#ident(tmp) => crate::fanbox::check::CheckUnknown::check_unknown(tmp),),
);
}
}
let stream = quote!(
impl crate::fanbox::check::CheckUnkown for #ident {
impl crate::fanbox::check::CheckUnknown for #ident {
fn check_unknown(&self) -> Result<(), crate::fanbox::error::FanboxAPIError> {
match self {
#(#streams)*

View File

@@ -1,4 +1,4 @@
use super::super::check::CheckUnkown;
use super::super::check::CheckUnknown;
use super::super::error::FanboxAPIError;
use json::JsonValue;
use proc_macros::check_json_keys;
@@ -26,7 +26,7 @@ impl FanboxArticleParagraphBoldStyle {
}
}
impl CheckUnkown for FanboxArticleParagraphBoldStyle {
impl CheckUnknown for FanboxArticleParagraphBoldStyle {
fn check_unknown(&self) -> Result<(), FanboxAPIError> {
check_json_keys!(
"offset"+,
@@ -46,7 +46,7 @@ impl Debug for FanboxArticleParagraphBoldStyle {
}
}
#[derive(proc_macros::CheckUnkown, Debug)]
#[derive(proc_macros::CheckUnknown, Debug)]
pub enum FanboxArticleParagraphStyle {
Bold(FanboxArticleParagraphBoldStyle),
Unknown(JsonValue),
@@ -97,7 +97,7 @@ impl FanboxArticleParagraphBlock {
}
}
impl CheckUnkown for FanboxArticleParagraphBlock {
impl CheckUnknown for FanboxArticleParagraphBlock {
fn check_unknown(&self) -> Result<(), FanboxAPIError> {
check_json_keys!(
"styles",
@@ -125,7 +125,7 @@ impl Debug for FanboxArticleParagraphBlock {
}
}
#[derive(proc_macros::CheckUnkown, Debug)]
#[derive(proc_macros::CheckUnknown, Debug)]
pub enum FanboxArticleBlock {
Paragraph(FanboxArticleParagraphBlock),
Unknown(JsonValue),

View File

@@ -1,4 +1,4 @@
use super::super::check::CheckUnkown;
use super::super::check::CheckUnknown;
use super::super::error::FanboxAPIError;
use super::block::FanboxArticleBlock;
use crate::fanbox_api::FanboxClientInternal;
@@ -39,7 +39,7 @@ impl FanboxArticleBody {
}
}
impl CheckUnkown for FanboxArticleBody {
impl CheckUnknown for FanboxArticleBody {
fn check_unknown(&self) -> Result<(), FanboxAPIError> {
check_json_keys!(
"blocks"+,

View File

@@ -1,6 +1,6 @@
use super::error::FanboxAPIError;
/// Check if have data that we don't handle
pub trait CheckUnkown {
pub trait CheckUnknown {
fn check_unknown(&self) -> Result<(), FanboxAPIError>;
}

View File

@@ -1,4 +1,4 @@
use super::check::CheckUnkown;
use super::check::CheckUnknown;
use crate::parser::json::parse_u64;
use json::JsonValue;
use proc_macros::check_json_keys;
@@ -85,7 +85,7 @@ impl FanboxComment {
}
}
impl CheckUnkown for FanboxComment {
impl CheckUnknown for FanboxComment {
fn check_unknown(&self) -> Result<(), super::error::FanboxAPIError> {
check_json_keys!(
"id"+,

View File

@@ -1,4 +1,4 @@
use super::check::CheckUnkown;
use super::check::CheckUnknown;
use super::comment::FanboxComment;
#[cfg(test)]
use super::post::FanboxPost;
@@ -72,7 +72,7 @@ impl FanboxCommentList {
}
}
impl CheckUnkown for FanboxCommentList {
impl CheckUnknown for FanboxCommentList {
fn check_unknown(&self) -> Result<(), super::error::FanboxAPIError> {
for i in self.items.iter() {
i.check_unknown()?;

View File

@@ -1,4 +1,4 @@
use super::check::CheckUnkown;
use super::check::CheckUnknown;
use super::error::FanboxAPIError;
use crate::fanbox_api::FanboxClientInternal;
use crate::parser::json::parse_u64;
@@ -44,12 +44,13 @@ impl FanboxProfileImage {
}
}
impl CheckUnkown for FanboxProfileImage {
impl CheckUnknown for FanboxProfileImage {
fn check_unknown(&self) -> Result<(), FanboxAPIError> {
check_json_keys!(
"id"+,
"imageUrl"+,
"thumbnailUrl",
"type",
);
Ok(())
}
@@ -66,7 +67,7 @@ impl Debug for FanboxProfileImage {
}
/// Profile item
#[derive(proc_macros::CheckUnkown, Debug)]
#[derive(proc_macros::CheckUnknown, Debug)]
pub enum FanboxProfileItem {
/// Image
Image(FanboxProfileImage),
@@ -113,7 +114,7 @@ impl FanboxProfileItems {
}
}
impl CheckUnkown for FanboxProfileItems {
impl CheckUnknown for FanboxProfileItems {
fn check_unknown(&self) -> Result<(), FanboxAPIError> {
for i in self.list.iter() {
i.check_unknown()?;
@@ -244,7 +245,7 @@ impl FanboxCreator {
}
}
impl CheckUnkown for FanboxCreator {
impl CheckUnknown for FanboxCreator {
fn check_unknown(&self) -> Result<(), FanboxAPIError> {
check_json_keys!(
"creatorId"+,

View File

@@ -1,4 +1,4 @@
use super::check::CheckUnkown;
use super::check::CheckUnknown;
use super::error::FanboxAPIError;
use crate::gettext;
use crate::parser::json::parse_u64;
@@ -71,7 +71,7 @@ impl FanboxPlan {
}
}
impl CheckUnkown for FanboxPlan {
impl CheckUnknown for FanboxPlan {
fn check_unknown(&self) -> Result<(), FanboxAPIError> {
check_json_keys!(
"id"+,
@@ -151,7 +151,7 @@ impl FanboxPlanList {
}
}
impl CheckUnkown for FanboxPlanList {
impl CheckUnknown for FanboxPlanList {
fn check_unknown(&self) -> Result<(), FanboxAPIError> {
for i in self.list.iter() {
i.check_unknown()?;

View File

@@ -1,8 +1,11 @@
use super::article::body::FanboxArticleBody;
use super::check::CheckUnknown;
use super::comment_list::FanboxCommentList;
use super::error::FanboxAPIError;
use crate::fanbox_api::FanboxClientInternal;
use crate::parser::json::parse_u64;
use json::JsonValue;
use proc_macros::check_json_keys;
use proc_macros::fanbox_api_test;
use std::fmt::Debug;
use std::fmt::Display;
@@ -163,6 +166,57 @@ impl FanboxPostArticle {
}
}
impl CheckUnknown for FanboxPostArticle {
fn check_unknown(&self) -> Result<(), FanboxAPIError> {
check_json_keys!(
"body",
"id"+,
"commentCount"+,
"commentList",
"coverImageUrl",
"creatorId"+,
"excerpt"+,
"feeRequired"+,
"hasAdultContent"+,
"imageForShare",
"isLiked"+,
"isRestricted"+,
"likeCount"+,
"nextPost",
"prevPost",
"publishedDatetime"+,
"restrictedFor",
"tags"+,
"type",
"title"+,
"updatedDatetime"+,
"user": [
"userId"+user_id,
"iconUrl",
"name"+,
],
);
self.body().check_unknown()?;
match self.comment_list() {
Some(list) => {
for i in list.items {
i.check_unknown()?;
}
}
None => {}
}
match self.next_post() {
Some(post) => post.check_unknown()?,
None => {}
}
match self.prev_post() {
Some(post) => post.check_unknown()?,
None => {}
}
Ok(())
}
}
impl Debug for FanboxPostArticle {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("FanboxPostArticle")
@@ -342,6 +396,55 @@ impl FanboxPostImage {
}
}
impl CheckUnknown for FanboxPostImage {
fn check_unknown(&self) -> Result<(), FanboxAPIError> {
check_json_keys!(
"id"+,
"commentCount"+,
"commentList",
"coverImageUrl",
"creatorId"+,
"excerpt"+,
"feeRequired"+,
"hasAdultContent"+,
"imageForShare",
"isLiked"+,
"isRestricted"+,
"likeCount"+,
"nextPost",
"prevPost",
"publishedDatetime"+,
"restrictedFor",
"tags"+,
"title"+,
"type",
"updatedDatetime"+,
"user": [
"userId"+user_id,
"iconUrl",
"name"+,
],
);
match self.comment_list() {
Some(list) => {
for i in list.items {
i.check_unknown()?;
}
}
None => {}
}
match self.next_post() {
Some(post) => post.check_unknown()?,
None => {}
}
match self.prev_post() {
Some(post) => post.check_unknown()?,
None => {}
}
Ok(())
}
}
impl Debug for FanboxPostImage {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("FanboxPostImage")
@@ -415,6 +518,17 @@ impl FanboxPostRef {
}
}
impl CheckUnknown for FanboxPostRef {
fn check_unknown(&self) -> Result<(), FanboxAPIError> {
check_json_keys!(
"id"+,
"publishedDatetime"+,
"title"+,
);
Ok(())
}
}
impl Debug for FanboxPostRef {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("FanboxPostRef")
@@ -484,7 +598,7 @@ impl Display for FanboxPostUnknown {
}
}
#[derive(Debug)]
#[derive(proc_macros::CheckUnknown, Debug)]
/// The fanbox's post
pub enum FanboxPost {
/// Article
@@ -669,6 +783,12 @@ fanbox_api_test!(test_get_post_info, {
);
assert_eq!(data.user_name(), Some("しらたま"));
assert_eq!(data.creator_id(), Some("shiratamaco"));
match data.check_unknown() {
Ok(_) => {}
Err(e) => {
println!("Check unknown: {}", e);
}
}
match data.next_post() {
Some(r) => {
println!("{:?}", r);