From 80aaada096744d6996dd7fadf76de61152a0ce5a Mon Sep 17 00:00:00 2001 From: lifegpc Date: Tue, 12 Jul 2022 04:43:28 +0000 Subject: [PATCH] Update --- src/fanbox/article/body.rs | 20 ++++- src/fanbox/article/image.rs | 4 +- src/fanbox/article/mod.rs | 1 + src/fanbox/article/url_embed.rs | 138 ++++++++++++++++++++++++++++++++ 4 files changed, 160 insertions(+), 3 deletions(-) create mode 100644 src/fanbox/article/url_embed.rs diff --git a/src/fanbox/article/body.rs b/src/fanbox/article/body.rs index ab8ffd8..178a08b 100644 --- a/src/fanbox/article/body.rs +++ b/src/fanbox/article/body.rs @@ -2,6 +2,7 @@ use super::super::check::CheckUnknown; use super::super::error::FanboxAPIError; use super::block::FanboxArticleBlock; use super::image::FanboxArticleImageMap; +use super::url_embed::FanboxArticleUrlEmbedMap; use crate::fanbox_api::FanboxClientInternal; use json::JsonValue; use proc_macros::check_json_keys; @@ -48,6 +49,16 @@ impl FanboxArticleBody { client, } } + + #[inline] + pub fn url_embed_map(&self) -> Option { + let map = &self.data["urlEmbedMap"]; + if map.is_object() { + Some(FanboxArticleUrlEmbedMap::new(map, Arc::clone(&self.client))) + } else { + None + } + } } impl CheckUnknown for FanboxArticleBody { @@ -57,7 +68,7 @@ impl CheckUnknown for FanboxArticleBody { "imageMap"+, "fileMap": [], "embedMap": [], - "urlEmbedMap": [], + "urlEmbedMap"+, ); match self.blocks() { Some(blocks) => { @@ -73,6 +84,12 @@ impl CheckUnknown for FanboxArticleBody { } None => {} } + match self.url_embed_map() { + Some(map) => { + map.check_unknown()?; + } + None => {} + } Ok(()) } } @@ -82,6 +99,7 @@ impl Debug for FanboxArticleBody { f.debug_struct("FanboxArticleBody") .field("blocks", &self.blocks()) .field("image_map", &self.image_map()) + .field("url_embed_map", &self.url_embed_map()) .finish_non_exhaustive() } } diff --git a/src/fanbox/article/image.rs b/src/fanbox/article/image.rs index 9a06fcf..413b3cc 100644 --- a/src/fanbox/article/image.rs +++ b/src/fanbox/article/image.rs @@ -18,8 +18,8 @@ impl FanboxArticleImage { } #[inline] - pub fn height(&self) -> Option<&str> { - self.data["height"].as_str() + pub fn height(&self) -> Option { + self.data["height"].as_u64() } #[inline] diff --git a/src/fanbox/article/mod.rs b/src/fanbox/article/mod.rs index 9f86765..e3276ad 100644 --- a/src/fanbox/article/mod.rs +++ b/src/fanbox/article/mod.rs @@ -1,3 +1,4 @@ pub mod block; pub mod body; pub mod image; +pub mod url_embed; diff --git a/src/fanbox/article/url_embed.rs b/src/fanbox/article/url_embed.rs new file mode 100644 index 0000000..8e702a3 --- /dev/null +++ b/src/fanbox/article/url_embed.rs @@ -0,0 +1,138 @@ +use super::super::check::CheckUnknown; +use super::super::error::FanboxAPIError; +use crate::fanbox_api::FanboxClientInternal; +use crate::parser::json::parse_u64; +use json::JsonValue; +use proc_macros::check_json_keys; +use std::fmt::Debug; +use std::sync::Arc; + +pub struct FanboxArticleUrlEmbedFanboxCreator { + pub data: JsonValue, + client: Arc, +} + +impl FanboxArticleUrlEmbedFanboxCreator { + #[inline] + pub fn id(&self) -> Option<&str> { + self.data["id"].as_str() + } + + #[inline] + /// Create a new instance + pub fn new(data: &JsonValue, client: Arc) -> Self { + Self { + data: data.clone(), + client, + } + } + + #[inline] + pub fn user_id(&self) -> Option { + parse_u64(&self.data["profile"]["user"]["userId"]) + } + + #[inline] + pub fn user_name(&self) -> Option<&str> { + self.data["profile"]["user"]["name"].as_str() + } +} + +impl CheckUnknown for FanboxArticleUrlEmbedFanboxCreator { + fn check_unknown(&self) -> Result<(), FanboxAPIError> { + check_json_keys!( + "id"+, + "type", + "profile": [ + "user": [ + "userId"+user_id, + "name"+user_name, + ], + ], + ); + Ok(()) + } +} + +impl Debug for FanboxArticleUrlEmbedFanboxCreator { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("FanboxArticleUrlEmbedFanboxCreator") + .field("id", &self.id()) + .field("user_id", &self.user_id()) + .field("user_name", &self.user_name()) + .finish_non_exhaustive() + } +} + +#[derive(proc_macros::CheckUnknown, Debug)] +pub enum FanboxArticleUrlEmbed { + FanboxCreator(FanboxArticleUrlEmbedFanboxCreator), + Unknown(JsonValue), +} + +impl FanboxArticleUrlEmbed { + #[inline] + /// Create a new instance + pub fn new(data: &JsonValue, client: Arc) -> Self { + match data["type"].as_str() { + Some(t) => match t { + "fanbox.creator" => { + Self::FanboxCreator(FanboxArticleUrlEmbedFanboxCreator::new(data, client)) + } + _ => Self::Unknown(data.clone()), + }, + None => Self::Unknown(data.clone()), + } + } +} + +pub struct FanboxArticleUrlEmbedMap { + pub data: JsonValue, + client: Arc, +} + +impl FanboxArticleUrlEmbedMap { + #[inline] + pub fn get_url_embed + ?Sized>(&self, id: &S) -> Option { + let id = id.as_ref(); + let embed = &self.data[id]; + if embed.is_object() { + Some(FanboxArticleUrlEmbed::new(embed, Arc::clone(&self.client))) + } else { + None + } + } + + #[inline] + /// Create a new instance + pub fn new(data: &JsonValue, client: Arc) -> Self { + Self { + data: data.clone(), + client, + } + } +} + +impl CheckUnknown for FanboxArticleUrlEmbedMap { + fn check_unknown(&self) -> Result<(), FanboxAPIError> { + for (key, _) in self.data.entries() { + match self.get_url_embed(key) { + Some(embed) => { + embed.check_unknown()?; + } + None => {} + } + } + Ok(()) + } +} + +impl Debug for FanboxArticleUrlEmbedMap { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let mut d = f.debug_struct("FanboxArticleUrlEmbedMap"); + for (key, _) in self.data.entries() { + d.field(key, &self.get_url_embed(key)); + } + d.finish_non_exhaustive() + } +}