fanbox api: item deserailze more data

This commit is contained in:
2022-06-26 08:34:52 +00:00
committed by GitHub
parent ad97e261cb
commit 9b2c7a8602
3 changed files with 44 additions and 12 deletions

View File

@@ -1,3 +1,4 @@
use crate::parser::json::parse_u64;
use json::JsonValue;
use std::fmt::Debug;
@@ -8,19 +9,20 @@ pub struct FanboxItem {
}
impl FanboxItem {
#[inline]
pub fn creator_id(&self) -> Option<&str> {
self.data["creatorId"].as_str()
}
#[inline]
pub fn fee_required(&self) -> Option<u64> {
self.data["feeRequired"].as_u64()
}
#[inline]
/// Returns the id of the item
pub fn id(&self) -> Option<u64> {
let id = &self.data["id"];
match id.as_u64() {
Some(id) => Some(id),
None => match id.as_str() {
Some(id) => match id.trim().parse::<u64>() {
Ok(id) => Some(id),
Err(_) => None,
},
None => None,
},
}
parse_u64(&self.data["id"])
}
#[inline]
@@ -28,19 +30,34 @@ impl FanboxItem {
self.data["isLiked"].as_bool()
}
#[inline]
pub fn like_count(&self) -> Option<u64> {
self.data["likeCount"].as_u64()
}
#[inline]
/// Returns item's title
pub fn title(&self) -> Option<&str> {
self.data["title"].as_str()
}
#[inline]
/// Returns pixiv user id
pub fn user_id(&self) -> Option<u64> {
parse_u64(&self.data["user"]["userId"])
}
}
impl Debug for FanboxItem {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("FanboxItem")
.field("id", &self.id())
.field("title", &self.title())
.field("creator_id", &self.creator_id())
.field("fee_required", &self.fee_required())
.field("is_liked", &self.is_liked())
.field("like_count", &self.like_count())
.field("title", &self.title())
.field("user_id", &self.user_id())
.finish_non_exhaustive()
}
}

14
src/parser/json.rs Normal file
View File

@@ -0,0 +1,14 @@
use json::JsonValue;
pub fn parse_u64(data: &JsonValue) -> Option<u64> {
match data.as_u64() {
Some(num) => Some(num),
None => match data.as_str() {
Some(num) => match num.trim().parse::<u64>() {
Ok(num) => Some(num),
Err(_) => None,
},
None => None,
},
}
}

View File

@@ -1,2 +1,3 @@
pub mod description;
pub mod json;
pub mod metadata;