From 811e5a929d7b000cb19c844ee47494a4bb2c58d5 Mon Sep 17 00:00:00 2001 From: lifegpc Date: Wed, 29 Jun 2022 13:36:03 +0000 Subject: [PATCH] Update --- src/fanbox/mod.rs | 1 + src/fanbox/plan.rs | 87 ++++++++++++++++++++++++++++++++++++++++++++++ src/fanbox_api.rs | 16 +++++++++ 3 files changed, 104 insertions(+) create mode 100644 src/fanbox/plan.rs diff --git a/src/fanbox/mod.rs b/src/fanbox/mod.rs index f06b845..245251b 100644 --- a/src/fanbox/mod.rs +++ b/src/fanbox/mod.rs @@ -2,3 +2,4 @@ pub mod error; pub mod item; pub mod item_list; pub mod paginated_creator_posts; +pub mod plan; diff --git a/src/fanbox/plan.rs b/src/fanbox/plan.rs new file mode 100644 index 0000000..1e7f578 --- /dev/null +++ b/src/fanbox/plan.rs @@ -0,0 +1,87 @@ +use super::error::FanboxAPIError; +use crate::gettext; +use crate::parser::json::parse_u64; +use json::JsonValue; +use std::fmt::Debug; +use std::ops::Deref; +use std::ops::DerefMut; + +/// Fanbox plan +pub struct FanboxPlan { + /// Raw data + pub data: JsonValue, +} + +impl FanboxPlan { + pub fn id(&self) -> Option { + parse_u64(&self.data["id"]) + } +} + +impl Debug for FanboxPlan { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("FanboxPlan") + .field("id", &self.id()) + .finish_non_exhaustive() + } +} + +impl From for FanboxPlan { + fn from(data: JsonValue) -> Self { + Self { data } + } +} + +impl From<&JsonValue> for FanboxPlan { + fn from(data: &JsonValue) -> Self { + Self { data: data.clone() } + } +} + +/// A list of fanbox plans +pub struct FanboxPlanList { + /// List + list: Vec, +} + +impl FanboxPlanList { + /// Create a new instance. + /// * `data` - The data returned from the fanbox API. + pub fn new(data: &JsonValue) -> Result { + if data.is_array() { + let mut list = Vec::new(); + for i in data.members() { + if !i.is_object() { + return Err(FanboxAPIError::from(gettext( + "Failed to parse fanbox plan list.", + ))); + } + list.push(FanboxPlan::from(i)); + } + Ok(Self { list }) + } else { + Err(FanboxAPIError::from(gettext( + "Failed to parse fanbox plan list.", + ))) + } + } +} + +impl Debug for FanboxPlanList { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_list().entries(self.list.iter()).finish() + } +} + +impl Deref for FanboxPlanList { + type Target = Vec; + fn deref(&self) -> &Self::Target { + &self.list + } +} + +impl DerefMut for FanboxPlanList { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.list + } +} diff --git a/src/fanbox_api.rs b/src/fanbox_api.rs index 6f1c8c3..d7ca556 100644 --- a/src/fanbox_api.rs +++ b/src/fanbox_api.rs @@ -3,6 +3,7 @@ use crate::ext::replace::ReplaceWith2; use crate::ext::rw_lock::GetRwLock; use crate::fanbox::item_list::FanboxItemList; use crate::fanbox::paginated_creator_posts::PaginatedCreatorPosts; +use crate::fanbox::plan::FanboxPlanList; use crate::gettext; use crate::opthelper::get_helper; use crate::parser::metadata::MetaDataParser; @@ -284,6 +285,21 @@ impl FanboxClient { } } + #[allow(dead_code)] + /// List all supporting plans. + pub async fn list_supporting_plan(&self) -> Option { + match self.client.list_supporting_plan().await { + Some(s) => match FanboxPlanList::new(&s["body"]) { + Ok(item) => Some(item), + Err(e) => { + println!("{}", e); + None + } + }, + None => None, + } + } + #[allow(dead_code)] /// List supported creators' posts. /// * `limit` - The max count. 10 is used on Fanbox website.