#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ActorDefsProfileViewBasic {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub associated: Option<crate::api::app::bsky::ActorDefsProfileAssociated>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub avatar: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub chat_disabled: Option<bool>,
pub did: crate::syntax::Did,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub display_name: Option<String>,
pub handle: crate::syntax::Handle,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub labels: Vec<crate::api::com::atproto::LabelDefsLabel>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub verification: Option<crate::api::app::bsky::ActorDefsVerificationState>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub viewer: Option<crate::api::app::bsky::ActorDefsViewerState>,
#[serde(flatten)]
pub extra: std::collections::HashMap<String, serde_json::Value>,
#[serde(skip)]
pub extra_cbor: Vec<(String, Vec<u8>)>,
}
impl ActorDefsProfileViewBasic {
pub fn to_cbor(&self) -> Result<Vec<u8>, crate::cbor::CborError> {
let mut buf = Vec::new();
self.encode_cbor(&mut buf)?;
Ok(buf)
}
pub fn encode_cbor(&self, buf: &mut Vec<u8>) -> Result<(), crate::cbor::CborError> {
if self.extra_cbor.is_empty() {
let mut count = 2u64;
if self.avatar.is_some() {
count += 1;
}
if !self.labels.is_empty() {
count += 1;
}
if self.viewer.is_some() {
count += 1;
}
if self.associated.is_some() {
count += 1;
}
if self.display_name.is_some() {
count += 1;
}
if self.chat_disabled.is_some() {
count += 1;
}
if self.verification.is_some() {
count += 1;
}
crate::cbor::Encoder::new(&mut *buf).encode_map_header(count)?;
crate::cbor::Encoder::new(&mut *buf).encode_text("did")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(self.did.as_str())?;
if self.avatar.is_some() {
crate::cbor::Encoder::new(&mut *buf).encode_text("avatar")?;
if let Some(ref val) = self.avatar {
crate::cbor::Encoder::new(&mut *buf).encode_text(val)?;
}
}
crate::cbor::Encoder::new(&mut *buf).encode_text("handle")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(self.handle.as_str())?;
if !self.labels.is_empty() {
crate::cbor::Encoder::new(&mut *buf).encode_text("labels")?;
crate::cbor::Encoder::new(&mut *buf)
.encode_array_header(self.labels.len() as u64)?;
for item in &self.labels {
item.encode_cbor(buf)?;
}
}
if self.viewer.is_some() {
crate::cbor::Encoder::new(&mut *buf).encode_text("viewer")?;
if let Some(ref val) = self.viewer {
val.encode_cbor(buf)?;
}
}
if self.associated.is_some() {
crate::cbor::Encoder::new(&mut *buf).encode_text("associated")?;
if let Some(ref val) = self.associated {
val.encode_cbor(buf)?;
}
}
if self.display_name.is_some() {
crate::cbor::Encoder::new(&mut *buf).encode_text("displayName")?;
if let Some(ref val) = self.display_name {
crate::cbor::Encoder::new(&mut *buf).encode_text(val)?;
}
}
if self.chat_disabled.is_some() {
crate::cbor::Encoder::new(&mut *buf).encode_text("chatDisabled")?;
if let Some(ref val) = self.chat_disabled {
crate::cbor::Encoder::new(&mut *buf).encode_bool(*val)?;
}
}
if self.verification.is_some() {
crate::cbor::Encoder::new(&mut *buf).encode_text("verification")?;
if let Some(ref val) = self.verification {
val.encode_cbor(buf)?;
}
}
} else {
let mut pairs: Vec<(&str, Vec<u8>)> = Vec::new();
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(self.did.as_str())?;
pairs.push(("did", vbuf));
}
if self.avatar.is_some() {
let mut vbuf = Vec::new();
if let Some(ref val) = self.avatar {
crate::cbor::Encoder::new(&mut vbuf).encode_text(val)?;
}
pairs.push(("avatar", vbuf));
}
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(self.handle.as_str())?;
pairs.push(("handle", vbuf));
}
if !self.labels.is_empty() {
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf)
.encode_array_header(self.labels.len() as u64)?;
for item in &self.labels {
item.encode_cbor(&mut vbuf)?;
}
pairs.push(("labels", vbuf));
}
if self.viewer.is_some() {
let mut vbuf = Vec::new();
if let Some(ref val) = self.viewer {
val.encode_cbor(&mut vbuf)?;
}
pairs.push(("viewer", vbuf));
}
if self.associated.is_some() {
let mut vbuf = Vec::new();
if let Some(ref val) = self.associated {
val.encode_cbor(&mut vbuf)?;
}
pairs.push(("associated", vbuf));
}
if self.display_name.is_some() {
let mut vbuf = Vec::new();
if let Some(ref val) = self.display_name {
crate::cbor::Encoder::new(&mut vbuf).encode_text(val)?;
}
pairs.push(("displayName", vbuf));
}
if self.chat_disabled.is_some() {
let mut vbuf = Vec::new();
if let Some(ref val) = self.chat_disabled {
crate::cbor::Encoder::new(&mut vbuf).encode_bool(*val)?;
}
pairs.push(("chatDisabled", vbuf));
}
if self.verification.is_some() {
let mut vbuf = Vec::new();
if let Some(ref val) = self.verification {
val.encode_cbor(&mut vbuf)?;
}
pairs.push(("verification", vbuf));
}
for (k, v) in &self.extra_cbor {
pairs.push((k.as_str(), v.clone()));
}
pairs.sort_by(|a, b| crate::cbor::cbor_key_cmp(a.0, b.0));
crate::cbor::Encoder::new(&mut *buf).encode_map_header(pairs.len() as u64)?;
for (k, v) in &pairs {
crate::cbor::Encoder::new(&mut *buf).encode_text(k)?;
buf.extend_from_slice(v);
}
}
Ok(())
}
pub fn from_cbor(data: &[u8]) -> Result<Self, crate::cbor::CborError> {
let mut decoder = crate::cbor::Decoder::new(data);
let result = Self::decode_cbor(&mut decoder)?;
if !decoder.is_empty() {
return Err(crate::cbor::CborError::InvalidCbor("trailing data".into()));
}
Ok(result)
}
pub fn decode_cbor(decoder: &mut crate::cbor::Decoder) -> Result<Self, crate::cbor::CborError> {
let val = decoder.decode()?;
let entries = match val {
crate::cbor::Value::Map(entries) => entries,
_ => return Err(crate::cbor::CborError::InvalidCbor("expected map".into())),
};
let mut field_did: Option<crate::syntax::Did> = None;
let mut field_avatar: Option<String> = None;
let mut field_handle: Option<crate::syntax::Handle> = None;
let mut field_labels: Vec<crate::api::com::atproto::LabelDefsLabel> = Vec::new();
let mut field_viewer: Option<crate::api::app::bsky::ActorDefsViewerState> = None;
let mut field_associated: Option<crate::api::app::bsky::ActorDefsProfileAssociated> = None;
let mut field_display_name: Option<String> = None;
let mut field_chat_disabled: Option<bool> = None;
let mut field_verification: Option<crate::api::app::bsky::ActorDefsVerificationState> =
None;
let mut extra_cbor: Vec<(String, Vec<u8>)> = Vec::new();
for (key, value) in entries {
match key {
"did" => {
if let crate::cbor::Value::Text(s) = value {
field_did = Some(
crate::syntax::Did::try_from(s)
.map_err(|e| crate::cbor::CborError::InvalidCbor(e.to_string()))?,
);
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
}
}
"avatar" => {
if let crate::cbor::Value::Text(s) = value {
field_avatar = Some(s.to_string());
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
}
}
"handle" => {
if let crate::cbor::Value::Text(s) = value {
field_handle = Some(
crate::syntax::Handle::try_from(s)
.map_err(|e| crate::cbor::CborError::InvalidCbor(e.to_string()))?,
);
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
}
}
"labels" => {
if let crate::cbor::Value::Array(items) = value {
for item in items {
let raw = crate::cbor::encode_value(&item)?;
let mut dec = crate::cbor::Decoder::new(&raw);
field_labels.push(
crate::api::com::atproto::LabelDefsLabel::decode_cbor(&mut dec)?,
);
}
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected array".into()));
}
}
"viewer" => {
let raw = crate::cbor::encode_value(&value)?;
let mut dec = crate::cbor::Decoder::new(&raw);
field_viewer = Some(crate::api::app::bsky::ActorDefsViewerState::decode_cbor(
&mut dec,
)?);
}
"associated" => {
let raw = crate::cbor::encode_value(&value)?;
let mut dec = crate::cbor::Decoder::new(&raw);
field_associated = Some(
crate::api::app::bsky::ActorDefsProfileAssociated::decode_cbor(&mut dec)?,
);
}
"displayName" => {
if let crate::cbor::Value::Text(s) = value {
field_display_name = Some(s.to_string());
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
}
}
"chatDisabled" => {
if let crate::cbor::Value::Bool(b) = value {
field_chat_disabled = Some(b);
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected bool".into()));
}
}
"verification" => {
let raw = crate::cbor::encode_value(&value)?;
let mut dec = crate::cbor::Decoder::new(&raw);
field_verification = Some(
crate::api::app::bsky::ActorDefsVerificationState::decode_cbor(&mut dec)?,
);
}
_ => {
let raw = crate::cbor::encode_value(&value)?;
extra_cbor.push((key.to_string(), raw));
}
}
}
Ok(ActorDefsProfileViewBasic {
did: field_did.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'did'".into())
})?,
avatar: field_avatar,
handle: field_handle.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'handle'".into())
})?,
labels: field_labels,
viewer: field_viewer,
associated: field_associated,
display_name: field_display_name,
chat_disabled: field_chat_disabled,
verification: field_verification,
extra: std::collections::HashMap::new(),
extra_cbor,
})
}
}