#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ModerationDefsConvoView {
pub id: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub kind: Option<ModerationDefsConvoViewKindUnion>,
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 ModerationDefsConvoViewKindUnion {
ModerationDefsDirectConvo(Box<ModerationDefsDirectConvo>),
ModerationDefsGroupConvo(Box<ModerationDefsGroupConvo>),
Unknown(crate::api::UnknownUnionVariant),
}
impl serde::Serialize for ModerationDefsConvoViewKindUnion {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
match self {
ModerationDefsConvoViewKindUnion::ModerationDefsDirectConvo(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.moderation.defs#directConvo".to_string(),
),
);
}
map.serialize(serializer)
}
ModerationDefsConvoViewKindUnion::ModerationDefsGroupConvo(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.moderation.defs#groupConvo".to_string(),
),
);
}
map.serialize(serializer)
}
ModerationDefsConvoViewKindUnion::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 ModerationDefsConvoViewKindUnion {
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.moderation.defs#directConvo" => {
let inner: ModerationDefsDirectConvo =
serde_json::from_value(value).map_err(serde::de::Error::custom)?;
Ok(ModerationDefsConvoViewKindUnion::ModerationDefsDirectConvo(
Box::new(inner),
))
}
"chat.bsky.moderation.defs#groupConvo" => {
let inner: ModerationDefsGroupConvo =
serde_json::from_value(value).map_err(serde::de::Error::custom)?;
Ok(ModerationDefsConvoViewKindUnion::ModerationDefsGroupConvo(
Box::new(inner),
))
}
_ => Ok(ModerationDefsConvoViewKindUnion::Unknown(
crate::api::UnknownUnionVariant {
r#type: type_str.to_string(),
json: Some(value),
cbor: None,
},
)),
}
}
}
impl ModerationDefsConvoViewKindUnion {
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 {
ModerationDefsConvoViewKindUnion::ModerationDefsDirectConvo(inner) => {
inner.encode_cbor(buf)
}
ModerationDefsConvoViewKindUnion::ModerationDefsGroupConvo(inner) => {
inner.encode_cbor(buf)
}
ModerationDefsConvoViewKindUnion::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.moderation.defs#directConvo" => {
let mut dec = crate::cbor::Decoder::new(raw);
let inner = ModerationDefsDirectConvo::decode_cbor(&mut dec)?;
Ok(ModerationDefsConvoViewKindUnion::ModerationDefsDirectConvo(
Box::new(inner),
))
}
"chat.bsky.moderation.defs#groupConvo" => {
let mut dec = crate::cbor::Decoder::new(raw);
let inner = ModerationDefsGroupConvo::decode_cbor(&mut dec)?;
Ok(ModerationDefsConvoViewKindUnion::ModerationDefsGroupConvo(
Box::new(inner),
))
}
_ => Ok(ModerationDefsConvoViewKindUnion::Unknown(
crate::api::UnknownUnionVariant {
r#type: type_str.to_string(),
json: None,
cbor: Some(raw.to_vec()),
},
)),
}
}
}
impl ModerationDefsConvoView {
pub fn to_cbor(&self) -> Result<Vec<u8>, crate::cbor::CborError> {
let mut buf = Vec::new();
self.encode_cbor(&mut buf)?;
Ok(buf)
}
pub fn encode_cbor(&self, buf: &mut Vec<u8>) -> Result<(), crate::cbor::CborError> {
if self.extra_cbor.is_empty() {
let mut count = 2u64;
if self.kind.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)?;
if self.kind.is_some() {
crate::cbor::Encoder::new(&mut *buf).encode_text("kind")?;
if let Some(ref val) = self.kind {
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));
}
if self.kind.is_some() {
let mut vbuf = Vec::new();
if let Some(ref val) = self.kind {
val.encode_cbor(&mut vbuf)?;
}
pairs.push(("kind", 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_kind: Option<ModerationDefsConvoViewKindUnion> = 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()));
}
}
"kind" => {
let raw = crate::cbor::encode_value(&value)?;
let mut dec = crate::cbor::Decoder::new(&raw);
field_kind = Some(ModerationDefsConvoViewKindUnion::decode_cbor(&mut dec)?);
}
_ => {
let raw = crate::cbor::encode_value(&value)?;
extra_cbor.push((key.to_string(), raw));
}
}
}
Ok(ModerationDefsConvoView {
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())
})?,
kind: field_kind,
extra: std::collections::HashMap::new(),
extra_cbor,
})
}
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ModerationDefsDirectConvo {
#[serde(flatten)]
pub extra: std::collections::HashMap<String, serde_json::Value>,
#[serde(skip)]
pub extra_cbor: Vec<(String, Vec<u8>)>,
}
impl ModerationDefsDirectConvo {
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 = 0u64;
crate::cbor::Encoder::new(&mut *buf).encode_map_header(count)?;
} else {
let mut pairs: Vec<(&str, Vec<u8>)> = Vec::new();
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 extra_cbor: Vec<(String, Vec<u8>)> = Vec::new();
for (key, value) in entries {
match key {
_ => {
let raw = crate::cbor::encode_value(&value)?;
extra_cbor.push((key.to_string(), raw));
}
}
}
Ok(ModerationDefsDirectConvo {
extra: std::collections::HashMap::new(),
extra_cbor,
})
}
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ModerationDefsGroupConvo {
pub created_at: crate::syntax::Datetime,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub join_link: Option<crate::api::chat::bsky::GroupDefsJoinLinkView>,
pub join_request_count: i64,
pub lock_status: crate::api::chat::bsky::ConvoDefsConvoLockStatus,
pub member_count: i64,
pub member_limit: i64,
pub name: String,
#[serde(flatten)]
pub extra: std::collections::HashMap<String, serde_json::Value>,
#[serde(skip)]
pub extra_cbor: Vec<(String, Vec<u8>)>,
}
impl ModerationDefsGroupConvo {
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 = 6u64;
if self.join_link.is_some() {
count += 1;
}
crate::cbor::Encoder::new(&mut *buf).encode_map_header(count)?;
crate::cbor::Encoder::new(&mut *buf).encode_text("name")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(&self.name)?;
if self.join_link.is_some() {
crate::cbor::Encoder::new(&mut *buf).encode_text("joinLink")?;
if let Some(ref val) = self.join_link {
val.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())?;
crate::cbor::Encoder::new(&mut *buf).encode_text("lockStatus")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(&self.lock_status)?;
crate::cbor::Encoder::new(&mut *buf).encode_text("memberCount")?;
crate::cbor::Encoder::new(&mut *buf).encode_i64(self.member_count)?;
crate::cbor::Encoder::new(&mut *buf).encode_text("memberLimit")?;
crate::cbor::Encoder::new(&mut *buf).encode_i64(self.member_limit)?;
crate::cbor::Encoder::new(&mut *buf).encode_text("joinRequestCount")?;
crate::cbor::Encoder::new(&mut *buf).encode_i64(self.join_request_count)?;
} else {
let mut pairs: Vec<(&str, Vec<u8>)> = Vec::new();
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(&self.name)?;
pairs.push(("name", vbuf));
}
if self.join_link.is_some() {
let mut vbuf = Vec::new();
if let Some(ref val) = self.join_link {
val.encode_cbor(&mut vbuf)?;
}
pairs.push(("joinLink", vbuf));
}
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(self.created_at.as_str())?;
pairs.push(("createdAt", vbuf));
}
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(&self.lock_status)?;
pairs.push(("lockStatus", vbuf));
}
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_i64(self.member_count)?;
pairs.push(("memberCount", vbuf));
}
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_i64(self.member_limit)?;
pairs.push(("memberLimit", vbuf));
}
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_i64(self.join_request_count)?;
pairs.push(("joinRequestCount", 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_name: Option<String> = None;
let mut field_join_link: Option<crate::api::chat::bsky::GroupDefsJoinLinkView> = None;
let mut field_created_at: Option<crate::syntax::Datetime> = None;
let mut field_lock_status: Option<crate::api::chat::bsky::ConvoDefsConvoLockStatus> = None;
let mut field_member_count: Option<i64> = None;
let mut field_member_limit: Option<i64> = None;
let mut field_join_request_count: Option<i64> = None;
let mut extra_cbor: Vec<(String, Vec<u8>)> = Vec::new();
for (key, value) in entries {
match key {
"name" => {
if let crate::cbor::Value::Text(s) = value {
field_name = Some(s.to_string());
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
}
}
"joinLink" => {
let raw = crate::cbor::encode_value(&value)?;
let mut dec = crate::cbor::Decoder::new(&raw);
field_join_link = Some(
crate::api::chat::bsky::GroupDefsJoinLinkView::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()));
}
}
"lockStatus" => {
if let crate::cbor::Value::Text(s) = value {
field_lock_status = Some(s.to_string());
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
}
}
"memberCount" => match value {
crate::cbor::Value::Unsigned(n) => {
field_member_count = Some(n as i64);
}
crate::cbor::Value::Signed(n) => {
field_member_count = Some(n);
}
_ => {
return Err(crate::cbor::CborError::InvalidCbor(
"expected integer".into(),
));
}
},
"memberLimit" => match value {
crate::cbor::Value::Unsigned(n) => {
field_member_limit = Some(n as i64);
}
crate::cbor::Value::Signed(n) => {
field_member_limit = Some(n);
}
_ => {
return Err(crate::cbor::CborError::InvalidCbor(
"expected integer".into(),
));
}
},
"joinRequestCount" => match value {
crate::cbor::Value::Unsigned(n) => {
field_join_request_count = Some(n as i64);
}
crate::cbor::Value::Signed(n) => {
field_join_request_count = Some(n);
}
_ => {
return Err(crate::cbor::CborError::InvalidCbor(
"expected integer".into(),
));
}
},
_ => {
let raw = crate::cbor::encode_value(&value)?;
extra_cbor.push((key.to_string(), raw));
}
}
}
Ok(ModerationDefsGroupConvo {
name: field_name.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'name'".into())
})?,
join_link: field_join_link,
created_at: field_created_at.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'createdAt'".into())
})?,
lock_status: field_lock_status.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'lockStatus'".into())
})?,
member_count: field_member_count.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'memberCount'".into())
})?,
member_limit: field_member_limit.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'memberLimit'".into())
})?,
join_request_count: field_join_request_count.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor(
"missing required field 'joinRequestCount'".into(),
)
})?,
extra: std::collections::HashMap::new(),
extra_cbor,
})
}
}