This commit is contained in:
2022-07-01 13:34:22 +00:00
committed by GitHub
parent 22b997e350
commit 3ed956c1a6

View File

@@ -1,9 +1,124 @@
use super::error::FanboxAPIError;
use crate::fanbox_api::FanboxClientInternal;
use crate::parser::json::parse_u64;
use json::JsonValue;
use std::convert::From;
use std::fmt::Debug;
use std::ops::Deref;
use std::ops::DerefMut;
use std::sync::Arc;
/// Profile image
pub struct FanboxProfileImage {
/// Raw data
pub data: JsonValue,
/// Fanbox API Client
client: Arc<FanboxClientInternal>,
}
impl FanboxProfileImage {
#[inline]
pub fn id(&self) -> Option<&str> {
self.data["id"].as_str()
}
#[inline]
pub fn image_url(&self) -> Option<&str> {
self.data["imageUrl"].as_str()
}
#[inline]
/// Create a new instance
pub fn new(data: &JsonValue, client: Arc<FanboxClientInternal>) -> Self {
Self {
data: data.clone(),
client,
}
}
#[inline]
pub fn thumbnail_url(&self) -> Option<&str> {
self.data["thumbnailUrl"].as_str()
}
}
impl Debug for FanboxProfileImage {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("FanboxProfileImage")
.field("id", &self.id())
.field("image_url", &self.image_url())
.field("thumbnail_url", &self.thumbnail_url())
.finish_non_exhaustive()
}
}
/// Profile item
#[derive(Debug)]
pub enum FanboxProfileItem {
/// Image
Image(FanboxProfileImage),
/// Other types
Unknown(JsonValue),
}
impl FanboxProfileItem {
#[inline]
/// Create a new instance
pub fn new(data: &JsonValue, client: Arc<FanboxClientInternal>) -> Self {
match data["type"].as_str() {
Some(s) => match s {
"image" => Self::Image(FanboxProfileImage::new(data, client)),
_ => Self::Unknown(data.clone()),
},
_ => Self::Unknown(data.clone()),
}
}
}
/// A list of [FanboxProfileItem]
pub struct FanboxProfileItems {
/// The list
list: Vec<FanboxProfileItem>,
}
impl FanboxProfileItems {
#[inline]
/// Create a new instance
pub fn new(
data: &JsonValue,
client: Arc<FanboxClientInternal>,
) -> Result<Self, FanboxAPIError> {
if data.is_array() {
let mut list = Vec::new();
for i in data.members() {
list.push(FanboxProfileItem::new(i, Arc::clone(&client)));
}
Ok(Self { list })
} else {
Err(FanboxAPIError::from("Failed to get profile items."))
}
}
}
impl Debug for FanboxProfileItems {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.list.fmt(f)
}
}
impl Deref for FanboxProfileItems {
type Target = Vec<FanboxProfileItem>;
fn deref(&self) -> &Self::Target {
&self.list
}
}
impl DerefMut for FanboxProfileItems {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.list
}
}
/// To present a creator's info
pub struct FanboxCreator {
/// Raw data
@@ -33,6 +148,7 @@ impl FanboxCreator {
self.data["hasAdultContent"].as_bool()
}
#[inline]
/// Create a new instance
pub fn new(data: &JsonValue, client: Arc<FanboxClientInternal>) -> Self {
Self {
@@ -41,6 +157,11 @@ impl FanboxCreator {
}
}
#[inline]
pub fn profile_items(&self) -> Result<FanboxProfileItems, FanboxAPIError> {
FanboxProfileItems::new(&self.data["profileItems"], Arc::clone(&self.client))
}
#[inline]
pub fn profile_links(&self) -> Option<Vec<&str>> {
let mut links = Vec::new();
@@ -83,6 +204,7 @@ impl Debug for FanboxCreator {
.field("cover_image_url", &self.cover_image_url())
.field("description", &self.description())
.field("has_adult_content", &self.has_adult_content())
.field("profile_items", &self.profile_items())
.field("profile_links", &self.profile_links())
.field("user_icon_url", &self.user_icon_url())
.field("user_id", &self.user_id())