From 3ed956c1a6e085d6bfc9f510d8b48d8dc8a3ba53 Mon Sep 17 00:00:00 2001 From: lifegpc Date: Fri, 1 Jul 2022 13:34:22 +0000 Subject: [PATCH] Update --- src/fanbox/creator.rs | 122 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 122 insertions(+) diff --git a/src/fanbox/creator.rs b/src/fanbox/creator.rs index cf211b5..08e833e 100644 --- a/src/fanbox/creator.rs +++ b/src/fanbox/creator.rs @@ -1,9 +1,124 @@ +use super::error::FanboxAPIError; use crate::fanbox_api::FanboxClientInternal; use crate::parser::json::parse_u64; use json::JsonValue; +use std::convert::From; use std::fmt::Debug; +use std::ops::Deref; +use std::ops::DerefMut; use std::sync::Arc; +/// Profile image +pub struct FanboxProfileImage { + /// Raw data + pub data: JsonValue, + /// Fanbox API Client + client: Arc, +} + +impl FanboxProfileImage { + #[inline] + pub fn id(&self) -> Option<&str> { + self.data["id"].as_str() + } + + #[inline] + pub fn image_url(&self) -> Option<&str> { + self.data["imageUrl"].as_str() + } + + #[inline] + /// Create a new instance + pub fn new(data: &JsonValue, client: Arc) -> Self { + Self { + data: data.clone(), + client, + } + } + + #[inline] + pub fn thumbnail_url(&self) -> Option<&str> { + self.data["thumbnailUrl"].as_str() + } +} + +impl Debug for FanboxProfileImage { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("FanboxProfileImage") + .field("id", &self.id()) + .field("image_url", &self.image_url()) + .field("thumbnail_url", &self.thumbnail_url()) + .finish_non_exhaustive() + } +} + +/// Profile item +#[derive(Debug)] +pub enum FanboxProfileItem { + /// Image + Image(FanboxProfileImage), + /// Other types + Unknown(JsonValue), +} + +impl FanboxProfileItem { + #[inline] + /// Create a new instance + pub fn new(data: &JsonValue, client: Arc) -> Self { + match data["type"].as_str() { + Some(s) => match s { + "image" => Self::Image(FanboxProfileImage::new(data, client)), + _ => Self::Unknown(data.clone()), + }, + _ => Self::Unknown(data.clone()), + } + } +} + +/// A list of [FanboxProfileItem] +pub struct FanboxProfileItems { + /// The list + list: Vec, +} + +impl FanboxProfileItems { + #[inline] + /// Create a new instance + pub fn new( + data: &JsonValue, + client: Arc, + ) -> Result { + if data.is_array() { + let mut list = Vec::new(); + for i in data.members() { + list.push(FanboxProfileItem::new(i, Arc::clone(&client))); + } + Ok(Self { list }) + } else { + Err(FanboxAPIError::from("Failed to get profile items.")) + } + } +} + +impl Debug for FanboxProfileItems { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + self.list.fmt(f) + } +} + +impl Deref for FanboxProfileItems { + type Target = Vec; + fn deref(&self) -> &Self::Target { + &self.list + } +} + +impl DerefMut for FanboxProfileItems { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.list + } +} + /// To present a creator's info pub struct FanboxCreator { /// Raw data @@ -33,6 +148,7 @@ impl FanboxCreator { self.data["hasAdultContent"].as_bool() } + #[inline] /// Create a new instance pub fn new(data: &JsonValue, client: Arc) -> Self { Self { @@ -41,6 +157,11 @@ impl FanboxCreator { } } + #[inline] + pub fn profile_items(&self) -> Result { + FanboxProfileItems::new(&self.data["profileItems"], Arc::clone(&self.client)) + } + #[inline] pub fn profile_links(&self) -> Option> { let mut links = Vec::new(); @@ -83,6 +204,7 @@ impl Debug for FanboxCreator { .field("cover_image_url", &self.cover_image_url()) .field("description", &self.description()) .field("has_adult_content", &self.has_adult_content()) + .field("profile_items", &self.profile_items()) .field("profile_links", &self.profile_links()) .field("user_icon_url", &self.user_icon_url()) .field("user_id", &self.user_id())