#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ConvoDefsConvoView {
pub id: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub last_message: Option<ConvoDefsConvoViewLastMessageUnion>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub last_reaction: Option<ConvoDefsConvoViewLastReactionUnion>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub members: Vec<crate::api::chat::bsky::ActorDefsProfileViewBasic>,
pub muted: bool,
pub rev: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub status: Option<String>,
pub unread_count: i64,
#[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 ConvoDefsConvoViewLastMessageUnion {
ConvoDefsMessageView(Box<ConvoDefsMessageView>),
ConvoDefsDeletedMessageView(Box<ConvoDefsDeletedMessageView>),
Unknown(crate::api::UnknownUnionVariant),
}
impl serde::Serialize for ConvoDefsConvoViewLastMessageUnion {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
match self {
ConvoDefsConvoViewLastMessageUnion::ConvoDefsMessageView(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("chat.bsky.convo.defs#messageView".to_string()),
);
}
map.serialize(serializer)
}
ConvoDefsConvoViewLastMessageUnion::ConvoDefsDeletedMessageView(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(
"chat.bsky.convo.defs#deletedMessageView".to_string(),
),
);
}
map.serialize(serializer)
}
ConvoDefsConvoViewLastMessageUnion::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 ConvoDefsConvoViewLastMessageUnion {
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 {
"chat.bsky.convo.defs#messageView" => {
let inner: ConvoDefsMessageView =
serde_json::from_value(value).map_err(serde::de::Error::custom)?;
Ok(ConvoDefsConvoViewLastMessageUnion::ConvoDefsMessageView(
Box::new(inner),
))
}
"chat.bsky.convo.defs#deletedMessageView" => {
let inner: ConvoDefsDeletedMessageView =
serde_json::from_value(value).map_err(serde::de::Error::custom)?;
Ok(
ConvoDefsConvoViewLastMessageUnion::ConvoDefsDeletedMessageView(Box::new(
inner,
)),
)
}
_ => Ok(ConvoDefsConvoViewLastMessageUnion::Unknown(
crate::api::UnknownUnionVariant {
r#type: type_str.to_string(),
json: Some(value),
cbor: None,
},
)),
}
}
}
impl ConvoDefsConvoViewLastMessageUnion {
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 {
ConvoDefsConvoViewLastMessageUnion::ConvoDefsMessageView(inner) => {
inner.encode_cbor(buf)
}
ConvoDefsConvoViewLastMessageUnion::ConvoDefsDeletedMessageView(inner) => {
inner.encode_cbor(buf)
}
ConvoDefsConvoViewLastMessageUnion::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 {
"chat.bsky.convo.defs#messageView" => {
let mut dec = crate::cbor::Decoder::new(raw);
let inner = ConvoDefsMessageView::decode_cbor(&mut dec)?;
Ok(ConvoDefsConvoViewLastMessageUnion::ConvoDefsMessageView(
Box::new(inner),
))
}
"chat.bsky.convo.defs#deletedMessageView" => {
let mut dec = crate::cbor::Decoder::new(raw);
let inner = ConvoDefsDeletedMessageView::decode_cbor(&mut dec)?;
Ok(
ConvoDefsConvoViewLastMessageUnion::ConvoDefsDeletedMessageView(Box::new(
inner,
)),
)
}
_ => Ok(ConvoDefsConvoViewLastMessageUnion::Unknown(
crate::api::UnknownUnionVariant {
r#type: type_str.to_string(),
json: None,
cbor: Some(raw.to_vec()),
},
)),
}
}
}
#[derive(Debug, Clone)]
pub enum ConvoDefsConvoViewLastReactionUnion {
ConvoDefsMessageAndReactionView(Box<ConvoDefsMessageAndReactionView>),
Unknown(crate::api::UnknownUnionVariant),
}
impl serde::Serialize for ConvoDefsConvoViewLastReactionUnion {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
match self {
ConvoDefsConvoViewLastReactionUnion::ConvoDefsMessageAndReactionView(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(
"chat.bsky.convo.defs#messageAndReactionView".to_string(),
),
);
}
map.serialize(serializer)
}
ConvoDefsConvoViewLastReactionUnion::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 ConvoDefsConvoViewLastReactionUnion {
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 {
"chat.bsky.convo.defs#messageAndReactionView" => {
let inner: ConvoDefsMessageAndReactionView =
serde_json::from_value(value).map_err(serde::de::Error::custom)?;
Ok(
ConvoDefsConvoViewLastReactionUnion::ConvoDefsMessageAndReactionView(Box::new(
inner,
)),
)
}
_ => Ok(ConvoDefsConvoViewLastReactionUnion::Unknown(
crate::api::UnknownUnionVariant {
r#type: type_str.to_string(),
json: Some(value),
cbor: None,
},
)),
}
}
}
impl ConvoDefsConvoViewLastReactionUnion {
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 {
ConvoDefsConvoViewLastReactionUnion::ConvoDefsMessageAndReactionView(inner) => {
inner.encode_cbor(buf)
}
ConvoDefsConvoViewLastReactionUnion::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 {
"chat.bsky.convo.defs#messageAndReactionView" => {
let mut dec = crate::cbor::Decoder::new(raw);
let inner = ConvoDefsMessageAndReactionView::decode_cbor(&mut dec)?;
Ok(
ConvoDefsConvoViewLastReactionUnion::ConvoDefsMessageAndReactionView(Box::new(
inner,
)),
)
}
_ => Ok(ConvoDefsConvoViewLastReactionUnion::Unknown(
crate::api::UnknownUnionVariant {
r#type: type_str.to_string(),
json: None,
cbor: Some(raw.to_vec()),
},
)),
}
}
}
impl ConvoDefsConvoView {
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.status.is_some() {
count += 1;
}
if self.last_message.is_some() {
count += 1;
}
if self.last_reaction.is_some() {
count += 1;
}
crate::cbor::Encoder::new(&mut *buf).encode_map_header(count)?;
crate::cbor::Encoder::new(&mut *buf).encode_text("id")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(&self.id)?;
crate::cbor::Encoder::new(&mut *buf).encode_text("rev")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(&self.rev)?;
crate::cbor::Encoder::new(&mut *buf).encode_text("muted")?;
crate::cbor::Encoder::new(&mut *buf).encode_bool(self.muted)?;
if self.status.is_some() {
crate::cbor::Encoder::new(&mut *buf).encode_text("status")?;
if let Some(ref val) = self.status {
crate::cbor::Encoder::new(&mut *buf).encode_text(val)?;
}
}
crate::cbor::Encoder::new(&mut *buf).encode_text("members")?;
crate::cbor::Encoder::new(&mut *buf).encode_array_header(self.members.len() as u64)?;
for item in &self.members {
item.encode_cbor(buf)?;
}
if self.last_message.is_some() {
crate::cbor::Encoder::new(&mut *buf).encode_text("lastMessage")?;
if let Some(ref val) = self.last_message {
val.encode_cbor(buf)?;
}
}
crate::cbor::Encoder::new(&mut *buf).encode_text("unreadCount")?;
crate::cbor::Encoder::new(&mut *buf).encode_i64(self.unread_count)?;
if self.last_reaction.is_some() {
crate::cbor::Encoder::new(&mut *buf).encode_text("lastReaction")?;
if let Some(ref val) = self.last_reaction {
val.encode_cbor(buf)?;
}
}
} else {
let mut pairs: Vec<(&str, Vec<u8>)> = Vec::new();
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(&self.id)?;
pairs.push(("id", vbuf));
}
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(&self.rev)?;
pairs.push(("rev", vbuf));
}
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_bool(self.muted)?;
pairs.push(("muted", vbuf));
}
if self.status.is_some() {
let mut vbuf = Vec::new();
if let Some(ref val) = self.status {
crate::cbor::Encoder::new(&mut vbuf).encode_text(val)?;
}
pairs.push(("status", vbuf));
}
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf)
.encode_array_header(self.members.len() as u64)?;
for item in &self.members {
item.encode_cbor(&mut vbuf)?;
}
pairs.push(("members", vbuf));
}
if self.last_message.is_some() {
let mut vbuf = Vec::new();
if let Some(ref val) = self.last_message {
val.encode_cbor(&mut vbuf)?;
}
pairs.push(("lastMessage", vbuf));
}
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_i64(self.unread_count)?;
pairs.push(("unreadCount", vbuf));
}
if self.last_reaction.is_some() {
let mut vbuf = Vec::new();
if let Some(ref val) = self.last_reaction {
val.encode_cbor(&mut vbuf)?;
}
pairs.push(("lastReaction", 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_id: Option<String> = None;
let mut field_rev: Option<String> = None;
let mut field_muted: Option<bool> = None;
let mut field_status: Option<String> = None;
let mut field_members: Vec<crate::api::chat::bsky::ActorDefsProfileViewBasic> = Vec::new();
let mut field_last_message: Option<ConvoDefsConvoViewLastMessageUnion> = None;
let mut field_unread_count: Option<i64> = None;
let mut field_last_reaction: Option<ConvoDefsConvoViewLastReactionUnion> = None;
let mut extra_cbor: Vec<(String, Vec<u8>)> = Vec::new();
for (key, value) in entries {
match key {
"id" => {
if let crate::cbor::Value::Text(s) = value {
field_id = Some(s.to_string());
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
}
}
"rev" => {
if let crate::cbor::Value::Text(s) = value {
field_rev = Some(s.to_string());
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
}
}
"muted" => {
if let crate::cbor::Value::Bool(b) = value {
field_muted = Some(b);
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected bool".into()));
}
}
"status" => {
if let crate::cbor::Value::Text(s) = value {
field_status = Some(s.to_string());
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
}
}
"members" => {
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_members.push(
crate::api::chat::bsky::ActorDefsProfileViewBasic::decode_cbor(
&mut dec,
)?,
);
}
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected array".into()));
}
}
"lastMessage" => {
let raw = crate::cbor::encode_value(&value)?;
let mut dec = crate::cbor::Decoder::new(&raw);
field_last_message =
Some(ConvoDefsConvoViewLastMessageUnion::decode_cbor(&mut dec)?);
}
"unreadCount" => match value {
crate::cbor::Value::Unsigned(n) => {
field_unread_count = Some(n as i64);
}
crate::cbor::Value::Signed(n) => {
field_unread_count = Some(n);
}
_ => {
return Err(crate::cbor::CborError::InvalidCbor(
"expected integer".into(),
));
}
},
"lastReaction" => {
let raw = crate::cbor::encode_value(&value)?;
let mut dec = crate::cbor::Decoder::new(&raw);
field_last_reaction =
Some(ConvoDefsConvoViewLastReactionUnion::decode_cbor(&mut dec)?);
}
_ => {
let raw = crate::cbor::encode_value(&value)?;
extra_cbor.push((key.to_string(), raw));
}
}
}
Ok(ConvoDefsConvoView {
id: field_id.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'id'".into())
})?,
rev: field_rev.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'rev'".into())
})?,
muted: field_muted.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'muted'".into())
})?,
status: field_status,
members: field_members,
last_message: field_last_message,
unread_count: field_unread_count.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'unreadCount'".into())
})?,
last_reaction: field_last_reaction,
extra: std::collections::HashMap::new(),
extra_cbor,
})
}
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ConvoDefsDeletedMessageView {
pub id: String,
pub rev: String,
pub sender: ConvoDefsMessageViewSender,
pub sent_at: crate::syntax::Datetime,
#[serde(flatten)]
pub extra: std::collections::HashMap<String, serde_json::Value>,
#[serde(skip)]
pub extra_cbor: Vec<(String, Vec<u8>)>,
}
impl ConvoDefsDeletedMessageView {
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 = 4u64;
crate::cbor::Encoder::new(&mut *buf).encode_map_header(count)?;
crate::cbor::Encoder::new(&mut *buf).encode_text("id")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(&self.id)?;
crate::cbor::Encoder::new(&mut *buf).encode_text("rev")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(&self.rev)?;
crate::cbor::Encoder::new(&mut *buf).encode_text("sender")?;
self.sender.encode_cbor(buf)?;
crate::cbor::Encoder::new(&mut *buf).encode_text("sentAt")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(self.sent_at.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.id)?;
pairs.push(("id", vbuf));
}
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(&self.rev)?;
pairs.push(("rev", vbuf));
}
{
let mut vbuf = Vec::new();
self.sender.encode_cbor(&mut vbuf)?;
pairs.push(("sender", vbuf));
}
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(self.sent_at.as_str())?;
pairs.push(("sentAt", 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_id: Option<String> = None;
let mut field_rev: Option<String> = None;
let mut field_sender: Option<ConvoDefsMessageViewSender> = None;
let mut field_sent_at: Option<crate::syntax::Datetime> = None;
let mut extra_cbor: Vec<(String, Vec<u8>)> = Vec::new();
for (key, value) in entries {
match key {
"id" => {
if let crate::cbor::Value::Text(s) = value {
field_id = Some(s.to_string());
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
}
}
"rev" => {
if let crate::cbor::Value::Text(s) = value {
field_rev = Some(s.to_string());
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
}
}
"sender" => {
let raw = crate::cbor::encode_value(&value)?;
let mut dec = crate::cbor::Decoder::new(&raw);
field_sender = Some(ConvoDefsMessageViewSender::decode_cbor(&mut dec)?);
}
"sentAt" => {
if let crate::cbor::Value::Text(s) = value {
field_sent_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(ConvoDefsDeletedMessageView {
id: field_id.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'id'".into())
})?,
rev: field_rev.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'rev'".into())
})?,
sender: field_sender.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'sender'".into())
})?,
sent_at: field_sent_at.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'sentAt'".into())
})?,
extra: std::collections::HashMap::new(),
extra_cbor,
})
}
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ConvoDefsLogAcceptConvo {
pub convo_id: String,
pub rev: String,
#[serde(flatten)]
pub extra: std::collections::HashMap<String, serde_json::Value>,
#[serde(skip)]
pub extra_cbor: Vec<(String, Vec<u8>)>,
}
impl ConvoDefsLogAcceptConvo {
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("rev")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(&self.rev)?;
crate::cbor::Encoder::new(&mut *buf).encode_text("convoId")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(&self.convo_id)?;
} else {
let mut pairs: Vec<(&str, Vec<u8>)> = Vec::new();
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(&self.rev)?;
pairs.push(("rev", vbuf));
}
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(&self.convo_id)?;
pairs.push(("convoId", 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_rev: Option<String> = None;
let mut field_convo_id: Option<String> = None;
let mut extra_cbor: Vec<(String, Vec<u8>)> = Vec::new();
for (key, value) in entries {
match key {
"rev" => {
if let crate::cbor::Value::Text(s) = value {
field_rev = Some(s.to_string());
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
}
}
"convoId" => {
if let crate::cbor::Value::Text(s) = value {
field_convo_id = 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(ConvoDefsLogAcceptConvo {
rev: field_rev.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'rev'".into())
})?,
convo_id: field_convo_id.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'convoId'".into())
})?,
extra: std::collections::HashMap::new(),
extra_cbor,
})
}
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ConvoDefsLogAddReaction {
pub convo_id: String,
pub message: ConvoDefsLogAddReactionMessageUnion,
pub reaction: ConvoDefsReactionView,
pub rev: 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 ConvoDefsLogAddReactionMessageUnion {
ConvoDefsMessageView(Box<ConvoDefsMessageView>),
ConvoDefsDeletedMessageView(Box<ConvoDefsDeletedMessageView>),
Unknown(crate::api::UnknownUnionVariant),
}
impl serde::Serialize for ConvoDefsLogAddReactionMessageUnion {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
match self {
ConvoDefsLogAddReactionMessageUnion::ConvoDefsMessageView(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("chat.bsky.convo.defs#messageView".to_string()),
);
}
map.serialize(serializer)
}
ConvoDefsLogAddReactionMessageUnion::ConvoDefsDeletedMessageView(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(
"chat.bsky.convo.defs#deletedMessageView".to_string(),
),
);
}
map.serialize(serializer)
}
ConvoDefsLogAddReactionMessageUnion::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 ConvoDefsLogAddReactionMessageUnion {
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 {
"chat.bsky.convo.defs#messageView" => {
let inner: ConvoDefsMessageView =
serde_json::from_value(value).map_err(serde::de::Error::custom)?;
Ok(ConvoDefsLogAddReactionMessageUnion::ConvoDefsMessageView(
Box::new(inner),
))
}
"chat.bsky.convo.defs#deletedMessageView" => {
let inner: ConvoDefsDeletedMessageView =
serde_json::from_value(value).map_err(serde::de::Error::custom)?;
Ok(
ConvoDefsLogAddReactionMessageUnion::ConvoDefsDeletedMessageView(Box::new(
inner,
)),
)
}
_ => Ok(ConvoDefsLogAddReactionMessageUnion::Unknown(
crate::api::UnknownUnionVariant {
r#type: type_str.to_string(),
json: Some(value),
cbor: None,
},
)),
}
}
}
impl ConvoDefsLogAddReactionMessageUnion {
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 {
ConvoDefsLogAddReactionMessageUnion::ConvoDefsMessageView(inner) => {
inner.encode_cbor(buf)
}
ConvoDefsLogAddReactionMessageUnion::ConvoDefsDeletedMessageView(inner) => {
inner.encode_cbor(buf)
}
ConvoDefsLogAddReactionMessageUnion::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 {
"chat.bsky.convo.defs#messageView" => {
let mut dec = crate::cbor::Decoder::new(raw);
let inner = ConvoDefsMessageView::decode_cbor(&mut dec)?;
Ok(ConvoDefsLogAddReactionMessageUnion::ConvoDefsMessageView(
Box::new(inner),
))
}
"chat.bsky.convo.defs#deletedMessageView" => {
let mut dec = crate::cbor::Decoder::new(raw);
let inner = ConvoDefsDeletedMessageView::decode_cbor(&mut dec)?;
Ok(
ConvoDefsLogAddReactionMessageUnion::ConvoDefsDeletedMessageView(Box::new(
inner,
)),
)
}
_ => Ok(ConvoDefsLogAddReactionMessageUnion::Unknown(
crate::api::UnknownUnionVariant {
r#type: type_str.to_string(),
json: None,
cbor: Some(raw.to_vec()),
},
)),
}
}
}
impl ConvoDefsLogAddReaction {
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 = 4u64;
crate::cbor::Encoder::new(&mut *buf).encode_map_header(count)?;
crate::cbor::Encoder::new(&mut *buf).encode_text("rev")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(&self.rev)?;
crate::cbor::Encoder::new(&mut *buf).encode_text("convoId")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(&self.convo_id)?;
crate::cbor::Encoder::new(&mut *buf).encode_text("message")?;
self.message.encode_cbor(buf)?;
crate::cbor::Encoder::new(&mut *buf).encode_text("reaction")?;
self.reaction.encode_cbor(buf)?;
} else {
let mut pairs: Vec<(&str, Vec<u8>)> = Vec::new();
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(&self.rev)?;
pairs.push(("rev", vbuf));
}
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(&self.convo_id)?;
pairs.push(("convoId", vbuf));
}
{
let mut vbuf = Vec::new();
self.message.encode_cbor(&mut vbuf)?;
pairs.push(("message", vbuf));
}
{
let mut vbuf = Vec::new();
self.reaction.encode_cbor(&mut vbuf)?;
pairs.push(("reaction", 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_rev: Option<String> = None;
let mut field_convo_id: Option<String> = None;
let mut field_message: Option<ConvoDefsLogAddReactionMessageUnion> = None;
let mut field_reaction: Option<ConvoDefsReactionView> = None;
let mut extra_cbor: Vec<(String, Vec<u8>)> = Vec::new();
for (key, value) in entries {
match key {
"rev" => {
if let crate::cbor::Value::Text(s) = value {
field_rev = Some(s.to_string());
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
}
}
"convoId" => {
if let crate::cbor::Value::Text(s) = value {
field_convo_id = Some(s.to_string());
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
}
}
"message" => {
let raw = crate::cbor::encode_value(&value)?;
let mut dec = crate::cbor::Decoder::new(&raw);
field_message =
Some(ConvoDefsLogAddReactionMessageUnion::decode_cbor(&mut dec)?);
}
"reaction" => {
let raw = crate::cbor::encode_value(&value)?;
let mut dec = crate::cbor::Decoder::new(&raw);
field_reaction = Some(ConvoDefsReactionView::decode_cbor(&mut dec)?);
}
_ => {
let raw = crate::cbor::encode_value(&value)?;
extra_cbor.push((key.to_string(), raw));
}
}
}
Ok(ConvoDefsLogAddReaction {
rev: field_rev.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'rev'".into())
})?,
convo_id: field_convo_id.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'convoId'".into())
})?,
message: field_message.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'message'".into())
})?,
reaction: field_reaction.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'reaction'".into())
})?,
extra: std::collections::HashMap::new(),
extra_cbor,
})
}
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ConvoDefsLogBeginConvo {
pub convo_id: String,
pub rev: String,
#[serde(flatten)]
pub extra: std::collections::HashMap<String, serde_json::Value>,
#[serde(skip)]
pub extra_cbor: Vec<(String, Vec<u8>)>,
}
impl ConvoDefsLogBeginConvo {
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("rev")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(&self.rev)?;
crate::cbor::Encoder::new(&mut *buf).encode_text("convoId")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(&self.convo_id)?;
} else {
let mut pairs: Vec<(&str, Vec<u8>)> = Vec::new();
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(&self.rev)?;
pairs.push(("rev", vbuf));
}
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(&self.convo_id)?;
pairs.push(("convoId", 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_rev: Option<String> = None;
let mut field_convo_id: Option<String> = None;
let mut extra_cbor: Vec<(String, Vec<u8>)> = Vec::new();
for (key, value) in entries {
match key {
"rev" => {
if let crate::cbor::Value::Text(s) = value {
field_rev = Some(s.to_string());
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
}
}
"convoId" => {
if let crate::cbor::Value::Text(s) = value {
field_convo_id = 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(ConvoDefsLogBeginConvo {
rev: field_rev.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'rev'".into())
})?,
convo_id: field_convo_id.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'convoId'".into())
})?,
extra: std::collections::HashMap::new(),
extra_cbor,
})
}
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ConvoDefsLogCreateMessage {
pub convo_id: String,
pub message: ConvoDefsLogCreateMessageMessageUnion,
pub rev: 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 ConvoDefsLogCreateMessageMessageUnion {
ConvoDefsMessageView(Box<ConvoDefsMessageView>),
ConvoDefsDeletedMessageView(Box<ConvoDefsDeletedMessageView>),
Unknown(crate::api::UnknownUnionVariant),
}
impl serde::Serialize for ConvoDefsLogCreateMessageMessageUnion {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
match self {
ConvoDefsLogCreateMessageMessageUnion::ConvoDefsMessageView(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("chat.bsky.convo.defs#messageView".to_string()),
);
}
map.serialize(serializer)
}
ConvoDefsLogCreateMessageMessageUnion::ConvoDefsDeletedMessageView(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(
"chat.bsky.convo.defs#deletedMessageView".to_string(),
),
);
}
map.serialize(serializer)
}
ConvoDefsLogCreateMessageMessageUnion::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 ConvoDefsLogCreateMessageMessageUnion {
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 {
"chat.bsky.convo.defs#messageView" => {
let inner: ConvoDefsMessageView =
serde_json::from_value(value).map_err(serde::de::Error::custom)?;
Ok(ConvoDefsLogCreateMessageMessageUnion::ConvoDefsMessageView(
Box::new(inner),
))
}
"chat.bsky.convo.defs#deletedMessageView" => {
let inner: ConvoDefsDeletedMessageView =
serde_json::from_value(value).map_err(serde::de::Error::custom)?;
Ok(
ConvoDefsLogCreateMessageMessageUnion::ConvoDefsDeletedMessageView(Box::new(
inner,
)),
)
}
_ => Ok(ConvoDefsLogCreateMessageMessageUnion::Unknown(
crate::api::UnknownUnionVariant {
r#type: type_str.to_string(),
json: Some(value),
cbor: None,
},
)),
}
}
}
impl ConvoDefsLogCreateMessageMessageUnion {
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 {
ConvoDefsLogCreateMessageMessageUnion::ConvoDefsMessageView(inner) => {
inner.encode_cbor(buf)
}
ConvoDefsLogCreateMessageMessageUnion::ConvoDefsDeletedMessageView(inner) => {
inner.encode_cbor(buf)
}
ConvoDefsLogCreateMessageMessageUnion::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 {
"chat.bsky.convo.defs#messageView" => {
let mut dec = crate::cbor::Decoder::new(raw);
let inner = ConvoDefsMessageView::decode_cbor(&mut dec)?;
Ok(ConvoDefsLogCreateMessageMessageUnion::ConvoDefsMessageView(
Box::new(inner),
))
}
"chat.bsky.convo.defs#deletedMessageView" => {
let mut dec = crate::cbor::Decoder::new(raw);
let inner = ConvoDefsDeletedMessageView::decode_cbor(&mut dec)?;
Ok(
ConvoDefsLogCreateMessageMessageUnion::ConvoDefsDeletedMessageView(Box::new(
inner,
)),
)
}
_ => Ok(ConvoDefsLogCreateMessageMessageUnion::Unknown(
crate::api::UnknownUnionVariant {
r#type: type_str.to_string(),
json: None,
cbor: Some(raw.to_vec()),
},
)),
}
}
}
impl ConvoDefsLogCreateMessage {
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("rev")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(&self.rev)?;
crate::cbor::Encoder::new(&mut *buf).encode_text("convoId")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(&self.convo_id)?;
crate::cbor::Encoder::new(&mut *buf).encode_text("message")?;
self.message.encode_cbor(buf)?;
} else {
let mut pairs: Vec<(&str, Vec<u8>)> = Vec::new();
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(&self.rev)?;
pairs.push(("rev", vbuf));
}
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(&self.convo_id)?;
pairs.push(("convoId", vbuf));
}
{
let mut vbuf = Vec::new();
self.message.encode_cbor(&mut vbuf)?;
pairs.push(("message", 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_rev: Option<String> = None;
let mut field_convo_id: Option<String> = None;
let mut field_message: Option<ConvoDefsLogCreateMessageMessageUnion> = None;
let mut extra_cbor: Vec<(String, Vec<u8>)> = Vec::new();
for (key, value) in entries {
match key {
"rev" => {
if let crate::cbor::Value::Text(s) = value {
field_rev = Some(s.to_string());
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
}
}
"convoId" => {
if let crate::cbor::Value::Text(s) = value {
field_convo_id = Some(s.to_string());
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
}
}
"message" => {
let raw = crate::cbor::encode_value(&value)?;
let mut dec = crate::cbor::Decoder::new(&raw);
field_message = Some(ConvoDefsLogCreateMessageMessageUnion::decode_cbor(
&mut dec,
)?);
}
_ => {
let raw = crate::cbor::encode_value(&value)?;
extra_cbor.push((key.to_string(), raw));
}
}
}
Ok(ConvoDefsLogCreateMessage {
rev: field_rev.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'rev'".into())
})?,
convo_id: field_convo_id.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'convoId'".into())
})?,
message: field_message.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'message'".into())
})?,
extra: std::collections::HashMap::new(),
extra_cbor,
})
}
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ConvoDefsLogDeleteMessage {
pub convo_id: String,
pub message: ConvoDefsLogDeleteMessageMessageUnion,
pub rev: 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 ConvoDefsLogDeleteMessageMessageUnion {
ConvoDefsMessageView(Box<ConvoDefsMessageView>),
ConvoDefsDeletedMessageView(Box<ConvoDefsDeletedMessageView>),
Unknown(crate::api::UnknownUnionVariant),
}
impl serde::Serialize for ConvoDefsLogDeleteMessageMessageUnion {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
match self {
ConvoDefsLogDeleteMessageMessageUnion::ConvoDefsMessageView(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("chat.bsky.convo.defs#messageView".to_string()),
);
}
map.serialize(serializer)
}
ConvoDefsLogDeleteMessageMessageUnion::ConvoDefsDeletedMessageView(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(
"chat.bsky.convo.defs#deletedMessageView".to_string(),
),
);
}
map.serialize(serializer)
}
ConvoDefsLogDeleteMessageMessageUnion::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 ConvoDefsLogDeleteMessageMessageUnion {
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 {
"chat.bsky.convo.defs#messageView" => {
let inner: ConvoDefsMessageView =
serde_json::from_value(value).map_err(serde::de::Error::custom)?;
Ok(ConvoDefsLogDeleteMessageMessageUnion::ConvoDefsMessageView(
Box::new(inner),
))
}
"chat.bsky.convo.defs#deletedMessageView" => {
let inner: ConvoDefsDeletedMessageView =
serde_json::from_value(value).map_err(serde::de::Error::custom)?;
Ok(
ConvoDefsLogDeleteMessageMessageUnion::ConvoDefsDeletedMessageView(Box::new(
inner,
)),
)
}
_ => Ok(ConvoDefsLogDeleteMessageMessageUnion::Unknown(
crate::api::UnknownUnionVariant {
r#type: type_str.to_string(),
json: Some(value),
cbor: None,
},
)),
}
}
}
impl ConvoDefsLogDeleteMessageMessageUnion {
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 {
ConvoDefsLogDeleteMessageMessageUnion::ConvoDefsMessageView(inner) => {
inner.encode_cbor(buf)
}
ConvoDefsLogDeleteMessageMessageUnion::ConvoDefsDeletedMessageView(inner) => {
inner.encode_cbor(buf)
}
ConvoDefsLogDeleteMessageMessageUnion::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 {
"chat.bsky.convo.defs#messageView" => {
let mut dec = crate::cbor::Decoder::new(raw);
let inner = ConvoDefsMessageView::decode_cbor(&mut dec)?;
Ok(ConvoDefsLogDeleteMessageMessageUnion::ConvoDefsMessageView(
Box::new(inner),
))
}
"chat.bsky.convo.defs#deletedMessageView" => {
let mut dec = crate::cbor::Decoder::new(raw);
let inner = ConvoDefsDeletedMessageView::decode_cbor(&mut dec)?;
Ok(
ConvoDefsLogDeleteMessageMessageUnion::ConvoDefsDeletedMessageView(Box::new(
inner,
)),
)
}
_ => Ok(ConvoDefsLogDeleteMessageMessageUnion::Unknown(
crate::api::UnknownUnionVariant {
r#type: type_str.to_string(),
json: None,
cbor: Some(raw.to_vec()),
},
)),
}
}
}
impl ConvoDefsLogDeleteMessage {
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("rev")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(&self.rev)?;
crate::cbor::Encoder::new(&mut *buf).encode_text("convoId")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(&self.convo_id)?;
crate::cbor::Encoder::new(&mut *buf).encode_text("message")?;
self.message.encode_cbor(buf)?;
} else {
let mut pairs: Vec<(&str, Vec<u8>)> = Vec::new();
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(&self.rev)?;
pairs.push(("rev", vbuf));
}
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(&self.convo_id)?;
pairs.push(("convoId", vbuf));
}
{
let mut vbuf = Vec::new();
self.message.encode_cbor(&mut vbuf)?;
pairs.push(("message", 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_rev: Option<String> = None;
let mut field_convo_id: Option<String> = None;
let mut field_message: Option<ConvoDefsLogDeleteMessageMessageUnion> = None;
let mut extra_cbor: Vec<(String, Vec<u8>)> = Vec::new();
for (key, value) in entries {
match key {
"rev" => {
if let crate::cbor::Value::Text(s) = value {
field_rev = Some(s.to_string());
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
}
}
"convoId" => {
if let crate::cbor::Value::Text(s) = value {
field_convo_id = Some(s.to_string());
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
}
}
"message" => {
let raw = crate::cbor::encode_value(&value)?;
let mut dec = crate::cbor::Decoder::new(&raw);
field_message = Some(ConvoDefsLogDeleteMessageMessageUnion::decode_cbor(
&mut dec,
)?);
}
_ => {
let raw = crate::cbor::encode_value(&value)?;
extra_cbor.push((key.to_string(), raw));
}
}
}
Ok(ConvoDefsLogDeleteMessage {
rev: field_rev.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'rev'".into())
})?,
convo_id: field_convo_id.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'convoId'".into())
})?,
message: field_message.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'message'".into())
})?,
extra: std::collections::HashMap::new(),
extra_cbor,
})
}
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ConvoDefsLogLeaveConvo {
pub convo_id: String,
pub rev: String,
#[serde(flatten)]
pub extra: std::collections::HashMap<String, serde_json::Value>,
#[serde(skip)]
pub extra_cbor: Vec<(String, Vec<u8>)>,
}
impl ConvoDefsLogLeaveConvo {
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("rev")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(&self.rev)?;
crate::cbor::Encoder::new(&mut *buf).encode_text("convoId")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(&self.convo_id)?;
} else {
let mut pairs: Vec<(&str, Vec<u8>)> = Vec::new();
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(&self.rev)?;
pairs.push(("rev", vbuf));
}
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(&self.convo_id)?;
pairs.push(("convoId", 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_rev: Option<String> = None;
let mut field_convo_id: Option<String> = None;
let mut extra_cbor: Vec<(String, Vec<u8>)> = Vec::new();
for (key, value) in entries {
match key {
"rev" => {
if let crate::cbor::Value::Text(s) = value {
field_rev = Some(s.to_string());
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
}
}
"convoId" => {
if let crate::cbor::Value::Text(s) = value {
field_convo_id = 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(ConvoDefsLogLeaveConvo {
rev: field_rev.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'rev'".into())
})?,
convo_id: field_convo_id.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'convoId'".into())
})?,
extra: std::collections::HashMap::new(),
extra_cbor,
})
}
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ConvoDefsLogMuteConvo {
pub convo_id: String,
pub rev: String,
#[serde(flatten)]
pub extra: std::collections::HashMap<String, serde_json::Value>,
#[serde(skip)]
pub extra_cbor: Vec<(String, Vec<u8>)>,
}
impl ConvoDefsLogMuteConvo {
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("rev")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(&self.rev)?;
crate::cbor::Encoder::new(&mut *buf).encode_text("convoId")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(&self.convo_id)?;
} else {
let mut pairs: Vec<(&str, Vec<u8>)> = Vec::new();
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(&self.rev)?;
pairs.push(("rev", vbuf));
}
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(&self.convo_id)?;
pairs.push(("convoId", 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_rev: Option<String> = None;
let mut field_convo_id: Option<String> = None;
let mut extra_cbor: Vec<(String, Vec<u8>)> = Vec::new();
for (key, value) in entries {
match key {
"rev" => {
if let crate::cbor::Value::Text(s) = value {
field_rev = Some(s.to_string());
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
}
}
"convoId" => {
if let crate::cbor::Value::Text(s) = value {
field_convo_id = 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(ConvoDefsLogMuteConvo {
rev: field_rev.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'rev'".into())
})?,
convo_id: field_convo_id.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'convoId'".into())
})?,
extra: std::collections::HashMap::new(),
extra_cbor,
})
}
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ConvoDefsLogReadMessage {
pub convo_id: String,
pub message: ConvoDefsLogReadMessageMessageUnion,
pub rev: 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 ConvoDefsLogReadMessageMessageUnion {
ConvoDefsMessageView(Box<ConvoDefsMessageView>),
ConvoDefsDeletedMessageView(Box<ConvoDefsDeletedMessageView>),
Unknown(crate::api::UnknownUnionVariant),
}
impl serde::Serialize for ConvoDefsLogReadMessageMessageUnion {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
match self {
ConvoDefsLogReadMessageMessageUnion::ConvoDefsMessageView(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("chat.bsky.convo.defs#messageView".to_string()),
);
}
map.serialize(serializer)
}
ConvoDefsLogReadMessageMessageUnion::ConvoDefsDeletedMessageView(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(
"chat.bsky.convo.defs#deletedMessageView".to_string(),
),
);
}
map.serialize(serializer)
}
ConvoDefsLogReadMessageMessageUnion::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 ConvoDefsLogReadMessageMessageUnion {
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 {
"chat.bsky.convo.defs#messageView" => {
let inner: ConvoDefsMessageView =
serde_json::from_value(value).map_err(serde::de::Error::custom)?;
Ok(ConvoDefsLogReadMessageMessageUnion::ConvoDefsMessageView(
Box::new(inner),
))
}
"chat.bsky.convo.defs#deletedMessageView" => {
let inner: ConvoDefsDeletedMessageView =
serde_json::from_value(value).map_err(serde::de::Error::custom)?;
Ok(
ConvoDefsLogReadMessageMessageUnion::ConvoDefsDeletedMessageView(Box::new(
inner,
)),
)
}
_ => Ok(ConvoDefsLogReadMessageMessageUnion::Unknown(
crate::api::UnknownUnionVariant {
r#type: type_str.to_string(),
json: Some(value),
cbor: None,
},
)),
}
}
}
impl ConvoDefsLogReadMessageMessageUnion {
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 {
ConvoDefsLogReadMessageMessageUnion::ConvoDefsMessageView(inner) => {
inner.encode_cbor(buf)
}
ConvoDefsLogReadMessageMessageUnion::ConvoDefsDeletedMessageView(inner) => {
inner.encode_cbor(buf)
}
ConvoDefsLogReadMessageMessageUnion::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 {
"chat.bsky.convo.defs#messageView" => {
let mut dec = crate::cbor::Decoder::new(raw);
let inner = ConvoDefsMessageView::decode_cbor(&mut dec)?;
Ok(ConvoDefsLogReadMessageMessageUnion::ConvoDefsMessageView(
Box::new(inner),
))
}
"chat.bsky.convo.defs#deletedMessageView" => {
let mut dec = crate::cbor::Decoder::new(raw);
let inner = ConvoDefsDeletedMessageView::decode_cbor(&mut dec)?;
Ok(
ConvoDefsLogReadMessageMessageUnion::ConvoDefsDeletedMessageView(Box::new(
inner,
)),
)
}
_ => Ok(ConvoDefsLogReadMessageMessageUnion::Unknown(
crate::api::UnknownUnionVariant {
r#type: type_str.to_string(),
json: None,
cbor: Some(raw.to_vec()),
},
)),
}
}
}
impl ConvoDefsLogReadMessage {
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("rev")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(&self.rev)?;
crate::cbor::Encoder::new(&mut *buf).encode_text("convoId")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(&self.convo_id)?;
crate::cbor::Encoder::new(&mut *buf).encode_text("message")?;
self.message.encode_cbor(buf)?;
} else {
let mut pairs: Vec<(&str, Vec<u8>)> = Vec::new();
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(&self.rev)?;
pairs.push(("rev", vbuf));
}
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(&self.convo_id)?;
pairs.push(("convoId", vbuf));
}
{
let mut vbuf = Vec::new();
self.message.encode_cbor(&mut vbuf)?;
pairs.push(("message", 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_rev: Option<String> = None;
let mut field_convo_id: Option<String> = None;
let mut field_message: Option<ConvoDefsLogReadMessageMessageUnion> = None;
let mut extra_cbor: Vec<(String, Vec<u8>)> = Vec::new();
for (key, value) in entries {
match key {
"rev" => {
if let crate::cbor::Value::Text(s) = value {
field_rev = Some(s.to_string());
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
}
}
"convoId" => {
if let crate::cbor::Value::Text(s) = value {
field_convo_id = Some(s.to_string());
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
}
}
"message" => {
let raw = crate::cbor::encode_value(&value)?;
let mut dec = crate::cbor::Decoder::new(&raw);
field_message =
Some(ConvoDefsLogReadMessageMessageUnion::decode_cbor(&mut dec)?);
}
_ => {
let raw = crate::cbor::encode_value(&value)?;
extra_cbor.push((key.to_string(), raw));
}
}
}
Ok(ConvoDefsLogReadMessage {
rev: field_rev.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'rev'".into())
})?,
convo_id: field_convo_id.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'convoId'".into())
})?,
message: field_message.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'message'".into())
})?,
extra: std::collections::HashMap::new(),
extra_cbor,
})
}
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ConvoDefsLogRemoveReaction {
pub convo_id: String,
pub message: ConvoDefsLogRemoveReactionMessageUnion,
pub reaction: ConvoDefsReactionView,
pub rev: 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 ConvoDefsLogRemoveReactionMessageUnion {
ConvoDefsMessageView(Box<ConvoDefsMessageView>),
ConvoDefsDeletedMessageView(Box<ConvoDefsDeletedMessageView>),
Unknown(crate::api::UnknownUnionVariant),
}
impl serde::Serialize for ConvoDefsLogRemoveReactionMessageUnion {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
match self {
ConvoDefsLogRemoveReactionMessageUnion::ConvoDefsMessageView(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("chat.bsky.convo.defs#messageView".to_string()),
);
}
map.serialize(serializer)
}
ConvoDefsLogRemoveReactionMessageUnion::ConvoDefsDeletedMessageView(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(
"chat.bsky.convo.defs#deletedMessageView".to_string(),
),
);
}
map.serialize(serializer)
}
ConvoDefsLogRemoveReactionMessageUnion::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 ConvoDefsLogRemoveReactionMessageUnion {
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 {
"chat.bsky.convo.defs#messageView" => {
let inner: ConvoDefsMessageView =
serde_json::from_value(value).map_err(serde::de::Error::custom)?;
Ok(ConvoDefsLogRemoveReactionMessageUnion::ConvoDefsMessageView(Box::new(inner)))
}
"chat.bsky.convo.defs#deletedMessageView" => {
let inner: ConvoDefsDeletedMessageView =
serde_json::from_value(value).map_err(serde::de::Error::custom)?;
Ok(
ConvoDefsLogRemoveReactionMessageUnion::ConvoDefsDeletedMessageView(Box::new(
inner,
)),
)
}
_ => Ok(ConvoDefsLogRemoveReactionMessageUnion::Unknown(
crate::api::UnknownUnionVariant {
r#type: type_str.to_string(),
json: Some(value),
cbor: None,
},
)),
}
}
}
impl ConvoDefsLogRemoveReactionMessageUnion {
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 {
ConvoDefsLogRemoveReactionMessageUnion::ConvoDefsMessageView(inner) => {
inner.encode_cbor(buf)
}
ConvoDefsLogRemoveReactionMessageUnion::ConvoDefsDeletedMessageView(inner) => {
inner.encode_cbor(buf)
}
ConvoDefsLogRemoveReactionMessageUnion::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 {
"chat.bsky.convo.defs#messageView" => {
let mut dec = crate::cbor::Decoder::new(raw);
let inner = ConvoDefsMessageView::decode_cbor(&mut dec)?;
Ok(ConvoDefsLogRemoveReactionMessageUnion::ConvoDefsMessageView(Box::new(inner)))
}
"chat.bsky.convo.defs#deletedMessageView" => {
let mut dec = crate::cbor::Decoder::new(raw);
let inner = ConvoDefsDeletedMessageView::decode_cbor(&mut dec)?;
Ok(
ConvoDefsLogRemoveReactionMessageUnion::ConvoDefsDeletedMessageView(Box::new(
inner,
)),
)
}
_ => Ok(ConvoDefsLogRemoveReactionMessageUnion::Unknown(
crate::api::UnknownUnionVariant {
r#type: type_str.to_string(),
json: None,
cbor: Some(raw.to_vec()),
},
)),
}
}
}
impl ConvoDefsLogRemoveReaction {
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 = 4u64;
crate::cbor::Encoder::new(&mut *buf).encode_map_header(count)?;
crate::cbor::Encoder::new(&mut *buf).encode_text("rev")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(&self.rev)?;
crate::cbor::Encoder::new(&mut *buf).encode_text("convoId")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(&self.convo_id)?;
crate::cbor::Encoder::new(&mut *buf).encode_text("message")?;
self.message.encode_cbor(buf)?;
crate::cbor::Encoder::new(&mut *buf).encode_text("reaction")?;
self.reaction.encode_cbor(buf)?;
} else {
let mut pairs: Vec<(&str, Vec<u8>)> = Vec::new();
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(&self.rev)?;
pairs.push(("rev", vbuf));
}
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(&self.convo_id)?;
pairs.push(("convoId", vbuf));
}
{
let mut vbuf = Vec::new();
self.message.encode_cbor(&mut vbuf)?;
pairs.push(("message", vbuf));
}
{
let mut vbuf = Vec::new();
self.reaction.encode_cbor(&mut vbuf)?;
pairs.push(("reaction", 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_rev: Option<String> = None;
let mut field_convo_id: Option<String> = None;
let mut field_message: Option<ConvoDefsLogRemoveReactionMessageUnion> = None;
let mut field_reaction: Option<ConvoDefsReactionView> = None;
let mut extra_cbor: Vec<(String, Vec<u8>)> = Vec::new();
for (key, value) in entries {
match key {
"rev" => {
if let crate::cbor::Value::Text(s) = value {
field_rev = Some(s.to_string());
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
}
}
"convoId" => {
if let crate::cbor::Value::Text(s) = value {
field_convo_id = Some(s.to_string());
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
}
}
"message" => {
let raw = crate::cbor::encode_value(&value)?;
let mut dec = crate::cbor::Decoder::new(&raw);
field_message = Some(ConvoDefsLogRemoveReactionMessageUnion::decode_cbor(
&mut dec,
)?);
}
"reaction" => {
let raw = crate::cbor::encode_value(&value)?;
let mut dec = crate::cbor::Decoder::new(&raw);
field_reaction = Some(ConvoDefsReactionView::decode_cbor(&mut dec)?);
}
_ => {
let raw = crate::cbor::encode_value(&value)?;
extra_cbor.push((key.to_string(), raw));
}
}
}
Ok(ConvoDefsLogRemoveReaction {
rev: field_rev.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'rev'".into())
})?,
convo_id: field_convo_id.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'convoId'".into())
})?,
message: field_message.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'message'".into())
})?,
reaction: field_reaction.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'reaction'".into())
})?,
extra: std::collections::HashMap::new(),
extra_cbor,
})
}
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ConvoDefsLogUnmuteConvo {
pub convo_id: String,
pub rev: String,
#[serde(flatten)]
pub extra: std::collections::HashMap<String, serde_json::Value>,
#[serde(skip)]
pub extra_cbor: Vec<(String, Vec<u8>)>,
}
impl ConvoDefsLogUnmuteConvo {
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("rev")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(&self.rev)?;
crate::cbor::Encoder::new(&mut *buf).encode_text("convoId")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(&self.convo_id)?;
} else {
let mut pairs: Vec<(&str, Vec<u8>)> = Vec::new();
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(&self.rev)?;
pairs.push(("rev", vbuf));
}
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(&self.convo_id)?;
pairs.push(("convoId", 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_rev: Option<String> = None;
let mut field_convo_id: Option<String> = None;
let mut extra_cbor: Vec<(String, Vec<u8>)> = Vec::new();
for (key, value) in entries {
match key {
"rev" => {
if let crate::cbor::Value::Text(s) = value {
field_rev = Some(s.to_string());
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
}
}
"convoId" => {
if let crate::cbor::Value::Text(s) = value {
field_convo_id = 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(ConvoDefsLogUnmuteConvo {
rev: field_rev.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'rev'".into())
})?,
convo_id: field_convo_id.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'convoId'".into())
})?,
extra: std::collections::HashMap::new(),
extra_cbor,
})
}
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ConvoDefsMessageAndReactionView {
pub message: ConvoDefsMessageView,
pub reaction: ConvoDefsReactionView,
#[serde(flatten)]
pub extra: std::collections::HashMap<String, serde_json::Value>,
#[serde(skip)]
pub extra_cbor: Vec<(String, Vec<u8>)>,
}
impl ConvoDefsMessageAndReactionView {
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("message")?;
self.message.encode_cbor(buf)?;
crate::cbor::Encoder::new(&mut *buf).encode_text("reaction")?;
self.reaction.encode_cbor(buf)?;
} else {
let mut pairs: Vec<(&str, Vec<u8>)> = Vec::new();
{
let mut vbuf = Vec::new();
self.message.encode_cbor(&mut vbuf)?;
pairs.push(("message", vbuf));
}
{
let mut vbuf = Vec::new();
self.reaction.encode_cbor(&mut vbuf)?;
pairs.push(("reaction", 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_message: Option<ConvoDefsMessageView> = None;
let mut field_reaction: Option<ConvoDefsReactionView> = None;
let mut extra_cbor: Vec<(String, Vec<u8>)> = Vec::new();
for (key, value) in entries {
match key {
"message" => {
let raw = crate::cbor::encode_value(&value)?;
let mut dec = crate::cbor::Decoder::new(&raw);
field_message = Some(ConvoDefsMessageView::decode_cbor(&mut dec)?);
}
"reaction" => {
let raw = crate::cbor::encode_value(&value)?;
let mut dec = crate::cbor::Decoder::new(&raw);
field_reaction = Some(ConvoDefsReactionView::decode_cbor(&mut dec)?);
}
_ => {
let raw = crate::cbor::encode_value(&value)?;
extra_cbor.push((key.to_string(), raw));
}
}
}
Ok(ConvoDefsMessageAndReactionView {
message: field_message.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'message'".into())
})?,
reaction: field_reaction.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'reaction'".into())
})?,
extra: std::collections::HashMap::new(),
extra_cbor,
})
}
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ConvoDefsMessageInput {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub embed: Option<ConvoDefsMessageInputEmbedUnion>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub facets: Vec<crate::api::app::bsky::RichtextFacet>,
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 ConvoDefsMessageInputEmbedUnion {
EmbedRecord(Box<crate::api::app::bsky::EmbedRecord>),
Unknown(crate::api::UnknownUnionVariant),
}
impl serde::Serialize for ConvoDefsMessageInputEmbedUnion {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
match self {
ConvoDefsMessageInputEmbedUnion::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)
}
ConvoDefsMessageInputEmbedUnion::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 ConvoDefsMessageInputEmbedUnion {
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" => {
let inner: crate::api::app::bsky::EmbedRecord =
serde_json::from_value(value).map_err(serde::de::Error::custom)?;
Ok(ConvoDefsMessageInputEmbedUnion::EmbedRecord(Box::new(
inner,
)))
}
_ => Ok(ConvoDefsMessageInputEmbedUnion::Unknown(
crate::api::UnknownUnionVariant {
r#type: type_str.to_string(),
json: Some(value),
cbor: None,
},
)),
}
}
}
impl ConvoDefsMessageInputEmbedUnion {
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 {
ConvoDefsMessageInputEmbedUnion::EmbedRecord(inner) => inner.encode_cbor(buf),
ConvoDefsMessageInputEmbedUnion::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" => {
let mut dec = crate::cbor::Decoder::new(raw);
let inner = crate::api::app::bsky::EmbedRecord::decode_cbor(&mut dec)?;
Ok(ConvoDefsMessageInputEmbedUnion::EmbedRecord(Box::new(
inner,
)))
}
_ => Ok(ConvoDefsMessageInputEmbedUnion::Unknown(
crate::api::UnknownUnionVariant {
r#type: type_str.to_string(),
json: None,
cbor: Some(raw.to_vec()),
},
)),
}
}
}
impl ConvoDefsMessageInput {
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.embed.is_some() {
count += 1;
}
if !self.facets.is_empty() {
count += 1;
}
crate::cbor::Encoder::new(&mut *buf).encode_map_header(count)?;
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.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)?;
}
}
} else {
let mut pairs: Vec<(&str, Vec<u8>)> = Vec::new();
{
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.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));
}
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_text: Option<String> = None;
let mut field_embed: Option<ConvoDefsMessageInputEmbedUnion> = None;
let mut field_facets: Vec<crate::api::app::bsky::RichtextFacet> = Vec::new();
let mut extra_cbor: Vec<(String, Vec<u8>)> = Vec::new();
for (key, value) in entries {
match key {
"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(ConvoDefsMessageInputEmbedUnion::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()));
}
}
_ => {
let raw = crate::cbor::encode_value(&value)?;
extra_cbor.push((key.to_string(), raw));
}
}
}
Ok(ConvoDefsMessageInput {
text: field_text.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'text'".into())
})?,
embed: field_embed,
facets: field_facets,
extra: std::collections::HashMap::new(),
extra_cbor,
})
}
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ConvoDefsMessageRef {
pub convo_id: String,
pub did: crate::syntax::Did,
pub message_id: String,
#[serde(flatten)]
pub extra: std::collections::HashMap<String, serde_json::Value>,
#[serde(skip)]
pub extra_cbor: Vec<(String, Vec<u8>)>,
}
impl ConvoDefsMessageRef {
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("did")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(self.did.as_str())?;
crate::cbor::Encoder::new(&mut *buf).encode_text("convoId")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(&self.convo_id)?;
crate::cbor::Encoder::new(&mut *buf).encode_text("messageId")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(&self.message_id)?;
} else {
let mut pairs: Vec<(&str, Vec<u8>)> = Vec::new();
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(self.did.as_str())?;
pairs.push(("did", vbuf));
}
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(&self.convo_id)?;
pairs.push(("convoId", vbuf));
}
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(&self.message_id)?;
pairs.push(("messageId", vbuf));
}
for (k, v) in &self.extra_cbor {
pairs.push((k.as_str(), v.clone()));
}
pairs.sort_by(|a, b| crate::cbor::cbor_key_cmp(a.0, b.0));
crate::cbor::Encoder::new(&mut *buf).encode_map_header(pairs.len() as u64)?;
for (k, v) in &pairs {
crate::cbor::Encoder::new(&mut *buf).encode_text(k)?;
buf.extend_from_slice(v);
}
}
Ok(())
}
pub fn from_cbor(data: &[u8]) -> Result<Self, crate::cbor::CborError> {
let mut decoder = crate::cbor::Decoder::new(data);
let result = Self::decode_cbor(&mut decoder)?;
if !decoder.is_empty() {
return Err(crate::cbor::CborError::InvalidCbor("trailing data".into()));
}
Ok(result)
}
pub fn decode_cbor(decoder: &mut crate::cbor::Decoder) -> Result<Self, crate::cbor::CborError> {
let val = decoder.decode()?;
let entries = match val {
crate::cbor::Value::Map(entries) => entries,
_ => return Err(crate::cbor::CborError::InvalidCbor("expected map".into())),
};
let mut field_did: Option<crate::syntax::Did> = None;
let mut field_convo_id: Option<String> = None;
let mut field_message_id: Option<String> = None;
let mut extra_cbor: Vec<(String, Vec<u8>)> = Vec::new();
for (key, value) in entries {
match key {
"did" => {
if let crate::cbor::Value::Text(s) = value {
field_did = Some(
crate::syntax::Did::try_from(s)
.map_err(|e| crate::cbor::CborError::InvalidCbor(e.to_string()))?,
);
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
}
}
"convoId" => {
if let crate::cbor::Value::Text(s) = value {
field_convo_id = Some(s.to_string());
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
}
}
"messageId" => {
if let crate::cbor::Value::Text(s) = value {
field_message_id = 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(ConvoDefsMessageRef {
did: field_did.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'did'".into())
})?,
convo_id: field_convo_id.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'convoId'".into())
})?,
message_id: field_message_id.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'messageId'".into())
})?,
extra: std::collections::HashMap::new(),
extra_cbor,
})
}
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ConvoDefsMessageView {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub embed: Option<ConvoDefsMessageViewEmbedUnion>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub facets: Vec<crate::api::app::bsky::RichtextFacet>,
pub id: String,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub reactions: Vec<ConvoDefsReactionView>,
pub rev: String,
pub sender: ConvoDefsMessageViewSender,
pub sent_at: crate::syntax::Datetime,
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 ConvoDefsMessageViewEmbedUnion {
EmbedRecordView(Box<crate::api::app::bsky::EmbedRecordView>),
Unknown(crate::api::UnknownUnionVariant),
}
impl serde::Serialize for ConvoDefsMessageViewEmbedUnion {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
match self {
ConvoDefsMessageViewEmbedUnion::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)
}
ConvoDefsMessageViewEmbedUnion::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 ConvoDefsMessageViewEmbedUnion {
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#view" => {
let inner: crate::api::app::bsky::EmbedRecordView =
serde_json::from_value(value).map_err(serde::de::Error::custom)?;
Ok(ConvoDefsMessageViewEmbedUnion::EmbedRecordView(Box::new(
inner,
)))
}
_ => Ok(ConvoDefsMessageViewEmbedUnion::Unknown(
crate::api::UnknownUnionVariant {
r#type: type_str.to_string(),
json: Some(value),
cbor: None,
},
)),
}
}
}
impl ConvoDefsMessageViewEmbedUnion {
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 {
ConvoDefsMessageViewEmbedUnion::EmbedRecordView(inner) => inner.encode_cbor(buf),
ConvoDefsMessageViewEmbedUnion::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#view" => {
let mut dec = crate::cbor::Decoder::new(raw);
let inner = crate::api::app::bsky::EmbedRecordView::decode_cbor(&mut dec)?;
Ok(ConvoDefsMessageViewEmbedUnion::EmbedRecordView(Box::new(
inner,
)))
}
_ => Ok(ConvoDefsMessageViewEmbedUnion::Unknown(
crate::api::UnknownUnionVariant {
r#type: type_str.to_string(),
json: None,
cbor: Some(raw.to_vec()),
},
)),
}
}
}
impl ConvoDefsMessageView {
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.embed.is_some() {
count += 1;
}
if !self.facets.is_empty() {
count += 1;
}
if !self.reactions.is_empty() {
count += 1;
}
crate::cbor::Encoder::new(&mut *buf).encode_map_header(count)?;
crate::cbor::Encoder::new(&mut *buf).encode_text("id")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(&self.id)?;
crate::cbor::Encoder::new(&mut *buf).encode_text("rev")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(&self.rev)?;
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.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)?;
}
}
crate::cbor::Encoder::new(&mut *buf).encode_text("sender")?;
self.sender.encode_cbor(buf)?;
crate::cbor::Encoder::new(&mut *buf).encode_text("sentAt")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(self.sent_at.as_str())?;
if !self.reactions.is_empty() {
crate::cbor::Encoder::new(&mut *buf).encode_text("reactions")?;
crate::cbor::Encoder::new(&mut *buf)
.encode_array_header(self.reactions.len() as u64)?;
for item in &self.reactions {
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_text(&self.id)?;
pairs.push(("id", vbuf));
}
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(&self.rev)?;
pairs.push(("rev", 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.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));
}
{
let mut vbuf = Vec::new();
self.sender.encode_cbor(&mut vbuf)?;
pairs.push(("sender", vbuf));
}
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(self.sent_at.as_str())?;
pairs.push(("sentAt", vbuf));
}
if !self.reactions.is_empty() {
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf)
.encode_array_header(self.reactions.len() as u64)?;
for item in &self.reactions {
item.encode_cbor(&mut vbuf)?;
}
pairs.push(("reactions", 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_id: Option<String> = None;
let mut field_rev: Option<String> = None;
let mut field_text: Option<String> = None;
let mut field_embed: Option<ConvoDefsMessageViewEmbedUnion> = None;
let mut field_facets: Vec<crate::api::app::bsky::RichtextFacet> = Vec::new();
let mut field_sender: Option<ConvoDefsMessageViewSender> = None;
let mut field_sent_at: Option<crate::syntax::Datetime> = None;
let mut field_reactions: Vec<ConvoDefsReactionView> = Vec::new();
let mut extra_cbor: Vec<(String, Vec<u8>)> = Vec::new();
for (key, value) in entries {
match key {
"id" => {
if let crate::cbor::Value::Text(s) = value {
field_id = Some(s.to_string());
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
}
}
"rev" => {
if let crate::cbor::Value::Text(s) = value {
field_rev = Some(s.to_string());
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected text".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(ConvoDefsMessageViewEmbedUnion::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()));
}
}
"sender" => {
let raw = crate::cbor::encode_value(&value)?;
let mut dec = crate::cbor::Decoder::new(&raw);
field_sender = Some(ConvoDefsMessageViewSender::decode_cbor(&mut dec)?);
}
"sentAt" => {
if let crate::cbor::Value::Text(s) = value {
field_sent_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()));
}
}
"reactions" => {
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_reactions.push(ConvoDefsReactionView::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(ConvoDefsMessageView {
id: field_id.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'id'".into())
})?,
rev: field_rev.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'rev'".into())
})?,
text: field_text.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'text'".into())
})?,
embed: field_embed,
facets: field_facets,
sender: field_sender.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'sender'".into())
})?,
sent_at: field_sent_at.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'sentAt'".into())
})?,
reactions: field_reactions,
extra: std::collections::HashMap::new(),
extra_cbor,
})
}
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ConvoDefsMessageViewSender {
pub did: crate::syntax::Did,
#[serde(flatten)]
pub extra: std::collections::HashMap<String, serde_json::Value>,
#[serde(skip)]
pub extra_cbor: Vec<(String, Vec<u8>)>,
}
impl ConvoDefsMessageViewSender {
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("did")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(self.did.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.did.as_str())?;
pairs.push(("did", vbuf));
}
for (k, v) in &self.extra_cbor {
pairs.push((k.as_str(), v.clone()));
}
pairs.sort_by(|a, b| crate::cbor::cbor_key_cmp(a.0, b.0));
crate::cbor::Encoder::new(&mut *buf).encode_map_header(pairs.len() as u64)?;
for (k, v) in &pairs {
crate::cbor::Encoder::new(&mut *buf).encode_text(k)?;
buf.extend_from_slice(v);
}
}
Ok(())
}
pub fn from_cbor(data: &[u8]) -> Result<Self, crate::cbor::CborError> {
let mut decoder = crate::cbor::Decoder::new(data);
let result = Self::decode_cbor(&mut decoder)?;
if !decoder.is_empty() {
return Err(crate::cbor::CborError::InvalidCbor("trailing data".into()));
}
Ok(result)
}
pub fn decode_cbor(decoder: &mut crate::cbor::Decoder) -> Result<Self, crate::cbor::CborError> {
let val = decoder.decode()?;
let entries = match val {
crate::cbor::Value::Map(entries) => entries,
_ => return Err(crate::cbor::CborError::InvalidCbor("expected map".into())),
};
let mut field_did: Option<crate::syntax::Did> = None;
let mut extra_cbor: Vec<(String, Vec<u8>)> = Vec::new();
for (key, value) in entries {
match key {
"did" => {
if let crate::cbor::Value::Text(s) = value {
field_did = Some(
crate::syntax::Did::try_from(s)
.map_err(|e| crate::cbor::CborError::InvalidCbor(e.to_string()))?,
);
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
}
}
_ => {
let raw = crate::cbor::encode_value(&value)?;
extra_cbor.push((key.to_string(), raw));
}
}
}
Ok(ConvoDefsMessageViewSender {
did: field_did.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'did'".into())
})?,
extra: std::collections::HashMap::new(),
extra_cbor,
})
}
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ConvoDefsReactionView {
pub created_at: crate::syntax::Datetime,
pub sender: ConvoDefsReactionViewSender,
pub value: String,
#[serde(flatten)]
pub extra: std::collections::HashMap<String, serde_json::Value>,
#[serde(skip)]
pub extra_cbor: Vec<(String, Vec<u8>)>,
}
impl ConvoDefsReactionView {
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("value")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(&self.value)?;
crate::cbor::Encoder::new(&mut *buf).encode_text("sender")?;
self.sender.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();
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(&self.value)?;
pairs.push(("value", vbuf));
}
{
let mut vbuf = Vec::new();
self.sender.encode_cbor(&mut vbuf)?;
pairs.push(("sender", 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_value: Option<String> = None;
let mut field_sender: Option<ConvoDefsReactionViewSender> = None;
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 {
"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()));
}
}
"sender" => {
let raw = crate::cbor::encode_value(&value)?;
let mut dec = crate::cbor::Decoder::new(&raw);
field_sender = Some(ConvoDefsReactionViewSender::decode_cbor(&mut dec)?);
}
"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(ConvoDefsReactionView {
value: field_value.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'value'".into())
})?,
sender: field_sender.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'sender'".into())
})?,
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 ConvoDefsReactionViewSender {
pub did: crate::syntax::Did,
#[serde(flatten)]
pub extra: std::collections::HashMap<String, serde_json::Value>,
#[serde(skip)]
pub extra_cbor: Vec<(String, Vec<u8>)>,
}
impl ConvoDefsReactionViewSender {
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("did")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(self.did.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.did.as_str())?;
pairs.push(("did", vbuf));
}
for (k, v) in &self.extra_cbor {
pairs.push((k.as_str(), v.clone()));
}
pairs.sort_by(|a, b| crate::cbor::cbor_key_cmp(a.0, b.0));
crate::cbor::Encoder::new(&mut *buf).encode_map_header(pairs.len() as u64)?;
for (k, v) in &pairs {
crate::cbor::Encoder::new(&mut *buf).encode_text(k)?;
buf.extend_from_slice(v);
}
}
Ok(())
}
pub fn from_cbor(data: &[u8]) -> Result<Self, crate::cbor::CborError> {
let mut decoder = crate::cbor::Decoder::new(data);
let result = Self::decode_cbor(&mut decoder)?;
if !decoder.is_empty() {
return Err(crate::cbor::CborError::InvalidCbor("trailing data".into()));
}
Ok(result)
}
pub fn decode_cbor(decoder: &mut crate::cbor::Decoder) -> Result<Self, crate::cbor::CborError> {
let val = decoder.decode()?;
let entries = match val {
crate::cbor::Value::Map(entries) => entries,
_ => return Err(crate::cbor::CborError::InvalidCbor("expected map".into())),
};
let mut field_did: Option<crate::syntax::Did> = None;
let mut extra_cbor: Vec<(String, Vec<u8>)> = Vec::new();
for (key, value) in entries {
match key {
"did" => {
if let crate::cbor::Value::Text(s) = value {
field_did = Some(
crate::syntax::Did::try_from(s)
.map_err(|e| crate::cbor::CborError::InvalidCbor(e.to_string()))?,
);
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
}
}
_ => {
let raw = crate::cbor::encode_value(&value)?;
extra_cbor.push((key.to_string(), raw));
}
}
}
Ok(ConvoDefsReactionViewSender {
did: field_did.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'did'".into())
})?,
extra: std::collections::HashMap::new(),
extra_cbor,
})
}
}