This commit is contained in:
2022-07-06 06:55:43 +00:00
committed by GitHub
parent bcb9470376
commit c2a0e8a02b

View File

@@ -155,6 +155,80 @@ impl Debug for FanboxPostArticle {
}
}
/// Fanbox image post
pub struct FanboxPostImage {
/// Raw data
pub data: JsonValue,
/// Fanbox api client
client: Arc<FanboxClientInternal>,
}
impl FanboxPostImage {
#[inline]
pub fn excerpt(&self) -> Option<&str> {
self.data["excerpt"].as_str()
}
#[inline]
pub fn fee_required(&self) -> Option<u64> {
self.data["feeRequired"].as_u64()
}
#[inline]
pub fn id(&self) -> Option<u64> {
parse_u64(&self.data["id"])
}
#[inline]
pub fn is_liked(&self) -> Option<bool> {
self.data["isLiked"].as_bool()
}
#[inline]
pub fn like_count(&self) -> Option<u64> {
self.data["likeCount"].as_u64()
}
#[inline]
/// Create a new instance
pub fn new(data: &JsonValue, client: Arc<FanboxClientInternal>) -> Self {
Self {
data: data.clone(),
client,
}
}
#[inline]
pub fn published_datetime(&self) -> Option<&str> {
self.data["publishedDatetime"].as_str()
}
#[inline]
pub fn title(&self) -> Option<&str> {
self.data["title"].as_str()
}
#[inline]
pub fn updated_datetime(&self) -> Option<&str> {
self.data["updatedDatetime"].as_str()
}
}
impl Debug for FanboxPostImage {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("FanboxPostImage")
.field("id", &self.id())
.field("excerpt", &self.excerpt())
.field("fee_required", &self.fee_required())
.field("is_liked", &self.is_liked())
.field("like_count", &self.like_count())
.field("published_datetime", &self.published_datetime())
.field("title", &self.title())
.field("updated_datetime", &self.updated_datetime())
.finish_non_exhaustive()
}
}
/// A reference to another post
pub struct FanboxPostRef {
/// Raw data
@@ -164,6 +238,7 @@ pub struct FanboxPostRef {
}
impl FanboxPostRef {
#[allow(dead_code)]
pub async fn get_post(&self) -> Option<FanboxPost> {
match self.id() {
Some(id) => match self.client.get_post_info(id).await {
@@ -214,6 +289,8 @@ impl Debug for FanboxPostRef {
pub enum FanboxPost {
/// Article
Article(FanboxPostArticle),
/// Image
Image(FanboxPostImage),
/// Unknown
Unknown(JsonValue),
}
@@ -225,6 +302,7 @@ impl FanboxPost {
match data["type"].as_str() {
Some(s) => match s {
"article" => Self::Article(FanboxPostArticle::new(data, client)),
"image" => Self::Image(FanboxPostImage::new(data, client)),
_ => Self::Unknown(data.clone()),
},
_ => Self::Unknown(data.clone()),
@@ -232,30 +310,89 @@ impl FanboxPost {
}
}
#[allow(dead_code)]
impl FanboxPost {
#[inline]
pub fn excerpt(&self) -> Option<&str> {
self.get_json()["excerpt"].as_str()
}
#[inline]
pub fn fee_required(&self) -> Option<u64> {
self.get_json()["feeRequired"].as_u64()
}
#[inline]
pub fn get_json(&self) -> &JsonValue {
match self {
Self::Article(a) => &a.data,
Self::Image(a) => &a.data,
Self::Unknown(a) => a,
}
}
#[inline]
pub fn id(&self) -> Option<u64> {
parse_u64(&self.get_json()["id"])
}
#[inline]
pub fn is_liked(&self) -> Option<bool> {
self.get_json()["isLiked"].as_bool()
}
#[inline]
pub fn like_count(&self) -> Option<u64> {
self.get_json()["likeCount"].as_u64()
}
#[inline]
pub fn published_datetime(&self) -> Option<&str> {
self.get_json()["publishedDatetime"].as_str()
}
#[inline]
pub fn title(&self) -> Option<&str> {
self.get_json()["title"].as_str()
}
#[inline]
pub fn updated_datetime(&self) -> Option<&str> {
self.get_json()["updatedDatetime"].as_str()
}
}
fanbox_api_test!(test_get_post_info, {
match client.get_post_info(4070200).await {
Some(data) => match data {
FanboxPost::Article(data) => {
println!("{:?}", data);
match data.next_post() {
Some(r) => {
println!("{:?}", r);
match r.get_post().await {
Some(r) => {
println!("{:?}", r);
}
None => {
panic!("Failed to get next post.")
Some(data) => {
assert_eq!(data.id(), Some(4070200));
match data {
FanboxPost::Article(data) => {
println!("{:?}", data);
match data.next_post() {
Some(r) => {
println!("{:?}", r);
match r.get_post().await {
Some(n) => {
println!("{:?}", n);
assert_eq!(n.id(), r.id());
}
None => {
panic!("Failed to get next post.")
}
}
}
None => {}
}
None => {}
}
FanboxPost::Image(data) => {
println!("{:?}", data);
}
FanboxPost::Unknown(data) => {
println!("{}", data);
}
}
FanboxPost::Unknown(data) => {
println!("{}", data);
}
},
}
None => {
panic!("Failed to get the post info(第253話 【壁紙プレゼント】ひんやりレモンころねちゃん🍋).");
}