#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct FeedPostEntity {
pub index: FeedPostTextSlice,
pub r#type: String,
pub value: String,
#[serde(flatten)]
pub extra: std::collections::HashMap<String, serde_json::Value>,
#[serde(skip)]
pub extra_cbor: Vec<(String, Vec<u8>)>,
}
impl FeedPostEntity {
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("type")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(&self.r#type)?;
crate::cbor::Encoder::new(&mut *buf).encode_text("index")?;
self.index.encode_cbor(buf)?;
crate::cbor::Encoder::new(&mut *buf).encode_text("value")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(&self.value)?;
} else {
let mut pairs: Vec<(&str, Vec<u8>)> = Vec::new();
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(&self.r#type)?;
pairs.push(("type", vbuf));
}
{
let mut vbuf = Vec::new();
self.index.encode_cbor(&mut vbuf)?;
pairs.push(("index", vbuf));
}
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(&self.value)?;
pairs.push(("value", 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_type: Option<String> = None;
let mut field_index: Option<FeedPostTextSlice> = None;
let mut field_value: Option<String> = None;
let mut extra_cbor: Vec<(String, Vec<u8>)> = Vec::new();
for (key, value) in entries {
match key {
"type" => {
if let crate::cbor::Value::Text(s) = value {
field_type = Some(s.to_string());
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
}
}
"index" => {
let raw = crate::cbor::encode_value(&value)?;
let mut dec = crate::cbor::Decoder::new(&raw);
field_index = Some(FeedPostTextSlice::decode_cbor(&mut dec)?);
}
"value" => {
if let crate::cbor::Value::Text(s) = value {
field_value = Some(s.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(FeedPostEntity {
r#type: field_type.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'type'".into())
})?,
index: field_index.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'index'".into())
})?,
value: field_value.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'value'".into())
})?,
extra: std::collections::HashMap::new(),
extra_cbor,
})
}
}
pub const NSID_FEED_POST: &str = "app.bsky.feed.post";
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct FeedPost {
pub created_at: crate::syntax::Datetime,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub embed: Option<FeedPostEmbedUnion>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub entities: Vec<FeedPostEntity>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub facets: Vec<crate::api::app::bsky::RichtextFacet>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub labels: Option<FeedPostLabelsUnion>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub langs: Vec<crate::syntax::Language>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub reply: Option<FeedPostReplyRef>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub tags: Vec<String>,
pub text: String,
#[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 FeedPostEmbedUnion {
EmbedImages(Box<crate::api::app::bsky::EmbedImages>),
EmbedVideo(Box<crate::api::app::bsky::EmbedVideo>),
EmbedExternal(Box<crate::api::app::bsky::EmbedExternal>),
EmbedRecord(Box<crate::api::app::bsky::EmbedRecord>),
EmbedRecordWithMedia(Box<crate::api::app::bsky::EmbedRecordWithMedia>),
Unknown(crate::api::UnknownUnionVariant),
}
impl serde::Serialize for FeedPostEmbedUnion {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
match self {
FeedPostEmbedUnion::EmbedImages(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".to_string()),
);
}
map.serialize(serializer)
}
FeedPostEmbedUnion::EmbedVideo(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".to_string()),
);
}
map.serialize(serializer)
}
FeedPostEmbedUnion::EmbedExternal(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".to_string()),
);
}
map.serialize(serializer)
}
FeedPostEmbedUnion::EmbedRecord(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".to_string()),
);
}
map.serialize(serializer)
}
FeedPostEmbedUnion::EmbedRecordWithMedia(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".to_string()),
);
}
map.serialize(serializer)
}
FeedPostEmbedUnion::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 FeedPostEmbedUnion {
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" => {
let inner: crate::api::app::bsky::EmbedImages =
serde_json::from_value(value).map_err(serde::de::Error::custom)?;
Ok(FeedPostEmbedUnion::EmbedImages(Box::new(inner)))
}
"app.bsky.embed.video" => {
let inner: crate::api::app::bsky::EmbedVideo =
serde_json::from_value(value).map_err(serde::de::Error::custom)?;
Ok(FeedPostEmbedUnion::EmbedVideo(Box::new(inner)))
}
"app.bsky.embed.external" => {
let inner: crate::api::app::bsky::EmbedExternal =
serde_json::from_value(value).map_err(serde::de::Error::custom)?;
Ok(FeedPostEmbedUnion::EmbedExternal(Box::new(inner)))
}
"app.bsky.embed.record" => {
let inner: crate::api::app::bsky::EmbedRecord =
serde_json::from_value(value).map_err(serde::de::Error::custom)?;
Ok(FeedPostEmbedUnion::EmbedRecord(Box::new(inner)))
}
"app.bsky.embed.recordWithMedia" => {
let inner: crate::api::app::bsky::EmbedRecordWithMedia =
serde_json::from_value(value).map_err(serde::de::Error::custom)?;
Ok(FeedPostEmbedUnion::EmbedRecordWithMedia(Box::new(inner)))
}
_ => Ok(FeedPostEmbedUnion::Unknown(
crate::api::UnknownUnionVariant {
r#type: type_str.to_string(),
json: Some(value),
cbor: None,
},
)),
}
}
}
impl FeedPostEmbedUnion {
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 {
FeedPostEmbedUnion::EmbedImages(inner) => inner.encode_cbor(buf),
FeedPostEmbedUnion::EmbedVideo(inner) => inner.encode_cbor(buf),
FeedPostEmbedUnion::EmbedExternal(inner) => inner.encode_cbor(buf),
FeedPostEmbedUnion::EmbedRecord(inner) => inner.encode_cbor(buf),
FeedPostEmbedUnion::EmbedRecordWithMedia(inner) => inner.encode_cbor(buf),
FeedPostEmbedUnion::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" => {
let mut dec = crate::cbor::Decoder::new(raw);
let inner = crate::api::app::bsky::EmbedImages::decode_cbor(&mut dec)?;
Ok(FeedPostEmbedUnion::EmbedImages(Box::new(inner)))
}
"app.bsky.embed.video" => {
let mut dec = crate::cbor::Decoder::new(raw);
let inner = crate::api::app::bsky::EmbedVideo::decode_cbor(&mut dec)?;
Ok(FeedPostEmbedUnion::EmbedVideo(Box::new(inner)))
}
"app.bsky.embed.external" => {
let mut dec = crate::cbor::Decoder::new(raw);
let inner = crate::api::app::bsky::EmbedExternal::decode_cbor(&mut dec)?;
Ok(FeedPostEmbedUnion::EmbedExternal(Box::new(inner)))
}
"app.bsky.embed.record" => {
let mut dec = crate::cbor::Decoder::new(raw);
let inner = crate::api::app::bsky::EmbedRecord::decode_cbor(&mut dec)?;
Ok(FeedPostEmbedUnion::EmbedRecord(Box::new(inner)))
}
"app.bsky.embed.recordWithMedia" => {
let mut dec = crate::cbor::Decoder::new(raw);
let inner = crate::api::app::bsky::EmbedRecordWithMedia::decode_cbor(&mut dec)?;
Ok(FeedPostEmbedUnion::EmbedRecordWithMedia(Box::new(inner)))
}
_ => Ok(FeedPostEmbedUnion::Unknown(
crate::api::UnknownUnionVariant {
r#type: type_str.to_string(),
json: None,
cbor: Some(raw.to_vec()),
},
)),
}
}
}
#[derive(Debug, Clone)]
pub enum FeedPostLabelsUnion {
LabelDefsSelfLabels(Box<crate::api::com::atproto::LabelDefsSelfLabels>),
Unknown(crate::api::UnknownUnionVariant),
}
impl serde::Serialize for FeedPostLabelsUnion {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
match self {
FeedPostLabelsUnion::LabelDefsSelfLabels(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("com.atproto.label.defs#selfLabels".to_string()),
);
}
map.serialize(serializer)
}
FeedPostLabelsUnion::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 FeedPostLabelsUnion {
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 {
"com.atproto.label.defs#selfLabels" => {
let inner: crate::api::com::atproto::LabelDefsSelfLabels =
serde_json::from_value(value).map_err(serde::de::Error::custom)?;
Ok(FeedPostLabelsUnion::LabelDefsSelfLabels(Box::new(inner)))
}
_ => Ok(FeedPostLabelsUnion::Unknown(
crate::api::UnknownUnionVariant {
r#type: type_str.to_string(),
json: Some(value),
cbor: None,
},
)),
}
}
}
impl FeedPostLabelsUnion {
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 {
FeedPostLabelsUnion::LabelDefsSelfLabels(inner) => inner.encode_cbor(buf),
FeedPostLabelsUnion::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 {
"com.atproto.label.defs#selfLabels" => {
let mut dec = crate::cbor::Decoder::new(raw);
let inner = crate::api::com::atproto::LabelDefsSelfLabels::decode_cbor(&mut dec)?;
Ok(FeedPostLabelsUnion::LabelDefsSelfLabels(Box::new(inner)))
}
_ => Ok(FeedPostLabelsUnion::Unknown(
crate::api::UnknownUnionVariant {
r#type: type_str.to_string(),
json: None,
cbor: Some(raw.to_vec()),
},
)),
}
}
}
impl FeedPost {
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.tags.is_empty() {
count += 1;
}
if self.embed.is_some() {
count += 1;
}
if !self.langs.is_empty() {
count += 1;
}
if self.reply.is_some() {
count += 1;
}
if !self.facets.is_empty() {
count += 1;
}
if self.labels.is_some() {
count += 1;
}
if !self.entities.is_empty() {
count += 1;
}
crate::cbor::Encoder::new(&mut *buf).encode_map_header(count)?;
if !self.tags.is_empty() {
crate::cbor::Encoder::new(&mut *buf).encode_text("tags")?;
crate::cbor::Encoder::new(&mut *buf).encode_array_header(self.tags.len() as u64)?;
for item in &self.tags {
crate::cbor::Encoder::new(&mut *buf).encode_text(item)?;
}
}
crate::cbor::Encoder::new(&mut *buf).encode_text("text")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(&self.text)?;
if self.embed.is_some() {
crate::cbor::Encoder::new(&mut *buf).encode_text("embed")?;
if let Some(ref val) = self.embed {
val.encode_cbor(buf)?;
}
}
if !self.langs.is_empty() {
crate::cbor::Encoder::new(&mut *buf).encode_text("langs")?;
crate::cbor::Encoder::new(&mut *buf).encode_array_header(self.langs.len() as u64)?;
for item in &self.langs {
crate::cbor::Encoder::new(&mut *buf).encode_text(item.as_str())?;
}
}
if self.reply.is_some() {
crate::cbor::Encoder::new(&mut *buf).encode_text("reply")?;
if let Some(ref val) = self.reply {
val.encode_cbor(buf)?;
}
}
if !self.facets.is_empty() {
crate::cbor::Encoder::new(&mut *buf).encode_text("facets")?;
crate::cbor::Encoder::new(&mut *buf)
.encode_array_header(self.facets.len() as u64)?;
for item in &self.facets {
item.encode_cbor(buf)?;
}
}
if self.labels.is_some() {
crate::cbor::Encoder::new(&mut *buf).encode_text("labels")?;
if let Some(ref val) = self.labels {
val.encode_cbor(buf)?;
}
}
if !self.entities.is_empty() {
crate::cbor::Encoder::new(&mut *buf).encode_text("entities")?;
crate::cbor::Encoder::new(&mut *buf)
.encode_array_header(self.entities.len() as u64)?;
for item in &self.entities {
item.encode_cbor(buf)?;
}
}
crate::cbor::Encoder::new(&mut *buf).encode_text("createdAt")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(self.created_at.as_str())?;
} else {
let mut pairs: Vec<(&str, Vec<u8>)> = Vec::new();
if !self.tags.is_empty() {
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_array_header(self.tags.len() as u64)?;
for item in &self.tags {
crate::cbor::Encoder::new(&mut vbuf).encode_text(item)?;
}
pairs.push(("tags", vbuf));
}
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(&self.text)?;
pairs.push(("text", vbuf));
}
if self.embed.is_some() {
let mut vbuf = Vec::new();
if let Some(ref val) = self.embed {
val.encode_cbor(&mut vbuf)?;
}
pairs.push(("embed", vbuf));
}
if !self.langs.is_empty() {
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_array_header(self.langs.len() as u64)?;
for item in &self.langs {
crate::cbor::Encoder::new(&mut vbuf).encode_text(item.as_str())?;
}
pairs.push(("langs", vbuf));
}
if self.reply.is_some() {
let mut vbuf = Vec::new();
if let Some(ref val) = self.reply {
val.encode_cbor(&mut vbuf)?;
}
pairs.push(("reply", vbuf));
}
if !self.facets.is_empty() {
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf)
.encode_array_header(self.facets.len() as u64)?;
for item in &self.facets {
item.encode_cbor(&mut vbuf)?;
}
pairs.push(("facets", vbuf));
}
if self.labels.is_some() {
let mut vbuf = Vec::new();
if let Some(ref val) = self.labels {
val.encode_cbor(&mut vbuf)?;
}
pairs.push(("labels", vbuf));
}
if !self.entities.is_empty() {
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf)
.encode_array_header(self.entities.len() as u64)?;
for item in &self.entities {
item.encode_cbor(&mut vbuf)?;
}
pairs.push(("entities", vbuf));
}
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(self.created_at.as_str())?;
pairs.push(("createdAt", 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_tags: Vec<String> = Vec::new();
let mut field_text: Option<String> = None;
let mut field_embed: Option<FeedPostEmbedUnion> = None;
let mut field_langs: Vec<crate::syntax::Language> = Vec::new();
let mut field_reply: Option<FeedPostReplyRef> = None;
let mut field_facets: Vec<crate::api::app::bsky::RichtextFacet> = Vec::new();
let mut field_labels: Option<FeedPostLabelsUnion> = None;
let mut field_entities: Vec<FeedPostEntity> = Vec::new();
let mut field_created_at: Option<crate::syntax::Datetime> = None;
let mut extra_cbor: Vec<(String, Vec<u8>)> = Vec::new();
for (key, value) in entries {
match key {
"tags" => {
if let crate::cbor::Value::Array(items) = value {
for item in items {
if let crate::cbor::Value::Text(s) = item {
field_tags.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()));
}
}
"text" => {
if let crate::cbor::Value::Text(s) = value {
field_text = Some(s.to_string());
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
}
}
"embed" => {
let raw = crate::cbor::encode_value(&value)?;
let mut dec = crate::cbor::Decoder::new(&raw);
field_embed = Some(FeedPostEmbedUnion::decode_cbor(&mut dec)?);
}
"langs" => {
if let crate::cbor::Value::Array(items) = value {
for item in items {
if let crate::cbor::Value::Text(s) = item {
field_langs.push(crate::syntax::Language::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()));
}
}
"reply" => {
let raw = crate::cbor::encode_value(&value)?;
let mut dec = crate::cbor::Decoder::new(&raw);
field_reply = Some(FeedPostReplyRef::decode_cbor(&mut dec)?);
}
"facets" => {
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_facets
.push(crate::api::app::bsky::RichtextFacet::decode_cbor(&mut dec)?);
}
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected array".into()));
}
}
"labels" => {
let raw = crate::cbor::encode_value(&value)?;
let mut dec = crate::cbor::Decoder::new(&raw);
field_labels = Some(FeedPostLabelsUnion::decode_cbor(&mut dec)?);
}
"entities" => {
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_entities.push(FeedPostEntity::decode_cbor(&mut dec)?);
}
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected array".into()));
}
}
"createdAt" => {
if let crate::cbor::Value::Text(s) = value {
field_created_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()));
}
}
_ => {
let raw = crate::cbor::encode_value(&value)?;
extra_cbor.push((key.to_string(), raw));
}
}
}
Ok(FeedPost {
tags: field_tags,
text: field_text.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'text'".into())
})?,
embed: field_embed,
langs: field_langs,
reply: field_reply,
facets: field_facets,
labels: field_labels,
entities: field_entities,
created_at: field_created_at.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'createdAt'".into())
})?,
extra: std::collections::HashMap::new(),
extra_cbor,
})
}
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct FeedPostReplyRef {
pub parent: crate::api::com::atproto::RepoStrongRef,
pub root: 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 FeedPostReplyRef {
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("root")?;
self.root.encode_cbor(buf)?;
crate::cbor::Encoder::new(&mut *buf).encode_text("parent")?;
self.parent.encode_cbor(buf)?;
} else {
let mut pairs: Vec<(&str, Vec<u8>)> = Vec::new();
{
let mut vbuf = Vec::new();
self.root.encode_cbor(&mut vbuf)?;
pairs.push(("root", vbuf));
}
{
let mut vbuf = Vec::new();
self.parent.encode_cbor(&mut vbuf)?;
pairs.push(("parent", 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_root: Option<crate::api::com::atproto::RepoStrongRef> = None;
let mut field_parent: Option<crate::api::com::atproto::RepoStrongRef> = None;
let mut extra_cbor: Vec<(String, Vec<u8>)> = Vec::new();
for (key, value) in entries {
match key {
"root" => {
let raw = crate::cbor::encode_value(&value)?;
let mut dec = crate::cbor::Decoder::new(&raw);
field_root = Some(crate::api::com::atproto::RepoStrongRef::decode_cbor(
&mut dec,
)?);
}
"parent" => {
let raw = crate::cbor::encode_value(&value)?;
let mut dec = crate::cbor::Decoder::new(&raw);
field_parent = 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(FeedPostReplyRef {
root: field_root.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'root'".into())
})?,
parent: field_parent.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'parent'".into())
})?,
extra: std::collections::HashMap::new(),
extra_cbor,
})
}
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct FeedPostTextSlice {
pub end: i64,
pub start: i64,
#[serde(flatten)]
pub extra: std::collections::HashMap<String, serde_json::Value>,
#[serde(skip)]
pub extra_cbor: Vec<(String, Vec<u8>)>,
}
impl FeedPostTextSlice {
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("end")?;
crate::cbor::Encoder::new(&mut *buf).encode_i64(self.end)?;
crate::cbor::Encoder::new(&mut *buf).encode_text("start")?;
crate::cbor::Encoder::new(&mut *buf).encode_i64(self.start)?;
} else {
let mut pairs: Vec<(&str, Vec<u8>)> = Vec::new();
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_i64(self.end)?;
pairs.push(("end", vbuf));
}
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_i64(self.start)?;
pairs.push(("start", 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_end: Option<i64> = None;
let mut field_start: Option<i64> = None;
let mut extra_cbor: Vec<(String, Vec<u8>)> = Vec::new();
for (key, value) in entries {
match key {
"end" => match value {
crate::cbor::Value::Unsigned(n) => {
field_end = Some(n as i64);
}
crate::cbor::Value::Signed(n) => {
field_end = Some(n);
}
_ => {
return Err(crate::cbor::CborError::InvalidCbor(
"expected integer".into(),
));
}
},
"start" => match value {
crate::cbor::Value::Unsigned(n) => {
field_start = Some(n as i64);
}
crate::cbor::Value::Signed(n) => {
field_start = 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(FeedPostTextSlice {
end: field_end.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'end'".into())
})?,
start: field_start.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'start'".into())
})?,
extra: std::collections::HashMap::new(),
extra_cbor,
})
}
}