This commit is contained in:
2022-07-12 04:43:28 +00:00
committed by GitHub
parent 6a0e1a976c
commit 80aaada096
4 changed files with 160 additions and 3 deletions

View File

@@ -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<FanboxArticleUrlEmbedMap> {
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()
}
}

View File

@@ -18,8 +18,8 @@ impl FanboxArticleImage {
}
#[inline]
pub fn height(&self) -> Option<&str> {
self.data["height"].as_str()
pub fn height(&self) -> Option<u64> {
self.data["height"].as_u64()
}
#[inline]

View File

@@ -1,3 +1,4 @@
pub mod block;
pub mod body;
pub mod image;
pub mod url_embed;

View File

@@ -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<FanboxClientInternal>,
}
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<FanboxClientInternal>) -> Self {
Self {
data: data.clone(),
client,
}
}
#[inline]
pub fn user_id(&self) -> Option<u64> {
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<FanboxClientInternal>) -> 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<FanboxClientInternal>,
}
impl FanboxArticleUrlEmbedMap {
#[inline]
pub fn get_url_embed<S: AsRef<str> + ?Sized>(&self, id: &S) -> Option<FanboxArticleUrlEmbed> {
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<FanboxClientInternal>) -> 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()
}
}