This commit is contained in:
2022-06-29 13:36:03 +00:00
committed by GitHub
parent 157bf82199
commit 811e5a929d
3 changed files with 104 additions and 0 deletions

View File

@@ -2,3 +2,4 @@ pub mod error;
pub mod item;
pub mod item_list;
pub mod paginated_creator_posts;
pub mod plan;

87
src/fanbox/plan.rs Normal file
View File

@@ -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<u64> {
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<JsonValue> 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<FanboxPlan>,
}
impl FanboxPlanList {
/// Create a new instance.
/// * `data` - The data returned from the fanbox API.
pub fn new(data: &JsonValue) -> Result<Self, FanboxAPIError> {
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<FanboxPlan>;
fn deref(&self) -> &Self::Target {
&self.list
}
}
impl DerefMut for FanboxPlanList {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.list
}
}

View File

@@ -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<FanboxPlanList> {
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.