From 60222a499b4970f670ac4b9841da586bd5c24a6e Mon Sep 17 00:00:00 2001 From: lifegpc Date: Sun, 10 Jul 2022 14:07:25 +0000 Subject: [PATCH] Update --- proc_macros/proc_macros.rs | 8 +-- src/fanbox/article/block.rs | 10 +-- src/fanbox/article/body.rs | 4 +- src/fanbox/check.rs | 2 +- src/fanbox/comment.rs | 4 +- src/fanbox/comment_list.rs | 4 +- src/fanbox/creator.rs | 11 ++-- src/fanbox/plan.rs | 6 +- src/fanbox/post.rs | 122 +++++++++++++++++++++++++++++++++++- 9 files changed, 146 insertions(+), 25 deletions(-) diff --git a/proc_macros/proc_macros.rs b/proc_macros/proc_macros.rs index 8efb868..42e184c 100644 --- a/proc_macros/proc_macros.rs +++ b/proc_macros/proc_macros.rs @@ -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 is handled:").replace("", key).as_str(), obj))?; } + _ => { Err(format!("{} {}", gettext("Key is not handled:").replace("", 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)* diff --git a/src/fanbox/article/block.rs b/src/fanbox/article/block.rs index 85d263d..ce92270 100644 --- a/src/fanbox/article/block.rs +++ b/src/fanbox/article/block.rs @@ -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), diff --git a/src/fanbox/article/body.rs b/src/fanbox/article/body.rs index 0267189..eb99e30 100644 --- a/src/fanbox/article/body.rs +++ b/src/fanbox/article/body.rs @@ -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"+, diff --git a/src/fanbox/check.rs b/src/fanbox/check.rs index b519dec..e70acd7 100644 --- a/src/fanbox/check.rs +++ b/src/fanbox/check.rs @@ -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>; } diff --git a/src/fanbox/comment.rs b/src/fanbox/comment.rs index e60525c..ce563ea 100644 --- a/src/fanbox/comment.rs +++ b/src/fanbox/comment.rs @@ -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"+, diff --git a/src/fanbox/comment_list.rs b/src/fanbox/comment_list.rs index dcb773e..98a5f51 100644 --- a/src/fanbox/comment_list.rs +++ b/src/fanbox/comment_list.rs @@ -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()?; diff --git a/src/fanbox/creator.rs b/src/fanbox/creator.rs index 1ff7cc4..b7b5e01 100644 --- a/src/fanbox/creator.rs +++ b/src/fanbox/creator.rs @@ -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"+, diff --git a/src/fanbox/plan.rs b/src/fanbox/plan.rs index 06ca086..126e415 100644 --- a/src/fanbox/plan.rs +++ b/src/fanbox/plan.rs @@ -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()?; diff --git a/src/fanbox/post.rs b/src/fanbox/post.rs index b218e0c..17826bf 100644 --- a/src/fanbox/post.rs +++ b/src/fanbox/post.rs @@ -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);