diff --git a/src/fanbox/creator.rs b/src/fanbox/creator.rs new file mode 100644 index 0000000..cf211b5 --- /dev/null +++ b/src/fanbox/creator.rs @@ -0,0 +1,92 @@ +use crate::fanbox_api::FanboxClientInternal; +use crate::parser::json::parse_u64; +use json::JsonValue; +use std::fmt::Debug; +use std::sync::Arc; + +/// To present a creator's info +pub struct FanboxCreator { + /// Raw data + pub data: JsonValue, + /// Fanbox api client + client: Arc, +} + +impl FanboxCreator { + #[inline] + pub fn creator_id(&self) -> Option<&str> { + self.data["creatorId"].as_str() + } + + #[inline] + pub fn cover_image_url(&self) -> Option<&str> { + self.data["coverImageUrl"].as_str() + } + + #[inline] + pub fn description(&self) -> Option<&str> { + self.data["description"].as_str() + } + + #[inline] + pub fn has_adult_content(&self) -> Option { + self.data["hasAdultContent"].as_bool() + } + + /// Create a new instance + pub fn new(data: &JsonValue, client: Arc) -> Self { + Self { + data: data.clone(), + client, + } + } + + #[inline] + pub fn profile_links(&self) -> Option> { + let mut links = Vec::new(); + let pro = &self.data["profileLinks"]; + if pro.is_array() { + for i in pro.members() { + match i.as_str() { + Some(s) => links.push(s), + None => { + return None; + } + } + } + Some(links) + } else { + None + } + } + + #[inline] + pub fn user_icon_url(&self) -> Option<&str> { + self.data["user"]["iconUrl"].as_str() + } + + #[inline] + pub fn user_id(&self) -> Option { + parse_u64(&self.data["user"]["userId"]) + } + + #[inline] + pub fn user_name(&self) -> Option<&str> { + self.data["user"]["name"].as_str() + } +} + +impl Debug for FanboxCreator { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("FanboxCreator") + .field("creator_id", &self.creator_id()) + .field("cover_image_url", &self.cover_image_url()) + .field("description", &self.description()) + .field("has_adult_content", &self.has_adult_content()) + .field("profile_links", &self.profile_links()) + .field("user_icon_url", &self.user_icon_url()) + .field("user_id", &self.user_id()) + .field("user_name", &self.user_name()) + .finish_non_exhaustive() + } +} diff --git a/src/fanbox/mod.rs b/src/fanbox/mod.rs index 245251b..bbbdb6f 100644 --- a/src/fanbox/mod.rs +++ b/src/fanbox/mod.rs @@ -1,3 +1,4 @@ +pub mod creator; pub mod error; pub mod item; pub mod item_list; diff --git a/src/fanbox_api.rs b/src/fanbox_api.rs index d7ca556..5936505 100644 --- a/src/fanbox_api.rs +++ b/src/fanbox_api.rs @@ -1,6 +1,7 @@ use crate::ext::atomic::AtomicQuick; use crate::ext::replace::ReplaceWith2; use crate::ext::rw_lock::GetRwLock; +use crate::fanbox::creator::FanboxCreator; use crate::fanbox::item_list::FanboxItemList; use crate::fanbox::paginated_creator_posts::PaginatedCreatorPosts; use crate::fanbox::plan::FanboxPlanList; @@ -262,6 +263,19 @@ pub struct FanboxClient { } impl FanboxClient { + #[allow(dead_code)] + /// Get creator's info + /// * `creator_id` - The id of the creator + pub async fn get_creator + ?Sized>( + &self, + creator_id: &S, + ) -> Option { + match self.client.get_creator(creator_id).await { + Some(s) => Some(FanboxCreator::new(&s["body"], Arc::clone(&self.client))), + None => None, + } + } + /// Create an new instance pub fn new() -> Self { Self {