From 4fafc3c3de9337a9d6b4f7202ae8a56b76905b65 Mon Sep 17 00:00:00 2001 From: lifegpc Date: Mon, 27 Jun 2022 14:17:18 +0000 Subject: [PATCH] Update --- src/fanbox/item.rs | 108 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) diff --git a/src/fanbox/item.rs b/src/fanbox/item.rs index 9b26a5c..636c2f8 100644 --- a/src/fanbox/item.rs +++ b/src/fanbox/item.rs @@ -8,17 +8,72 @@ pub struct FanboxItem { pub data: JsonValue, } +macro_rules! quick_return { + ($exp:expr) => { + match $exp { + Some(r) => { + return Some(r); + } + None => {} + } + }; +} + impl FanboxItem { + #[inline] + pub fn author_name(&self) -> Option<&str> { + self.data["user"]["name"].as_str() + } + + #[inline] + pub fn comment_count(&self) -> Option { + self.data["commentCount"].as_u64() + } + + pub fn cover_image_url(&self) -> Option<&str> { + quick_return!(self.data["coverImageUrl"].as_str()); + match self.cover_type() { + Some(t) => { + if t == "cover_image" { + self.cover_url() + } else { + None + } + } + None => None, + } + } + + #[inline] + pub fn cover_type(&self) -> Option<&str> { + self.data["cover"]["type"].as_str() + } + + #[inline] + pub fn cover_url(&self) -> Option<&str> { + self.data["cover"]["url"].as_str() + } + #[inline] pub fn creator_id(&self) -> Option<&str> { self.data["creatorId"].as_str() } + #[inline] + pub fn excerpt(&self) -> Option<&str> { + self.data["excerpt"].as_str() + } + #[inline] pub fn fee_required(&self) -> Option { self.data["feeRequired"].as_u64() } + #[inline] + pub fn has_adult_content(&self) -> Option { + self.data["hasAdultContent"].as_bool() + } + #[inline] /// Returns the id of the item pub fn id(&self) -> Option { @@ -30,17 +85,58 @@ impl FanboxItem { self.data["isLiked"].as_bool() } + #[inline] + pub fn is_restricted(&self) -> Option { + self.data["isRestricted"].as_bool() + } + #[inline] pub fn like_count(&self) -> Option { self.data["likeCount"].as_u64() } + #[inline] + pub fn published_datetime(&self) -> Option<&str> { + self.data["publishedDatetime"].as_str() + } + + #[inline] + pub fn tags(&self) -> Option> { + let mut list = Vec::new(); + let tags = &self.data["tags"]; + if tags.is_array() { + for i in tags.members() { + match i.as_str() { + Some(tag) => { + list.push(tag); + } + None => { + return None; + } + } + } + Some(list) + } else { + None + } + } + #[inline] /// Returns item's title pub fn title(&self) -> Option<&str> { self.data["title"].as_str() } + #[inline] + pub fn updated_datetime(&self) -> Option<&str> { + self.data["updatedDatetime"].as_str() + } + + #[inline] + pub fn user_icon_url(&self) -> Option<&str> { + self.data["user"]["iconUrl"].as_str() + } + #[inline] /// Returns pixiv user id pub fn user_id(&self) -> Option { @@ -52,11 +148,23 @@ impl Debug for FanboxItem { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.debug_struct("FanboxItem") .field("id", &self.id()) + .field("author_name", &self.author_name()) + .field("comment_count", &self.comment_count()) + .field("cover_image_url", &self.cover_image_url()) + .field("cover_type", &self.cover_type()) + .field("cover_url", &self.cover_url()) .field("creator_id", &self.creator_id()) + .field("excerpt", &self.excerpt()) .field("fee_required", &self.fee_required()) + .field("has_adult_content", &self.has_adult_content()) .field("is_liked", &self.is_liked()) + .field("is_restricted", &self.is_restricted()) .field("like_count", &self.like_count()) + .field("published_datetime", &self.published_datetime()) + .field("tags", &self.tags()) .field("title", &self.title()) + .field("updated_datetime", &self.updated_datetime()) + .field("user_icon_url", &self.user_icon_url()) .field("user_id", &self.user_id()) .finish_non_exhaustive() }