use serde::{Deserialize, Serialize};
use time::{serde::timestamp, OffsetDateTime};
use url::Url;
use crate::model::common::{AccountID, ProExpiration, Username};
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[serde(deny_unknown_fields)]
pub struct Account {
pub id: AccountID,
pub url: Username,
pub bio: Option<String>,
pub avatar: Option<Url>,
pub avatar_name: Option<String>,
pub cover: Option<Url>,
pub cover_name: Option<String>,
pub reputation: f64,
pub reputation_name: String,
#[serde(with = "timestamp")]
pub created: OffsetDateTime,
pub pro_expiration: ProExpiration,
pub is_blocked: bool,
pub user_follow: UserFollow,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[serde(deny_unknown_fields)]
pub struct UserFollow {
pub status: bool,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[serde(deny_unknown_fields)]
pub struct BlockedStatus {
pub blocked: bool,
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[serde(deny_unknown_fields)]
pub struct AccountBlocks {
pub items: Vec<BlockedAccount>,
pub next: Option<serde_json::Value>,
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[serde(deny_unknown_fields)]
pub struct BlockedAccount {
pub url: Username,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[serde(deny_unknown_fields)]
pub struct BlockResponse {
pub blocked: bool,
}
#[cfg(test)]
mod test {
use std::error::Error;
use crate::model::{account::Account, basic::Basic};
#[test]
fn test_deserialize_account_local() -> Result<(), Box<dyn Error>> {
let data = r#"{"data":{"id":48437714,"url":"ghostinspector","bio":null,"avatar":"https://imgur.com/user/ghostinspector/avatar?maxwidth=290","avatar_name":"default/G","cover":"https://imgur.com/user/ghostinspector/cover?maxwidth=2560","cover_name":"default/1-space","reputation":-252,"reputation_name":"Neutral","created":1481839668,"pro_expiration":false,"user_follow":{"status":false},"is_blocked":false},"success":true,"status":200}"#;
let account = serde_json::from_str::<Basic<Account>>(data)?;
println!("{:#?}", account);
Ok(())
}
}