#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct EmbedRecord {
pub record: crate::api::com::atproto::RepoStrongRef,
#[serde(flatten)]
pub extra: std::collections::HashMap<String, serde_json::Value>,
#[serde(skip)]
pub extra_cbor: Vec<(String, Vec<u8>)>,
}
impl EmbedRecord {
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 count = 1u64;
crate::cbor::Encoder::new(&mut *buf).encode_map_header(count)?;
crate::cbor::Encoder::new(&mut *buf).encode_text("record")?;
self.record.encode_cbor(buf)?;
} else {
let mut pairs: Vec<(&str, Vec<u8>)> = Vec::new();
{
let mut vbuf = Vec::new();
self.record.encode_cbor(&mut vbuf)?;
pairs.push(("record", 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_record: Option<crate::api::com::atproto::RepoStrongRef> = None;
let mut extra_cbor: Vec<(String, Vec<u8>)> = Vec::new();
for (key, value) in entries {
match key {
"record" => {
let raw = crate::cbor::encode_value(&value)?;
let mut dec = crate::cbor::Decoder::new(&raw);
field_record = Some(crate::api::com::atproto::RepoStrongRef::decode_cbor(
&mut dec,
)?);
}
_ => {
let raw = crate::cbor::encode_value(&value)?;
extra_cbor.push((key.to_string(), raw));
}
}
}
Ok(EmbedRecord {
record: field_record.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'record'".into())
})?,
extra: std::collections::HashMap::new(),
extra_cbor,
})
}
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct EmbedRecordView {
pub record: EmbedRecordViewRecordUnion,
#[serde(flatten)]
pub extra: std::collections::HashMap<String, serde_json::Value>,
#[serde(skip)]
pub extra_cbor: Vec<(String, Vec<u8>)>,
}
#[derive(Debug, Clone)]
pub enum EmbedRecordViewRecordUnion {
EmbedRecordViewRecord(Box<EmbedRecordViewRecord>),
EmbedRecordViewNotFound(Box<EmbedRecordViewNotFound>),
EmbedRecordViewBlocked(Box<EmbedRecordViewBlocked>),
EmbedRecordViewDetached(Box<EmbedRecordViewDetached>),
FeedDefsGeneratorView(Box<crate::api::app::bsky::FeedDefsGeneratorView>),
GraphDefsListView(Box<crate::api::app::bsky::GraphDefsListView>),
LabelerDefsLabelerView(Box<crate::api::app::bsky::LabelerDefsLabelerView>),
GraphDefsStarterPackViewBasic(Box<crate::api::app::bsky::GraphDefsStarterPackViewBasic>),
Unknown(crate::api::UnknownUnionVariant),
}
impl serde::Serialize for EmbedRecordViewRecordUnion {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
match self {
EmbedRecordViewRecordUnion::EmbedRecordViewRecord(inner) => {
let mut map =
serde_json::to_value(inner.as_ref()).map_err(serde::ser::Error::custom)?;
if let serde_json::Value::Object(ref mut m) = map {
m.insert(
"$type".to_string(),
serde_json::Value::String("app.bsky.embed.record#viewRecord".to_string()),
);
}
map.serialize(serializer)
}
EmbedRecordViewRecordUnion::EmbedRecordViewNotFound(inner) => {
let mut map =
serde_json::to_value(inner.as_ref()).map_err(serde::ser::Error::custom)?;
if let serde_json::Value::Object(ref mut m) = map {
m.insert(
"$type".to_string(),
serde_json::Value::String("app.bsky.embed.record#viewNotFound".to_string()),
);
}
map.serialize(serializer)
}
EmbedRecordViewRecordUnion::EmbedRecordViewBlocked(inner) => {
let mut map =
serde_json::to_value(inner.as_ref()).map_err(serde::ser::Error::custom)?;
if let serde_json::Value::Object(ref mut m) = map {
m.insert(
"$type".to_string(),
serde_json::Value::String("app.bsky.embed.record#viewBlocked".to_string()),
);
}
map.serialize(serializer)
}
EmbedRecordViewRecordUnion::EmbedRecordViewDetached(inner) => {
let mut map =
serde_json::to_value(inner.as_ref()).map_err(serde::ser::Error::custom)?;
if let serde_json::Value::Object(ref mut m) = map {
m.insert(
"$type".to_string(),
serde_json::Value::String("app.bsky.embed.record#viewDetached".to_string()),
);
}
map.serialize(serializer)
}
EmbedRecordViewRecordUnion::FeedDefsGeneratorView(inner) => {
let mut map =
serde_json::to_value(inner.as_ref()).map_err(serde::ser::Error::custom)?;
if let serde_json::Value::Object(ref mut m) = map {
m.insert(
"$type".to_string(),
serde_json::Value::String("app.bsky.feed.defs#generatorView".to_string()),
);
}
map.serialize(serializer)
}
EmbedRecordViewRecordUnion::GraphDefsListView(inner) => {
let mut map =
serde_json::to_value(inner.as_ref()).map_err(serde::ser::Error::custom)?;
if let serde_json::Value::Object(ref mut m) = map {
m.insert(
"$type".to_string(),
serde_json::Value::String("app.bsky.graph.defs#listView".to_string()),
);
}
map.serialize(serializer)
}
EmbedRecordViewRecordUnion::LabelerDefsLabelerView(inner) => {
let mut map =
serde_json::to_value(inner.as_ref()).map_err(serde::ser::Error::custom)?;
if let serde_json::Value::Object(ref mut m) = map {
m.insert(
"$type".to_string(),
serde_json::Value::String("app.bsky.labeler.defs#labelerView".to_string()),
);
}
map.serialize(serializer)
}
EmbedRecordViewRecordUnion::GraphDefsStarterPackViewBasic(inner) => {
let mut map =
serde_json::to_value(inner.as_ref()).map_err(serde::ser::Error::custom)?;
if let serde_json::Value::Object(ref mut m) = map {
m.insert(
"$type".to_string(),
serde_json::Value::String(
"app.bsky.graph.defs#starterPackViewBasic".to_string(),
),
);
}
map.serialize(serializer)
}
EmbedRecordViewRecordUnion::Unknown(v) => {
if let Some(ref j) = v.json {
j.serialize(serializer)
} else {
Err(serde::ser::Error::custom(
"no JSON data for unknown union variant",
))
}
}
}
}
}
impl<'de> serde::Deserialize<'de> for EmbedRecordViewRecordUnion {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
let value = serde_json::Value::deserialize(deserializer)?;
let type_str = value
.get("$type")
.and_then(|v| v.as_str())
.unwrap_or_default();
match type_str {
"app.bsky.embed.record#viewRecord" => {
let inner: EmbedRecordViewRecord =
serde_json::from_value(value).map_err(serde::de::Error::custom)?;
Ok(EmbedRecordViewRecordUnion::EmbedRecordViewRecord(Box::new(
inner,
)))
}
"app.bsky.embed.record#viewNotFound" => {
let inner: EmbedRecordViewNotFound =
serde_json::from_value(value).map_err(serde::de::Error::custom)?;
Ok(EmbedRecordViewRecordUnion::EmbedRecordViewNotFound(
Box::new(inner),
))
}
"app.bsky.embed.record#viewBlocked" => {
let inner: EmbedRecordViewBlocked =
serde_json::from_value(value).map_err(serde::de::Error::custom)?;
Ok(EmbedRecordViewRecordUnion::EmbedRecordViewBlocked(
Box::new(inner),
))
}
"app.bsky.embed.record#viewDetached" => {
let inner: EmbedRecordViewDetached =
serde_json::from_value(value).map_err(serde::de::Error::custom)?;
Ok(EmbedRecordViewRecordUnion::EmbedRecordViewDetached(
Box::new(inner),
))
}
"app.bsky.feed.defs#generatorView" => {
let inner: crate::api::app::bsky::FeedDefsGeneratorView =
serde_json::from_value(value).map_err(serde::de::Error::custom)?;
Ok(EmbedRecordViewRecordUnion::FeedDefsGeneratorView(Box::new(
inner,
)))
}
"app.bsky.graph.defs#listView" => {
let inner: crate::api::app::bsky::GraphDefsListView =
serde_json::from_value(value).map_err(serde::de::Error::custom)?;
Ok(EmbedRecordViewRecordUnion::GraphDefsListView(Box::new(
inner,
)))
}
"app.bsky.labeler.defs#labelerView" => {
let inner: crate::api::app::bsky::LabelerDefsLabelerView =
serde_json::from_value(value).map_err(serde::de::Error::custom)?;
Ok(EmbedRecordViewRecordUnion::LabelerDefsLabelerView(
Box::new(inner),
))
}
"app.bsky.graph.defs#starterPackViewBasic" => {
let inner: crate::api::app::bsky::GraphDefsStarterPackViewBasic =
serde_json::from_value(value).map_err(serde::de::Error::custom)?;
Ok(EmbedRecordViewRecordUnion::GraphDefsStarterPackViewBasic(
Box::new(inner),
))
}
_ => Ok(EmbedRecordViewRecordUnion::Unknown(
crate::api::UnknownUnionVariant {
r#type: type_str.to_string(),
json: Some(value),
cbor: None,
},
)),
}
}
}
impl EmbedRecordViewRecordUnion {
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> {
match self {
EmbedRecordViewRecordUnion::EmbedRecordViewRecord(inner) => inner.encode_cbor(buf),
EmbedRecordViewRecordUnion::EmbedRecordViewNotFound(inner) => inner.encode_cbor(buf),
EmbedRecordViewRecordUnion::EmbedRecordViewBlocked(inner) => inner.encode_cbor(buf),
EmbedRecordViewRecordUnion::EmbedRecordViewDetached(inner) => inner.encode_cbor(buf),
EmbedRecordViewRecordUnion::FeedDefsGeneratorView(inner) => inner.encode_cbor(buf),
EmbedRecordViewRecordUnion::GraphDefsListView(inner) => inner.encode_cbor(buf),
EmbedRecordViewRecordUnion::LabelerDefsLabelerView(inner) => inner.encode_cbor(buf),
EmbedRecordViewRecordUnion::GraphDefsStarterPackViewBasic(inner) => {
inner.encode_cbor(buf)
}
EmbedRecordViewRecordUnion::Unknown(v) => {
if let Some(ref data) = v.cbor {
buf.extend_from_slice(data);
Ok(())
} else {
Err(crate::cbor::CborError::InvalidCbor(
"no CBOR data for unknown union variant".into(),
))
}
}
}
}
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 start = decoder.position();
let val = decoder.decode()?;
let end = decoder.position();
let raw = &decoder.raw_input()[start..end];
let entries = match val {
crate::cbor::Value::Map(entries) => entries,
_ => {
return Err(crate::cbor::CborError::InvalidCbor(
"expected map for union".into(),
));
}
};
let type_str = entries
.iter()
.find(|(k, _)| *k == "$type")
.and_then(|(_, v)| match v {
crate::cbor::Value::Text(s) => Some(*s),
_ => None,
})
.unwrap_or_default();
match type_str {
"app.bsky.embed.record#viewRecord" => {
let mut dec = crate::cbor::Decoder::new(raw);
let inner = EmbedRecordViewRecord::decode_cbor(&mut dec)?;
Ok(EmbedRecordViewRecordUnion::EmbedRecordViewRecord(Box::new(
inner,
)))
}
"app.bsky.embed.record#viewNotFound" => {
let mut dec = crate::cbor::Decoder::new(raw);
let inner = EmbedRecordViewNotFound::decode_cbor(&mut dec)?;
Ok(EmbedRecordViewRecordUnion::EmbedRecordViewNotFound(
Box::new(inner),
))
}
"app.bsky.embed.record#viewBlocked" => {
let mut dec = crate::cbor::Decoder::new(raw);
let inner = EmbedRecordViewBlocked::decode_cbor(&mut dec)?;
Ok(EmbedRecordViewRecordUnion::EmbedRecordViewBlocked(
Box::new(inner),
))
}
"app.bsky.embed.record#viewDetached" => {
let mut dec = crate::cbor::Decoder::new(raw);
let inner = EmbedRecordViewDetached::decode_cbor(&mut dec)?;
Ok(EmbedRecordViewRecordUnion::EmbedRecordViewDetached(
Box::new(inner),
))
}
"app.bsky.feed.defs#generatorView" => {
let mut dec = crate::cbor::Decoder::new(raw);
let inner = crate::api::app::bsky::FeedDefsGeneratorView::decode_cbor(&mut dec)?;
Ok(EmbedRecordViewRecordUnion::FeedDefsGeneratorView(Box::new(
inner,
)))
}
"app.bsky.graph.defs#listView" => {
let mut dec = crate::cbor::Decoder::new(raw);
let inner = crate::api::app::bsky::GraphDefsListView::decode_cbor(&mut dec)?;
Ok(EmbedRecordViewRecordUnion::GraphDefsListView(Box::new(
inner,
)))
}
"app.bsky.labeler.defs#labelerView" => {
let mut dec = crate::cbor::Decoder::new(raw);
let inner = crate::api::app::bsky::LabelerDefsLabelerView::decode_cbor(&mut dec)?;
Ok(EmbedRecordViewRecordUnion::LabelerDefsLabelerView(
Box::new(inner),
))
}
"app.bsky.graph.defs#starterPackViewBasic" => {
let mut dec = crate::cbor::Decoder::new(raw);
let inner =
crate::api::app::bsky::GraphDefsStarterPackViewBasic::decode_cbor(&mut dec)?;
Ok(EmbedRecordViewRecordUnion::GraphDefsStarterPackViewBasic(
Box::new(inner),
))
}
_ => Ok(EmbedRecordViewRecordUnion::Unknown(
crate::api::UnknownUnionVariant {
r#type: type_str.to_string(),
json: None,
cbor: Some(raw.to_vec()),
},
)),
}
}
}
impl EmbedRecordView {
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 count = 1u64;
crate::cbor::Encoder::new(&mut *buf).encode_map_header(count)?;
crate::cbor::Encoder::new(&mut *buf).encode_text("record")?;
self.record.encode_cbor(buf)?;
} else {
let mut pairs: Vec<(&str, Vec<u8>)> = Vec::new();
{
let mut vbuf = Vec::new();
self.record.encode_cbor(&mut vbuf)?;
pairs.push(("record", 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_record: Option<EmbedRecordViewRecordUnion> = None;
let mut extra_cbor: Vec<(String, Vec<u8>)> = Vec::new();
for (key, value) in entries {
match key {
"record" => {
let raw = crate::cbor::encode_value(&value)?;
let mut dec = crate::cbor::Decoder::new(&raw);
field_record = Some(EmbedRecordViewRecordUnion::decode_cbor(&mut dec)?);
}
_ => {
let raw = crate::cbor::encode_value(&value)?;
extra_cbor.push((key.to_string(), raw));
}
}
}
Ok(EmbedRecordView {
record: field_record.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'record'".into())
})?,
extra: std::collections::HashMap::new(),
extra_cbor,
})
}
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct EmbedRecordViewBlocked {
pub author: crate::api::app::bsky::FeedDefsBlockedAuthor,
pub blocked: bool,
pub uri: crate::syntax::AtUri,
#[serde(flatten)]
pub extra: std::collections::HashMap<String, serde_json::Value>,
#[serde(skip)]
pub extra_cbor: Vec<(String, Vec<u8>)>,
}
impl EmbedRecordViewBlocked {
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 count = 3u64;
crate::cbor::Encoder::new(&mut *buf).encode_map_header(count)?;
crate::cbor::Encoder::new(&mut *buf).encode_text("uri")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(self.uri.as_str())?;
crate::cbor::Encoder::new(&mut *buf).encode_text("author")?;
self.author.encode_cbor(buf)?;
crate::cbor::Encoder::new(&mut *buf).encode_text("blocked")?;
crate::cbor::Encoder::new(&mut *buf).encode_bool(self.blocked)?;
} else {
let mut pairs: Vec<(&str, Vec<u8>)> = Vec::new();
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(self.uri.as_str())?;
pairs.push(("uri", vbuf));
}
{
let mut vbuf = Vec::new();
self.author.encode_cbor(&mut vbuf)?;
pairs.push(("author", vbuf));
}
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_bool(self.blocked)?;
pairs.push(("blocked", 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_uri: Option<crate::syntax::AtUri> = None;
let mut field_author: Option<crate::api::app::bsky::FeedDefsBlockedAuthor> = None;
let mut field_blocked: Option<bool> = None;
let mut extra_cbor: Vec<(String, Vec<u8>)> = Vec::new();
for (key, value) in entries {
match key {
"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()));
}
}
"author" => {
let raw = crate::cbor::encode_value(&value)?;
let mut dec = crate::cbor::Decoder::new(&raw);
field_author = Some(crate::api::app::bsky::FeedDefsBlockedAuthor::decode_cbor(
&mut dec,
)?);
}
"blocked" => {
if let crate::cbor::Value::Bool(b) = value {
field_blocked = Some(b);
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected bool".into()));
}
}
_ => {
let raw = crate::cbor::encode_value(&value)?;
extra_cbor.push((key.to_string(), raw));
}
}
}
Ok(EmbedRecordViewBlocked {
uri: field_uri.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'uri'".into())
})?,
author: field_author.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'author'".into())
})?,
blocked: field_blocked.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'blocked'".into())
})?,
extra: std::collections::HashMap::new(),
extra_cbor,
})
}
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct EmbedRecordViewDetached {
pub detached: bool,
pub uri: crate::syntax::AtUri,
#[serde(flatten)]
pub extra: std::collections::HashMap<String, serde_json::Value>,
#[serde(skip)]
pub extra_cbor: Vec<(String, Vec<u8>)>,
}
impl EmbedRecordViewDetached {
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 count = 2u64;
crate::cbor::Encoder::new(&mut *buf).encode_map_header(count)?;
crate::cbor::Encoder::new(&mut *buf).encode_text("uri")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(self.uri.as_str())?;
crate::cbor::Encoder::new(&mut *buf).encode_text("detached")?;
crate::cbor::Encoder::new(&mut *buf).encode_bool(self.detached)?;
} else {
let mut pairs: Vec<(&str, Vec<u8>)> = Vec::new();
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(self.uri.as_str())?;
pairs.push(("uri", vbuf));
}
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_bool(self.detached)?;
pairs.push(("detached", 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_uri: Option<crate::syntax::AtUri> = None;
let mut field_detached: Option<bool> = None;
let mut extra_cbor: Vec<(String, Vec<u8>)> = Vec::new();
for (key, value) in entries {
match key {
"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()));
}
}
"detached" => {
if let crate::cbor::Value::Bool(b) = value {
field_detached = Some(b);
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected bool".into()));
}
}
_ => {
let raw = crate::cbor::encode_value(&value)?;
extra_cbor.push((key.to_string(), raw));
}
}
}
Ok(EmbedRecordViewDetached {
uri: field_uri.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'uri'".into())
})?,
detached: field_detached.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'detached'".into())
})?,
extra: std::collections::HashMap::new(),
extra_cbor,
})
}
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct EmbedRecordViewNotFound {
pub not_found: bool,
pub uri: crate::syntax::AtUri,
#[serde(flatten)]
pub extra: std::collections::HashMap<String, serde_json::Value>,
#[serde(skip)]
pub extra_cbor: Vec<(String, Vec<u8>)>,
}
impl EmbedRecordViewNotFound {
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 count = 2u64;
crate::cbor::Encoder::new(&mut *buf).encode_map_header(count)?;
crate::cbor::Encoder::new(&mut *buf).encode_text("uri")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(self.uri.as_str())?;
crate::cbor::Encoder::new(&mut *buf).encode_text("notFound")?;
crate::cbor::Encoder::new(&mut *buf).encode_bool(self.not_found)?;
} else {
let mut pairs: Vec<(&str, Vec<u8>)> = Vec::new();
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(self.uri.as_str())?;
pairs.push(("uri", vbuf));
}
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_bool(self.not_found)?;
pairs.push(("notFound", 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_uri: Option<crate::syntax::AtUri> = None;
let mut field_not_found: Option<bool> = None;
let mut extra_cbor: Vec<(String, Vec<u8>)> = Vec::new();
for (key, value) in entries {
match key {
"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()));
}
}
"notFound" => {
if let crate::cbor::Value::Bool(b) = value {
field_not_found = Some(b);
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected bool".into()));
}
}
_ => {
let raw = crate::cbor::encode_value(&value)?;
extra_cbor.push((key.to_string(), raw));
}
}
}
Ok(EmbedRecordViewNotFound {
uri: field_uri.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'uri'".into())
})?,
not_found: field_not_found.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'notFound'".into())
})?,
extra: std::collections::HashMap::new(),
extra_cbor,
})
}
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct EmbedRecordViewRecord {
pub author: crate::api::app::bsky::ActorDefsProfileViewBasic,
pub cid: String,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub embeds: Vec<EmbedRecordViewRecordEmbedsUnion>,
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>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub quote_count: Option<i64>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub reply_count: Option<i64>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub repost_count: Option<i64>,
pub uri: crate::syntax::AtUri,
pub value: serde_json::Value,
#[serde(flatten)]
pub extra: std::collections::HashMap<String, serde_json::Value>,
#[serde(skip)]
pub extra_cbor: Vec<(String, Vec<u8>)>,
}
#[derive(Debug, Clone)]
pub enum EmbedRecordViewRecordEmbedsUnion {
EmbedImagesView(Box<crate::api::app::bsky::EmbedImagesView>),
EmbedVideoView(Box<crate::api::app::bsky::EmbedVideoView>),
EmbedExternalView(Box<crate::api::app::bsky::EmbedExternalView>),
EmbedRecordView(Box<EmbedRecordView>),
EmbedRecordWithMediaView(Box<crate::api::app::bsky::EmbedRecordWithMediaView>),
Unknown(crate::api::UnknownUnionVariant),
}
impl serde::Serialize for EmbedRecordViewRecordEmbedsUnion {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
match self {
EmbedRecordViewRecordEmbedsUnion::EmbedImagesView(inner) => {
let mut map =
serde_json::to_value(inner.as_ref()).map_err(serde::ser::Error::custom)?;
if let serde_json::Value::Object(ref mut m) = map {
m.insert(
"$type".to_string(),
serde_json::Value::String("app.bsky.embed.images#view".to_string()),
);
}
map.serialize(serializer)
}
EmbedRecordViewRecordEmbedsUnion::EmbedVideoView(inner) => {
let mut map =
serde_json::to_value(inner.as_ref()).map_err(serde::ser::Error::custom)?;
if let serde_json::Value::Object(ref mut m) = map {
m.insert(
"$type".to_string(),
serde_json::Value::String("app.bsky.embed.video#view".to_string()),
);
}
map.serialize(serializer)
}
EmbedRecordViewRecordEmbedsUnion::EmbedExternalView(inner) => {
let mut map =
serde_json::to_value(inner.as_ref()).map_err(serde::ser::Error::custom)?;
if let serde_json::Value::Object(ref mut m) = map {
m.insert(
"$type".to_string(),
serde_json::Value::String("app.bsky.embed.external#view".to_string()),
);
}
map.serialize(serializer)
}
EmbedRecordViewRecordEmbedsUnion::EmbedRecordView(inner) => {
let mut map =
serde_json::to_value(inner.as_ref()).map_err(serde::ser::Error::custom)?;
if let serde_json::Value::Object(ref mut m) = map {
m.insert(
"$type".to_string(),
serde_json::Value::String("app.bsky.embed.record#view".to_string()),
);
}
map.serialize(serializer)
}
EmbedRecordViewRecordEmbedsUnion::EmbedRecordWithMediaView(inner) => {
let mut map =
serde_json::to_value(inner.as_ref()).map_err(serde::ser::Error::custom)?;
if let serde_json::Value::Object(ref mut m) = map {
m.insert(
"$type".to_string(),
serde_json::Value::String(
"app.bsky.embed.recordWithMedia#view".to_string(),
),
);
}
map.serialize(serializer)
}
EmbedRecordViewRecordEmbedsUnion::Unknown(v) => {
if let Some(ref j) = v.json {
j.serialize(serializer)
} else {
Err(serde::ser::Error::custom(
"no JSON data for unknown union variant",
))
}
}
}
}
}
impl<'de> serde::Deserialize<'de> for EmbedRecordViewRecordEmbedsUnion {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
let value = serde_json::Value::deserialize(deserializer)?;
let type_str = value
.get("$type")
.and_then(|v| v.as_str())
.unwrap_or_default();
match type_str {
"app.bsky.embed.images#view" => {
let inner: crate::api::app::bsky::EmbedImagesView =
serde_json::from_value(value).map_err(serde::de::Error::custom)?;
Ok(EmbedRecordViewRecordEmbedsUnion::EmbedImagesView(Box::new(
inner,
)))
}
"app.bsky.embed.video#view" => {
let inner: crate::api::app::bsky::EmbedVideoView =
serde_json::from_value(value).map_err(serde::de::Error::custom)?;
Ok(EmbedRecordViewRecordEmbedsUnion::EmbedVideoView(Box::new(
inner,
)))
}
"app.bsky.embed.external#view" => {
let inner: crate::api::app::bsky::EmbedExternalView =
serde_json::from_value(value).map_err(serde::de::Error::custom)?;
Ok(EmbedRecordViewRecordEmbedsUnion::EmbedExternalView(
Box::new(inner),
))
}
"app.bsky.embed.record#view" => {
let inner: EmbedRecordView =
serde_json::from_value(value).map_err(serde::de::Error::custom)?;
Ok(EmbedRecordViewRecordEmbedsUnion::EmbedRecordView(Box::new(
inner,
)))
}
"app.bsky.embed.recordWithMedia#view" => {
let inner: crate::api::app::bsky::EmbedRecordWithMediaView =
serde_json::from_value(value).map_err(serde::de::Error::custom)?;
Ok(EmbedRecordViewRecordEmbedsUnion::EmbedRecordWithMediaView(
Box::new(inner),
))
}
_ => Ok(EmbedRecordViewRecordEmbedsUnion::Unknown(
crate::api::UnknownUnionVariant {
r#type: type_str.to_string(),
json: Some(value),
cbor: None,
},
)),
}
}
}
impl EmbedRecordViewRecordEmbedsUnion {
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> {
match self {
EmbedRecordViewRecordEmbedsUnion::EmbedImagesView(inner) => inner.encode_cbor(buf),
EmbedRecordViewRecordEmbedsUnion::EmbedVideoView(inner) => inner.encode_cbor(buf),
EmbedRecordViewRecordEmbedsUnion::EmbedExternalView(inner) => inner.encode_cbor(buf),
EmbedRecordViewRecordEmbedsUnion::EmbedRecordView(inner) => inner.encode_cbor(buf),
EmbedRecordViewRecordEmbedsUnion::EmbedRecordWithMediaView(inner) => {
inner.encode_cbor(buf)
}
EmbedRecordViewRecordEmbedsUnion::Unknown(v) => {
if let Some(ref data) = v.cbor {
buf.extend_from_slice(data);
Ok(())
} else {
Err(crate::cbor::CborError::InvalidCbor(
"no CBOR data for unknown union variant".into(),
))
}
}
}
}
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 start = decoder.position();
let val = decoder.decode()?;
let end = decoder.position();
let raw = &decoder.raw_input()[start..end];
let entries = match val {
crate::cbor::Value::Map(entries) => entries,
_ => {
return Err(crate::cbor::CborError::InvalidCbor(
"expected map for union".into(),
));
}
};
let type_str = entries
.iter()
.find(|(k, _)| *k == "$type")
.and_then(|(_, v)| match v {
crate::cbor::Value::Text(s) => Some(*s),
_ => None,
})
.unwrap_or_default();
match type_str {
"app.bsky.embed.images#view" => {
let mut dec = crate::cbor::Decoder::new(raw);
let inner = crate::api::app::bsky::EmbedImagesView::decode_cbor(&mut dec)?;
Ok(EmbedRecordViewRecordEmbedsUnion::EmbedImagesView(Box::new(
inner,
)))
}
"app.bsky.embed.video#view" => {
let mut dec = crate::cbor::Decoder::new(raw);
let inner = crate::api::app::bsky::EmbedVideoView::decode_cbor(&mut dec)?;
Ok(EmbedRecordViewRecordEmbedsUnion::EmbedVideoView(Box::new(
inner,
)))
}
"app.bsky.embed.external#view" => {
let mut dec = crate::cbor::Decoder::new(raw);
let inner = crate::api::app::bsky::EmbedExternalView::decode_cbor(&mut dec)?;
Ok(EmbedRecordViewRecordEmbedsUnion::EmbedExternalView(
Box::new(inner),
))
}
"app.bsky.embed.record#view" => {
let mut dec = crate::cbor::Decoder::new(raw);
let inner = EmbedRecordView::decode_cbor(&mut dec)?;
Ok(EmbedRecordViewRecordEmbedsUnion::EmbedRecordView(Box::new(
inner,
)))
}
"app.bsky.embed.recordWithMedia#view" => {
let mut dec = crate::cbor::Decoder::new(raw);
let inner = crate::api::app::bsky::EmbedRecordWithMediaView::decode_cbor(&mut dec)?;
Ok(EmbedRecordViewRecordEmbedsUnion::EmbedRecordWithMediaView(
Box::new(inner),
))
}
_ => Ok(EmbedRecordViewRecordEmbedsUnion::Unknown(
crate::api::UnknownUnionVariant {
r#type: type_str.to_string(),
json: None,
cbor: Some(raw.to_vec()),
},
)),
}
}
}
impl EmbedRecordViewRecord {
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.embeds.is_empty() {
count += 1;
}
if !self.labels.is_empty() {
count += 1;
}
if self.like_count.is_some() {
count += 1;
}
if self.quote_count.is_some() {
count += 1;
}
if self.reply_count.is_some() {
count += 1;
}
if self.repost_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())?;
crate::cbor::Encoder::new(&mut *buf).encode_text("author")?;
self.author.encode_cbor(buf)?;
if !self.embeds.is_empty() {
crate::cbor::Encoder::new(&mut *buf).encode_text("embeds")?;
crate::cbor::Encoder::new(&mut *buf)
.encode_array_header(self.embeds.len() as u64)?;
for item in &self.embeds {
item.encode_cbor(buf)?;
}
}
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)?;
}
}
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.quote_count.is_some() {
crate::cbor::Encoder::new(&mut *buf).encode_text("quoteCount")?;
if let Some(ref val) = self.quote_count {
crate::cbor::Encoder::new(&mut *buf).encode_i64(*val)?;
}
}
if self.reply_count.is_some() {
crate::cbor::Encoder::new(&mut *buf).encode_text("replyCount")?;
if let Some(ref val) = self.reply_count {
crate::cbor::Encoder::new(&mut *buf).encode_i64(*val)?;
}
}
if self.repost_count.is_some() {
crate::cbor::Encoder::new(&mut *buf).encode_text("repostCount")?;
if let Some(ref val) = self.repost_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));
}
{
let mut vbuf = Vec::new();
self.author.encode_cbor(&mut vbuf)?;
pairs.push(("author", vbuf));
}
if !self.embeds.is_empty() {
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf)
.encode_array_header(self.embeds.len() as u64)?;
for item in &self.embeds {
item.encode_cbor(&mut vbuf)?;
}
pairs.push(("embeds", 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));
}
{
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.quote_count.is_some() {
let mut vbuf = Vec::new();
if let Some(ref val) = self.quote_count {
crate::cbor::Encoder::new(&mut vbuf).encode_i64(*val)?;
}
pairs.push(("quoteCount", vbuf));
}
if self.reply_count.is_some() {
let mut vbuf = Vec::new();
if let Some(ref val) = self.reply_count {
crate::cbor::Encoder::new(&mut vbuf).encode_i64(*val)?;
}
pairs.push(("replyCount", vbuf));
}
if self.repost_count.is_some() {
let mut vbuf = Vec::new();
if let Some(ref val) = self.repost_count {
crate::cbor::Encoder::new(&mut vbuf).encode_i64(*val)?;
}
pairs.push(("repostCount", 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_author: Option<crate::api::app::bsky::ActorDefsProfileViewBasic> = None;
let mut field_embeds: Vec<EmbedRecordViewRecordEmbedsUnion> = Vec::new();
let mut field_labels: Vec<crate::api::com::atproto::LabelDefsLabel> = Vec::new();
let mut field_indexed_at: Option<crate::syntax::Datetime> = None;
let mut field_like_count: Option<i64> = None;
let mut field_quote_count: Option<i64> = None;
let mut field_reply_count: Option<i64> = None;
let mut field_repost_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()));
}
}
"author" => {
let raw = crate::cbor::encode_value(&value)?;
let mut dec = crate::cbor::Decoder::new(&raw);
field_author = Some(
crate::api::app::bsky::ActorDefsProfileViewBasic::decode_cbor(&mut dec)?,
);
}
"embeds" => {
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_embeds
.push(EmbedRecordViewRecordEmbedsUnion::decode_cbor(&mut dec)?);
}
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected array".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()));
}
}
"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(),
));
}
},
"quoteCount" => match value {
crate::cbor::Value::Unsigned(n) => {
field_quote_count = Some(n as i64);
}
crate::cbor::Value::Signed(n) => {
field_quote_count = Some(n);
}
_ => {
return Err(crate::cbor::CborError::InvalidCbor(
"expected integer".into(),
));
}
},
"replyCount" => match value {
crate::cbor::Value::Unsigned(n) => {
field_reply_count = Some(n as i64);
}
crate::cbor::Value::Signed(n) => {
field_reply_count = Some(n);
}
_ => {
return Err(crate::cbor::CborError::InvalidCbor(
"expected integer".into(),
));
}
},
"repostCount" => match value {
crate::cbor::Value::Unsigned(n) => {
field_repost_count = Some(n as i64);
}
crate::cbor::Value::Signed(n) => {
field_repost_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(EmbedRecordViewRecord {
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())
})?,
value: Default::default(),
author: field_author.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'author'".into())
})?,
embeds: field_embeds,
labels: field_labels,
indexed_at: field_indexed_at.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'indexedAt'".into())
})?,
like_count: field_like_count,
quote_count: field_quote_count,
reply_count: field_reply_count,
repost_count: field_repost_count,
extra: std::collections::HashMap::new(),
extra_cbor,
})
}
}