#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct LabelerDefsLabelerPolicies {
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub label_value_definitions: Vec<crate::api::com::atproto::LabelDefsLabelValueDefinition>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub label_values: Vec<crate::api::com::atproto::LabelDefsLabelValue>,
#[serde(flatten)]
pub extra: std::collections::HashMap<String, serde_json::Value>,
#[serde(skip)]
pub extra_cbor: Vec<(String, Vec<u8>)>,
}
impl LabelerDefsLabelerPolicies {
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 = 1u64;
if !self.label_value_definitions.is_empty() {
count += 1;
}
crate::cbor::Encoder::new(&mut *buf).encode_map_header(count)?;
crate::cbor::Encoder::new(&mut *buf).encode_text("labelValues")?;
crate::cbor::Encoder::new(&mut *buf)
.encode_array_header(self.label_values.len() as u64)?;
for item in &self.label_values {
crate::cbor::Encoder::new(&mut *buf).encode_text(item)?;
}
if !self.label_value_definitions.is_empty() {
crate::cbor::Encoder::new(&mut *buf).encode_text("labelValueDefinitions")?;
crate::cbor::Encoder::new(&mut *buf)
.encode_array_header(self.label_value_definitions.len() as u64)?;
for item in &self.label_value_definitions {
item.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_array_header(self.label_values.len() as u64)?;
for item in &self.label_values {
crate::cbor::Encoder::new(&mut vbuf).encode_text(item)?;
}
pairs.push(("labelValues", vbuf));
}
if !self.label_value_definitions.is_empty() {
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf)
.encode_array_header(self.label_value_definitions.len() as u64)?;
for item in &self.label_value_definitions {
item.encode_cbor(&mut vbuf)?;
}
pairs.push(("labelValueDefinitions", 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_label_values: Vec<crate::api::com::atproto::LabelDefsLabelValue> = Vec::new();
let mut field_label_value_definitions: Vec<
crate::api::com::atproto::LabelDefsLabelValueDefinition,
> = Vec::new();
let mut extra_cbor: Vec<(String, Vec<u8>)> = Vec::new();
for (key, value) in entries {
match key {
"labelValues" => {
if let crate::cbor::Value::Array(items) = value {
for item in items {
if let crate::cbor::Value::Text(s) = item {
field_label_values.push(s.to_string());
} else {
return Err(crate::cbor::CborError::InvalidCbor(
"expected text in array".into(),
));
}
}
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected array".into()));
}
}
"labelValueDefinitions" => {
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_label_value_definitions.push(crate::api::com::atproto::LabelDefsLabelValueDefinition::decode_cbor(&mut dec)?);
}
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected array".into()));
}
}
_ => {
let raw = crate::cbor::encode_value(&value)?;
extra_cbor.push((key.to_string(), raw));
}
}
}
Ok(LabelerDefsLabelerPolicies {
label_values: field_label_values,
label_value_definitions: field_label_value_definitions,
extra: std::collections::HashMap::new(),
extra_cbor,
})
}
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct LabelerDefsLabelerView {
pub cid: String,
pub creator: crate::api::app::bsky::ActorDefsProfileView,
pub indexed_at: crate::syntax::Datetime,
#[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 like_count: Option<i64>,
pub uri: crate::syntax::AtUri,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub viewer: Option<LabelerDefsLabelerViewerState>,
#[serde(flatten)]
pub extra: std::collections::HashMap<String, serde_json::Value>,
#[serde(skip)]
pub extra_cbor: Vec<(String, Vec<u8>)>,
}
impl LabelerDefsLabelerView {
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 = 4u64;
if !self.labels.is_empty() {
count += 1;
}
if self.viewer.is_some() {
count += 1;
}
if self.like_count.is_some() {
count += 1;
}
crate::cbor::Encoder::new(&mut *buf).encode_map_header(count)?;
crate::cbor::Encoder::new(&mut *buf).encode_text("cid")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(&self.cid)?;
crate::cbor::Encoder::new(&mut *buf).encode_text("uri")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(self.uri.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)?;
}
}
crate::cbor::Encoder::new(&mut *buf).encode_text("creator")?;
self.creator.encode_cbor(buf)?;
crate::cbor::Encoder::new(&mut *buf).encode_text("indexedAt")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(self.indexed_at.as_str())?;
if self.like_count.is_some() {
crate::cbor::Encoder::new(&mut *buf).encode_text("likeCount")?;
if let Some(ref val) = self.like_count {
crate::cbor::Encoder::new(&mut *buf).encode_i64(*val)?;
}
}
} else {
let mut pairs: Vec<(&str, Vec<u8>)> = Vec::new();
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(&self.cid)?;
pairs.push(("cid", vbuf));
}
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(self.uri.as_str())?;
pairs.push(("uri", 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));
}
{
let mut vbuf = Vec::new();
self.creator.encode_cbor(&mut vbuf)?;
pairs.push(("creator", vbuf));
}
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(self.indexed_at.as_str())?;
pairs.push(("indexedAt", vbuf));
}
if self.like_count.is_some() {
let mut vbuf = Vec::new();
if let Some(ref val) = self.like_count {
crate::cbor::Encoder::new(&mut vbuf).encode_i64(*val)?;
}
pairs.push(("likeCount", 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_cid: Option<String> = None;
let mut field_uri: Option<crate::syntax::AtUri> = None;
let mut field_labels: Vec<crate::api::com::atproto::LabelDefsLabel> = Vec::new();
let mut field_viewer: Option<LabelerDefsLabelerViewerState> = None;
let mut field_creator: Option<crate::api::app::bsky::ActorDefsProfileView> = None;
let mut field_indexed_at: Option<crate::syntax::Datetime> = None;
let mut field_like_count: Option<i64> = None;
let mut extra_cbor: Vec<(String, Vec<u8>)> = Vec::new();
for (key, value) in entries {
match key {
"cid" => {
if let crate::cbor::Value::Text(s) = value {
field_cid = Some(s.to_string());
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
}
}
"uri" => {
if let crate::cbor::Value::Text(s) = value {
field_uri = Some(
crate::syntax::AtUri::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(LabelerDefsLabelerViewerState::decode_cbor(&mut dec)?);
}
"creator" => {
let raw = crate::cbor::encode_value(&value)?;
let mut dec = crate::cbor::Decoder::new(&raw);
field_creator = Some(crate::api::app::bsky::ActorDefsProfileView::decode_cbor(
&mut dec,
)?);
}
"indexedAt" => {
if let crate::cbor::Value::Text(s) = value {
field_indexed_at = Some(
crate::syntax::Datetime::try_from(s)
.map_err(|e| crate::cbor::CborError::InvalidCbor(e.to_string()))?,
);
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
}
}
"likeCount" => match value {
crate::cbor::Value::Unsigned(n) => {
field_like_count = Some(n as i64);
}
crate::cbor::Value::Signed(n) => {
field_like_count = Some(n);
}
_ => {
return Err(crate::cbor::CborError::InvalidCbor(
"expected integer".into(),
));
}
},
_ => {
let raw = crate::cbor::encode_value(&value)?;
extra_cbor.push((key.to_string(), raw));
}
}
}
Ok(LabelerDefsLabelerView {
cid: field_cid.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'cid'".into())
})?,
uri: field_uri.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'uri'".into())
})?,
labels: field_labels,
viewer: field_viewer,
creator: field_creator.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'creator'".into())
})?,
indexed_at: field_indexed_at.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'indexedAt'".into())
})?,
like_count: field_like_count,
extra: std::collections::HashMap::new(),
extra_cbor,
})
}
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct LabelerDefsLabelerViewDetailed {
pub cid: String,
pub creator: crate::api::app::bsky::ActorDefsProfileView,
pub indexed_at: crate::syntax::Datetime,
#[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 like_count: Option<i64>,
pub policies: LabelerDefsLabelerPolicies,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub reason_types: Vec<crate::api::com::atproto::ModerationDefsReasonType>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub subject_collections: Vec<crate::syntax::Nsid>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub subject_types: Vec<crate::api::com::atproto::ModerationDefsSubjectType>,
pub uri: crate::syntax::AtUri,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub viewer: Option<LabelerDefsLabelerViewerState>,
#[serde(flatten)]
pub extra: std::collections::HashMap<String, serde_json::Value>,
#[serde(skip)]
pub extra_cbor: Vec<(String, Vec<u8>)>,
}
impl LabelerDefsLabelerViewDetailed {
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 = 5u64;
if !self.labels.is_empty() {
count += 1;
}
if self.viewer.is_some() {
count += 1;
}
if self.like_count.is_some() {
count += 1;
}
if !self.reason_types.is_empty() {
count += 1;
}
if !self.subject_types.is_empty() {
count += 1;
}
if !self.subject_collections.is_empty() {
count += 1;
}
crate::cbor::Encoder::new(&mut *buf).encode_map_header(count)?;
crate::cbor::Encoder::new(&mut *buf).encode_text("cid")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(&self.cid)?;
crate::cbor::Encoder::new(&mut *buf).encode_text("uri")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(self.uri.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)?;
}
}
crate::cbor::Encoder::new(&mut *buf).encode_text("creator")?;
self.creator.encode_cbor(buf)?;
crate::cbor::Encoder::new(&mut *buf).encode_text("policies")?;
self.policies.encode_cbor(buf)?;
crate::cbor::Encoder::new(&mut *buf).encode_text("indexedAt")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(self.indexed_at.as_str())?;
if self.like_count.is_some() {
crate::cbor::Encoder::new(&mut *buf).encode_text("likeCount")?;
if let Some(ref val) = self.like_count {
crate::cbor::Encoder::new(&mut *buf).encode_i64(*val)?;
}
}
if !self.reason_types.is_empty() {
crate::cbor::Encoder::new(&mut *buf).encode_text("reasonTypes")?;
crate::cbor::Encoder::new(&mut *buf)
.encode_array_header(self.reason_types.len() as u64)?;
for item in &self.reason_types {
crate::cbor::Encoder::new(&mut *buf).encode_text(item)?;
}
}
if !self.subject_types.is_empty() {
crate::cbor::Encoder::new(&mut *buf).encode_text("subjectTypes")?;
crate::cbor::Encoder::new(&mut *buf)
.encode_array_header(self.subject_types.len() as u64)?;
for item in &self.subject_types {
crate::cbor::Encoder::new(&mut *buf).encode_text(item)?;
}
}
if !self.subject_collections.is_empty() {
crate::cbor::Encoder::new(&mut *buf).encode_text("subjectCollections")?;
crate::cbor::Encoder::new(&mut *buf)
.encode_array_header(self.subject_collections.len() as u64)?;
for item in &self.subject_collections {
crate::cbor::Encoder::new(&mut *buf).encode_text(item.as_str())?;
}
}
} else {
let mut pairs: Vec<(&str, Vec<u8>)> = Vec::new();
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(&self.cid)?;
pairs.push(("cid", vbuf));
}
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(self.uri.as_str())?;
pairs.push(("uri", 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));
}
{
let mut vbuf = Vec::new();
self.creator.encode_cbor(&mut vbuf)?;
pairs.push(("creator", vbuf));
}
{
let mut vbuf = Vec::new();
self.policies.encode_cbor(&mut vbuf)?;
pairs.push(("policies", vbuf));
}
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(self.indexed_at.as_str())?;
pairs.push(("indexedAt", vbuf));
}
if self.like_count.is_some() {
let mut vbuf = Vec::new();
if let Some(ref val) = self.like_count {
crate::cbor::Encoder::new(&mut vbuf).encode_i64(*val)?;
}
pairs.push(("likeCount", vbuf));
}
if !self.reason_types.is_empty() {
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf)
.encode_array_header(self.reason_types.len() as u64)?;
for item in &self.reason_types {
crate::cbor::Encoder::new(&mut vbuf).encode_text(item)?;
}
pairs.push(("reasonTypes", vbuf));
}
if !self.subject_types.is_empty() {
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf)
.encode_array_header(self.subject_types.len() as u64)?;
for item in &self.subject_types {
crate::cbor::Encoder::new(&mut vbuf).encode_text(item)?;
}
pairs.push(("subjectTypes", vbuf));
}
if !self.subject_collections.is_empty() {
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf)
.encode_array_header(self.subject_collections.len() as u64)?;
for item in &self.subject_collections {
crate::cbor::Encoder::new(&mut vbuf).encode_text(item.as_str())?;
}
pairs.push(("subjectCollections", 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_cid: Option<String> = None;
let mut field_uri: Option<crate::syntax::AtUri> = None;
let mut field_labels: Vec<crate::api::com::atproto::LabelDefsLabel> = Vec::new();
let mut field_viewer: Option<LabelerDefsLabelerViewerState> = None;
let mut field_creator: Option<crate::api::app::bsky::ActorDefsProfileView> = None;
let mut field_policies: Option<LabelerDefsLabelerPolicies> = None;
let mut field_indexed_at: Option<crate::syntax::Datetime> = None;
let mut field_like_count: Option<i64> = None;
let mut field_reason_types: Vec<crate::api::com::atproto::ModerationDefsReasonType> =
Vec::new();
let mut field_subject_types: Vec<crate::api::com::atproto::ModerationDefsSubjectType> =
Vec::new();
let mut field_subject_collections: Vec<crate::syntax::Nsid> = Vec::new();
let mut extra_cbor: Vec<(String, Vec<u8>)> = Vec::new();
for (key, value) in entries {
match key {
"cid" => {
if let crate::cbor::Value::Text(s) = value {
field_cid = Some(s.to_string());
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
}
}
"uri" => {
if let crate::cbor::Value::Text(s) = value {
field_uri = Some(
crate::syntax::AtUri::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(LabelerDefsLabelerViewerState::decode_cbor(&mut dec)?);
}
"creator" => {
let raw = crate::cbor::encode_value(&value)?;
let mut dec = crate::cbor::Decoder::new(&raw);
field_creator = Some(crate::api::app::bsky::ActorDefsProfileView::decode_cbor(
&mut dec,
)?);
}
"policies" => {
let raw = crate::cbor::encode_value(&value)?;
let mut dec = crate::cbor::Decoder::new(&raw);
field_policies = Some(LabelerDefsLabelerPolicies::decode_cbor(&mut dec)?);
}
"indexedAt" => {
if let crate::cbor::Value::Text(s) = value {
field_indexed_at = Some(
crate::syntax::Datetime::try_from(s)
.map_err(|e| crate::cbor::CborError::InvalidCbor(e.to_string()))?,
);
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
}
}
"likeCount" => match value {
crate::cbor::Value::Unsigned(n) => {
field_like_count = Some(n as i64);
}
crate::cbor::Value::Signed(n) => {
field_like_count = Some(n);
}
_ => {
return Err(crate::cbor::CborError::InvalidCbor(
"expected integer".into(),
));
}
},
"reasonTypes" => {
if let crate::cbor::Value::Array(items) = value {
for item in items {
if let crate::cbor::Value::Text(s) = item {
field_reason_types.push(s.to_string());
} else {
return Err(crate::cbor::CborError::InvalidCbor(
"expected text in array".into(),
));
}
}
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected array".into()));
}
}
"subjectTypes" => {
if let crate::cbor::Value::Array(items) = value {
for item in items {
if let crate::cbor::Value::Text(s) = item {
field_subject_types.push(s.to_string());
} else {
return Err(crate::cbor::CborError::InvalidCbor(
"expected text in array".into(),
));
}
}
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected array".into()));
}
}
"subjectCollections" => {
if let crate::cbor::Value::Array(items) = value {
for item in items {
if let crate::cbor::Value::Text(s) = item {
field_subject_collections.push(
crate::syntax::Nsid::try_from(s).map_err(|e| {
crate::cbor::CborError::InvalidCbor(e.to_string())
})?,
);
} else {
return Err(crate::cbor::CborError::InvalidCbor(
"expected text in array".into(),
));
}
}
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected array".into()));
}
}
_ => {
let raw = crate::cbor::encode_value(&value)?;
extra_cbor.push((key.to_string(), raw));
}
}
}
Ok(LabelerDefsLabelerViewDetailed {
cid: field_cid.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'cid'".into())
})?,
uri: field_uri.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'uri'".into())
})?,
labels: field_labels,
viewer: field_viewer,
creator: field_creator.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'creator'".into())
})?,
policies: field_policies.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'policies'".into())
})?,
indexed_at: field_indexed_at.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'indexedAt'".into())
})?,
like_count: field_like_count,
reason_types: field_reason_types,
subject_types: field_subject_types,
subject_collections: field_subject_collections,
extra: std::collections::HashMap::new(),
extra_cbor,
})
}
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct LabelerDefsLabelerViewerState {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub like: Option<crate::syntax::AtUri>,
#[serde(flatten)]
pub extra: std::collections::HashMap<String, serde_json::Value>,
#[serde(skip)]
pub extra_cbor: Vec<(String, Vec<u8>)>,
}
impl LabelerDefsLabelerViewerState {
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 = 0u64;
if self.like.is_some() {
count += 1;
}
crate::cbor::Encoder::new(&mut *buf).encode_map_header(count)?;
if self.like.is_some() {
crate::cbor::Encoder::new(&mut *buf).encode_text("like")?;
if let Some(ref val) = self.like {
crate::cbor::Encoder::new(&mut *buf).encode_text(val.as_str())?;
}
}
} else {
let mut pairs: Vec<(&str, Vec<u8>)> = Vec::new();
if self.like.is_some() {
let mut vbuf = Vec::new();
if let Some(ref val) = self.like {
crate::cbor::Encoder::new(&mut vbuf).encode_text(val.as_str())?;
}
pairs.push(("like", 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_like: Option<crate::syntax::AtUri> = None;
let mut extra_cbor: Vec<(String, Vec<u8>)> = Vec::new();
for (key, value) in entries {
match key {
"like" => {
if let crate::cbor::Value::Text(s) = value {
field_like = Some(
crate::syntax::AtUri::try_from(s)
.map_err(|e| crate::cbor::CborError::InvalidCbor(e.to_string()))?,
);
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
}
}
_ => {
let raw = crate::cbor::encode_value(&value)?;
extra_cbor.push((key.to_string(), raw));
}
}
}
Ok(LabelerDefsLabelerViewerState {
like: field_like,
extra: std::collections::HashMap::new(),
extra_cbor,
})
}
}