1use serde::{
4 de::{self, Deserializer, Unexpected},
5 Deserialize, Serialize,
6};
7use std::{fmt::Display, path::PathBuf};
8use time::{serde::iso8601, OffsetDateTime};
9
10#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
12pub struct Account {
13 pub acct: String,
15 pub avatar: String,
17 pub avatar_static: String,
19 #[serde(with = "iso8601")]
21 pub created_at: OffsetDateTime,
22 pub display_name: String,
24 pub followers_count: u64,
26 pub following_count: u64,
28 pub header: String,
30 pub header_static: String,
32 pub id: AccountId,
34 pub locked: bool,
37 pub note: String,
39 pub statuses_count: u64,
41 pub url: String,
43 pub username: String,
45 pub source: Option<Source>,
48 pub moved: Option<Box<Account>>,
51 pub fields: Option<Vec<MetadataField>>,
53 pub bot: Option<bool>,
55}
56
57#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
59#[serde(transparent)]
60pub struct AccountId(String);
61
62impl AsRef<str> for AccountId {
63 fn as_ref(&self) -> &str {
64 &self.0
65 }
66}
67
68impl AccountId {
69 pub fn new(value: impl Into<String>) -> Self {
70 Self(value.into())
71 }
72}
73
74impl Display for AccountId {
75 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
76 write!(f, "{}", self.0)
77 }
78}
79
80static_assertions::assert_not_impl_any!(
81 AccountId: PartialEq<crate::attachment::AttachmentId>,
82 PartialEq<crate::filter::FilterId>,
83 PartialEq<crate::list::ListId>,
84 PartialEq<crate::mention::MentionId>,
85 PartialEq<crate::notification::NotificationId>,
86 PartialEq<crate::relationship::RelationshipId>,
87 PartialEq<crate::push::SubscriptionId>,
88 PartialEq<crate::report::ReportId>,
89 PartialEq<crate::status::StatusId>,
90);
91
92#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
94pub struct MetadataField {
95 pub name: String,
97 pub value: String,
99}
100
101impl MetadataField {
102 pub fn new(name: &str, value: &str) -> MetadataField {
103 MetadataField {
104 name: name.into(),
105 value: value.into(),
106 }
107 }
108}
109
110#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
112pub struct Source {
113 privacy: Option<crate::visibility::Visibility>,
114 #[serde(deserialize_with = "string_or_bool")]
115 sensitive: bool,
116 note: Option<String>,
117 fields: Option<Vec<MetadataField>>,
118}
119
120fn string_or_bool<'de, D: Deserializer<'de>>(val: D) -> ::std::result::Result<bool, D::Error> {
121 #[derive(Clone, Debug, Deserialize, PartialEq, Eq)]
122 #[serde(untagged)]
123 pub enum BoolOrString {
124 Bool(bool),
125 Str(String),
126 }
127
128 Ok(match BoolOrString::deserialize(val)? {
129 BoolOrString::Bool(b) => b,
130 BoolOrString::Str(ref s) => {
131 if s == "true" {
132 true
133 } else if s == "false" {
134 false
135 } else {
136 return Err(de::Error::invalid_value(
137 Unexpected::Str(s),
138 &"true or false",
139 ));
140 }
141 }
142 })
143}
144
145#[derive(Debug, Default, Clone, Serialize, PartialEq, Eq)]
146pub struct UpdateSource {
147 #[serde(skip_serializing_if = "Option::is_none")]
148 pub privacy: Option<crate::visibility::Visibility>,
149 #[serde(skip_serializing_if = "Option::is_none")]
150 pub sensitive: Option<bool>,
151}
152
153#[derive(Debug, Default, Serialize, PartialEq, Eq)]
154pub struct Credentials {
155 #[serde(skip_serializing_if = "Option::is_none")]
156 pub display_name: Option<String>,
157 #[serde(skip_serializing_if = "Option::is_none")]
158 pub note: Option<String>,
159 #[serde(skip_serializing_if = "Option::is_none")]
160 pub avatar: Option<PathBuf>,
161 #[serde(skip_serializing_if = "Option::is_none")]
162 pub header: Option<PathBuf>,
163 #[serde(skip_serializing_if = "Option::is_none")]
164 pub source: Option<UpdateSource>,
165 #[serde(serialize_with = "fields_attributes_ser::ser")]
166 pub fields_attributes: Vec<MetadataField>,
167}
168
169mod fields_attributes_ser {
170 use super::*;
171 use serde::ser::{SerializeMap, Serializer};
172 pub fn ser<S>(attrs: &Vec<MetadataField>, serializer: S) -> Result<S::Ok, S::Error>
173 where
174 S: Serializer,
175 {
176 let mut map = serializer.serialize_map(Some(attrs.len()))?;
177 for (i, field) in attrs.iter().enumerate() {
178 map.serialize_entry(&i, &field)?;
179 }
180 map.end()
181 }
182}