#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DraftDefsDraft {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub device_id: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub device_name: Option<String>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub langs: Vec<crate::syntax::Language>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub postgate_embedding_rules: Vec<DraftDefsDraftPostgateEmbeddingRulesUnion>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub posts: Vec<DraftDefsDraftPost>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub threadgate_allow: Vec<DraftDefsDraftThreadgateAllowUnion>,
#[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 DraftDefsDraftPostgateEmbeddingRulesUnion {
FeedPostgateDisableRule(Box<crate::api::app::bsky::FeedPostgateDisableRule>),
Unknown(crate::api::UnknownUnionVariant),
}
impl serde::Serialize for DraftDefsDraftPostgateEmbeddingRulesUnion {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
match self {
DraftDefsDraftPostgateEmbeddingRulesUnion::FeedPostgateDisableRule(inner) => {
let mut map =
serde_json::to_value(inner.as_ref()).map_err(serde::ser::Error::custom)?;
if let serde_json::Value::Object(ref mut m) = map {
m.insert(
"$type".to_string(),
serde_json::Value::String("app.bsky.feed.postgate#disableRule".to_string()),
);
}
map.serialize(serializer)
}
DraftDefsDraftPostgateEmbeddingRulesUnion::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 DraftDefsDraftPostgateEmbeddingRulesUnion {
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.feed.postgate#disableRule" => {
let inner: crate::api::app::bsky::FeedPostgateDisableRule =
serde_json::from_value(value).map_err(serde::de::Error::custom)?;
Ok(
DraftDefsDraftPostgateEmbeddingRulesUnion::FeedPostgateDisableRule(Box::new(
inner,
)),
)
}
_ => Ok(DraftDefsDraftPostgateEmbeddingRulesUnion::Unknown(
crate::api::UnknownUnionVariant {
r#type: type_str.to_string(),
json: Some(value),
cbor: None,
},
)),
}
}
}
impl DraftDefsDraftPostgateEmbeddingRulesUnion {
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 {
DraftDefsDraftPostgateEmbeddingRulesUnion::FeedPostgateDisableRule(inner) => {
inner.encode_cbor(buf)
}
DraftDefsDraftPostgateEmbeddingRulesUnion::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.feed.postgate#disableRule" => {
let mut dec = crate::cbor::Decoder::new(raw);
let inner = crate::api::app::bsky::FeedPostgateDisableRule::decode_cbor(&mut dec)?;
Ok(
DraftDefsDraftPostgateEmbeddingRulesUnion::FeedPostgateDisableRule(Box::new(
inner,
)),
)
}
_ => Ok(DraftDefsDraftPostgateEmbeddingRulesUnion::Unknown(
crate::api::UnknownUnionVariant {
r#type: type_str.to_string(),
json: None,
cbor: Some(raw.to_vec()),
},
)),
}
}
}
#[derive(Debug, Clone)]
pub enum DraftDefsDraftThreadgateAllowUnion {
FeedThreadgateMentionRule(Box<crate::api::app::bsky::FeedThreadgateMentionRule>),
FeedThreadgateFollowerRule(Box<crate::api::app::bsky::FeedThreadgateFollowerRule>),
FeedThreadgateFollowingRule(Box<crate::api::app::bsky::FeedThreadgateFollowingRule>),
FeedThreadgateListRule(Box<crate::api::app::bsky::FeedThreadgateListRule>),
Unknown(crate::api::UnknownUnionVariant),
}
impl serde::Serialize for DraftDefsDraftThreadgateAllowUnion {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
match self {
DraftDefsDraftThreadgateAllowUnion::FeedThreadgateMentionRule(inner) => {
let mut map =
serde_json::to_value(inner.as_ref()).map_err(serde::ser::Error::custom)?;
if let serde_json::Value::Object(ref mut m) = map {
m.insert(
"$type".to_string(),
serde_json::Value::String(
"app.bsky.feed.threadgate#mentionRule".to_string(),
),
);
}
map.serialize(serializer)
}
DraftDefsDraftThreadgateAllowUnion::FeedThreadgateFollowerRule(inner) => {
let mut map =
serde_json::to_value(inner.as_ref()).map_err(serde::ser::Error::custom)?;
if let serde_json::Value::Object(ref mut m) = map {
m.insert(
"$type".to_string(),
serde_json::Value::String(
"app.bsky.feed.threadgate#followerRule".to_string(),
),
);
}
map.serialize(serializer)
}
DraftDefsDraftThreadgateAllowUnion::FeedThreadgateFollowingRule(inner) => {
let mut map =
serde_json::to_value(inner.as_ref()).map_err(serde::ser::Error::custom)?;
if let serde_json::Value::Object(ref mut m) = map {
m.insert(
"$type".to_string(),
serde_json::Value::String(
"app.bsky.feed.threadgate#followingRule".to_string(),
),
);
}
map.serialize(serializer)
}
DraftDefsDraftThreadgateAllowUnion::FeedThreadgateListRule(inner) => {
let mut map =
serde_json::to_value(inner.as_ref()).map_err(serde::ser::Error::custom)?;
if let serde_json::Value::Object(ref mut m) = map {
m.insert(
"$type".to_string(),
serde_json::Value::String("app.bsky.feed.threadgate#listRule".to_string()),
);
}
map.serialize(serializer)
}
DraftDefsDraftThreadgateAllowUnion::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 DraftDefsDraftThreadgateAllowUnion {
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.feed.threadgate#mentionRule" => {
let inner: crate::api::app::bsky::FeedThreadgateMentionRule =
serde_json::from_value(value).map_err(serde::de::Error::custom)?;
Ok(DraftDefsDraftThreadgateAllowUnion::FeedThreadgateMentionRule(Box::new(inner)))
}
"app.bsky.feed.threadgate#followerRule" => {
let inner: crate::api::app::bsky::FeedThreadgateFollowerRule =
serde_json::from_value(value).map_err(serde::de::Error::custom)?;
Ok(DraftDefsDraftThreadgateAllowUnion::FeedThreadgateFollowerRule(Box::new(inner)))
}
"app.bsky.feed.threadgate#followingRule" => {
let inner: crate::api::app::bsky::FeedThreadgateFollowingRule =
serde_json::from_value(value).map_err(serde::de::Error::custom)?;
Ok(
DraftDefsDraftThreadgateAllowUnion::FeedThreadgateFollowingRule(Box::new(
inner,
)),
)
}
"app.bsky.feed.threadgate#listRule" => {
let inner: crate::api::app::bsky::FeedThreadgateListRule =
serde_json::from_value(value).map_err(serde::de::Error::custom)?;
Ok(DraftDefsDraftThreadgateAllowUnion::FeedThreadgateListRule(
Box::new(inner),
))
}
_ => Ok(DraftDefsDraftThreadgateAllowUnion::Unknown(
crate::api::UnknownUnionVariant {
r#type: type_str.to_string(),
json: Some(value),
cbor: None,
},
)),
}
}
}
impl DraftDefsDraftThreadgateAllowUnion {
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 {
DraftDefsDraftThreadgateAllowUnion::FeedThreadgateMentionRule(inner) => {
inner.encode_cbor(buf)
}
DraftDefsDraftThreadgateAllowUnion::FeedThreadgateFollowerRule(inner) => {
inner.encode_cbor(buf)
}
DraftDefsDraftThreadgateAllowUnion::FeedThreadgateFollowingRule(inner) => {
inner.encode_cbor(buf)
}
DraftDefsDraftThreadgateAllowUnion::FeedThreadgateListRule(inner) => {
inner.encode_cbor(buf)
}
DraftDefsDraftThreadgateAllowUnion::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.feed.threadgate#mentionRule" => {
let mut dec = crate::cbor::Decoder::new(raw);
let inner =
crate::api::app::bsky::FeedThreadgateMentionRule::decode_cbor(&mut dec)?;
Ok(DraftDefsDraftThreadgateAllowUnion::FeedThreadgateMentionRule(Box::new(inner)))
}
"app.bsky.feed.threadgate#followerRule" => {
let mut dec = crate::cbor::Decoder::new(raw);
let inner =
crate::api::app::bsky::FeedThreadgateFollowerRule::decode_cbor(&mut dec)?;
Ok(DraftDefsDraftThreadgateAllowUnion::FeedThreadgateFollowerRule(Box::new(inner)))
}
"app.bsky.feed.threadgate#followingRule" => {
let mut dec = crate::cbor::Decoder::new(raw);
let inner =
crate::api::app::bsky::FeedThreadgateFollowingRule::decode_cbor(&mut dec)?;
Ok(
DraftDefsDraftThreadgateAllowUnion::FeedThreadgateFollowingRule(Box::new(
inner,
)),
)
}
"app.bsky.feed.threadgate#listRule" => {
let mut dec = crate::cbor::Decoder::new(raw);
let inner = crate::api::app::bsky::FeedThreadgateListRule::decode_cbor(&mut dec)?;
Ok(DraftDefsDraftThreadgateAllowUnion::FeedThreadgateListRule(
Box::new(inner),
))
}
_ => Ok(DraftDefsDraftThreadgateAllowUnion::Unknown(
crate::api::UnknownUnionVariant {
r#type: type_str.to_string(),
json: None,
cbor: Some(raw.to_vec()),
},
)),
}
}
}
impl DraftDefsDraft {
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.langs.is_empty() {
count += 1;
}
if self.device_id.is_some() {
count += 1;
}
if self.device_name.is_some() {
count += 1;
}
if !self.threadgate_allow.is_empty() {
count += 1;
}
if !self.postgate_embedding_rules.is_empty() {
count += 1;
}
crate::cbor::Encoder::new(&mut *buf).encode_map_header(count)?;
if !self.langs.is_empty() {
crate::cbor::Encoder::new(&mut *buf).encode_text("langs")?;
crate::cbor::Encoder::new(&mut *buf).encode_array_header(self.langs.len() as u64)?;
for item in &self.langs {
crate::cbor::Encoder::new(&mut *buf).encode_text(item.as_str())?;
}
}
crate::cbor::Encoder::new(&mut *buf).encode_text("posts")?;
crate::cbor::Encoder::new(&mut *buf).encode_array_header(self.posts.len() as u64)?;
for item in &self.posts {
item.encode_cbor(buf)?;
}
if self.device_id.is_some() {
crate::cbor::Encoder::new(&mut *buf).encode_text("deviceId")?;
if let Some(ref val) = self.device_id {
crate::cbor::Encoder::new(&mut *buf).encode_text(val)?;
}
}
if self.device_name.is_some() {
crate::cbor::Encoder::new(&mut *buf).encode_text("deviceName")?;
if let Some(ref val) = self.device_name {
crate::cbor::Encoder::new(&mut *buf).encode_text(val)?;
}
}
if !self.threadgate_allow.is_empty() {
crate::cbor::Encoder::new(&mut *buf).encode_text("threadgateAllow")?;
crate::cbor::Encoder::new(&mut *buf)
.encode_array_header(self.threadgate_allow.len() as u64)?;
for item in &self.threadgate_allow {
item.encode_cbor(buf)?;
}
}
if !self.postgate_embedding_rules.is_empty() {
crate::cbor::Encoder::new(&mut *buf).encode_text("postgateEmbeddingRules")?;
crate::cbor::Encoder::new(&mut *buf)
.encode_array_header(self.postgate_embedding_rules.len() as u64)?;
for item in &self.postgate_embedding_rules {
item.encode_cbor(buf)?;
}
}
} else {
let mut pairs: Vec<(&str, Vec<u8>)> = Vec::new();
if !self.langs.is_empty() {
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_array_header(self.langs.len() as u64)?;
for item in &self.langs {
crate::cbor::Encoder::new(&mut vbuf).encode_text(item.as_str())?;
}
pairs.push(("langs", vbuf));
}
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_array_header(self.posts.len() as u64)?;
for item in &self.posts {
item.encode_cbor(&mut vbuf)?;
}
pairs.push(("posts", vbuf));
}
if self.device_id.is_some() {
let mut vbuf = Vec::new();
if let Some(ref val) = self.device_id {
crate::cbor::Encoder::new(&mut vbuf).encode_text(val)?;
}
pairs.push(("deviceId", vbuf));
}
if self.device_name.is_some() {
let mut vbuf = Vec::new();
if let Some(ref val) = self.device_name {
crate::cbor::Encoder::new(&mut vbuf).encode_text(val)?;
}
pairs.push(("deviceName", vbuf));
}
if !self.threadgate_allow.is_empty() {
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf)
.encode_array_header(self.threadgate_allow.len() as u64)?;
for item in &self.threadgate_allow {
item.encode_cbor(&mut vbuf)?;
}
pairs.push(("threadgateAllow", vbuf));
}
if !self.postgate_embedding_rules.is_empty() {
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf)
.encode_array_header(self.postgate_embedding_rules.len() as u64)?;
for item in &self.postgate_embedding_rules {
item.encode_cbor(&mut vbuf)?;
}
pairs.push(("postgateEmbeddingRules", 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_langs: Vec<crate::syntax::Language> = Vec::new();
let mut field_posts: Vec<DraftDefsDraftPost> = Vec::new();
let mut field_device_id: Option<String> = None;
let mut field_device_name: Option<String> = None;
let mut field_threadgate_allow: Vec<DraftDefsDraftThreadgateAllowUnion> = Vec::new();
let mut field_postgate_embedding_rules: Vec<DraftDefsDraftPostgateEmbeddingRulesUnion> =
Vec::new();
let mut extra_cbor: Vec<(String, Vec<u8>)> = Vec::new();
for (key, value) in entries {
match key {
"langs" => {
if let crate::cbor::Value::Array(items) = value {
for item in items {
if let crate::cbor::Value::Text(s) = item {
field_langs.push(crate::syntax::Language::try_from(s).map_err(
|e| crate::cbor::CborError::InvalidCbor(e.to_string()),
)?);
} else {
return Err(crate::cbor::CborError::InvalidCbor(
"expected text in array".into(),
));
}
}
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected array".into()));
}
}
"posts" => {
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_posts.push(DraftDefsDraftPost::decode_cbor(&mut dec)?);
}
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected array".into()));
}
}
"deviceId" => {
if let crate::cbor::Value::Text(s) = value {
field_device_id = Some(s.to_string());
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
}
}
"deviceName" => {
if let crate::cbor::Value::Text(s) = value {
field_device_name = Some(s.to_string());
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
}
}
"threadgateAllow" => {
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_threadgate_allow
.push(DraftDefsDraftThreadgateAllowUnion::decode_cbor(&mut dec)?);
}
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected array".into()));
}
}
"postgateEmbeddingRules" => {
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_postgate_embedding_rules.push(
DraftDefsDraftPostgateEmbeddingRulesUnion::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(DraftDefsDraft {
langs: field_langs,
posts: field_posts,
device_id: field_device_id,
device_name: field_device_name,
threadgate_allow: field_threadgate_allow,
postgate_embedding_rules: field_postgate_embedding_rules,
extra: std::collections::HashMap::new(),
extra_cbor,
})
}
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DraftDefsDraftEmbedCaption {
pub content: String,
pub lang: crate::syntax::Language,
#[serde(flatten)]
pub extra: std::collections::HashMap<String, serde_json::Value>,
#[serde(skip)]
pub extra_cbor: Vec<(String, Vec<u8>)>,
}
impl DraftDefsDraftEmbedCaption {
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("lang")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(self.lang.as_str())?;
crate::cbor::Encoder::new(&mut *buf).encode_text("content")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(&self.content)?;
} else {
let mut pairs: Vec<(&str, Vec<u8>)> = Vec::new();
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(self.lang.as_str())?;
pairs.push(("lang", vbuf));
}
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(&self.content)?;
pairs.push(("content", 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_lang: Option<crate::syntax::Language> = None;
let mut field_content: Option<String> = None;
let mut extra_cbor: Vec<(String, Vec<u8>)> = Vec::new();
for (key, value) in entries {
match key {
"lang" => {
if let crate::cbor::Value::Text(s) = value {
field_lang = Some(
crate::syntax::Language::try_from(s)
.map_err(|e| crate::cbor::CborError::InvalidCbor(e.to_string()))?,
);
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
}
}
"content" => {
if let crate::cbor::Value::Text(s) = value {
field_content = 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(DraftDefsDraftEmbedCaption {
lang: field_lang.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'lang'".into())
})?,
content: field_content.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'content'".into())
})?,
extra: std::collections::HashMap::new(),
extra_cbor,
})
}
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DraftDefsDraftEmbedExternal {
pub uri: String,
#[serde(flatten)]
pub extra: std::collections::HashMap<String, serde_json::Value>,
#[serde(skip)]
pub extra_cbor: Vec<(String, Vec<u8>)>,
}
impl DraftDefsDraftEmbedExternal {
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("uri")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(&self.uri)?;
} else {
let mut pairs: Vec<(&str, Vec<u8>)> = Vec::new();
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(&self.uri)?;
pairs.push(("uri", vbuf));
}
for (k, v) in &self.extra_cbor {
pairs.push((k.as_str(), v.clone()));
}
pairs.sort_by(|a, b| crate::cbor::cbor_key_cmp(a.0, b.0));
crate::cbor::Encoder::new(&mut *buf).encode_map_header(pairs.len() as u64)?;
for (k, v) in &pairs {
crate::cbor::Encoder::new(&mut *buf).encode_text(k)?;
buf.extend_from_slice(v);
}
}
Ok(())
}
pub fn from_cbor(data: &[u8]) -> Result<Self, crate::cbor::CborError> {
let mut decoder = crate::cbor::Decoder::new(data);
let result = Self::decode_cbor(&mut decoder)?;
if !decoder.is_empty() {
return Err(crate::cbor::CborError::InvalidCbor("trailing data".into()));
}
Ok(result)
}
pub fn decode_cbor(decoder: &mut crate::cbor::Decoder) -> Result<Self, crate::cbor::CborError> {
let val = decoder.decode()?;
let entries = match val {
crate::cbor::Value::Map(entries) => entries,
_ => return Err(crate::cbor::CborError::InvalidCbor("expected map".into())),
};
let mut field_uri: Option<String> = None;
let mut extra_cbor: Vec<(String, Vec<u8>)> = Vec::new();
for (key, value) in entries {
match key {
"uri" => {
if let crate::cbor::Value::Text(s) = value {
field_uri = Some(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(DraftDefsDraftEmbedExternal {
uri: field_uri.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'uri'".into())
})?,
extra: std::collections::HashMap::new(),
extra_cbor,
})
}
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DraftDefsDraftEmbedImage {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub alt: Option<String>,
pub local_ref: DraftDefsDraftEmbedLocalRef,
#[serde(flatten)]
pub extra: std::collections::HashMap<String, serde_json::Value>,
#[serde(skip)]
pub extra_cbor: Vec<(String, Vec<u8>)>,
}
impl DraftDefsDraftEmbedImage {
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.alt.is_some() {
count += 1;
}
crate::cbor::Encoder::new(&mut *buf).encode_map_header(count)?;
if self.alt.is_some() {
crate::cbor::Encoder::new(&mut *buf).encode_text("alt")?;
if let Some(ref val) = self.alt {
crate::cbor::Encoder::new(&mut *buf).encode_text(val)?;
}
}
crate::cbor::Encoder::new(&mut *buf).encode_text("localRef")?;
self.local_ref.encode_cbor(buf)?;
} else {
let mut pairs: Vec<(&str, Vec<u8>)> = Vec::new();
if self.alt.is_some() {
let mut vbuf = Vec::new();
if let Some(ref val) = self.alt {
crate::cbor::Encoder::new(&mut vbuf).encode_text(val)?;
}
pairs.push(("alt", vbuf));
}
{
let mut vbuf = Vec::new();
self.local_ref.encode_cbor(&mut vbuf)?;
pairs.push(("localRef", 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_alt: Option<String> = None;
let mut field_local_ref: Option<DraftDefsDraftEmbedLocalRef> = None;
let mut extra_cbor: Vec<(String, Vec<u8>)> = Vec::new();
for (key, value) in entries {
match key {
"alt" => {
if let crate::cbor::Value::Text(s) = value {
field_alt = Some(s.to_string());
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
}
}
"localRef" => {
let raw = crate::cbor::encode_value(&value)?;
let mut dec = crate::cbor::Decoder::new(&raw);
field_local_ref = Some(DraftDefsDraftEmbedLocalRef::decode_cbor(&mut dec)?);
}
_ => {
let raw = crate::cbor::encode_value(&value)?;
extra_cbor.push((key.to_string(), raw));
}
}
}
Ok(DraftDefsDraftEmbedImage {
alt: field_alt,
local_ref: field_local_ref.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'localRef'".into())
})?,
extra: std::collections::HashMap::new(),
extra_cbor,
})
}
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DraftDefsDraftEmbedLocalRef {
pub path: String,
#[serde(flatten)]
pub extra: std::collections::HashMap<String, serde_json::Value>,
#[serde(skip)]
pub extra_cbor: Vec<(String, Vec<u8>)>,
}
impl DraftDefsDraftEmbedLocalRef {
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("path")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(&self.path)?;
} else {
let mut pairs: Vec<(&str, Vec<u8>)> = Vec::new();
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(&self.path)?;
pairs.push(("path", 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_path: Option<String> = None;
let mut extra_cbor: Vec<(String, Vec<u8>)> = Vec::new();
for (key, value) in entries {
match key {
"path" => {
if let crate::cbor::Value::Text(s) = value {
field_path = 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(DraftDefsDraftEmbedLocalRef {
path: field_path.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'path'".into())
})?,
extra: std::collections::HashMap::new(),
extra_cbor,
})
}
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DraftDefsDraftEmbedRecord {
pub record: crate::api::com::atproto::RepoStrongRef,
#[serde(flatten)]
pub extra: std::collections::HashMap<String, serde_json::Value>,
#[serde(skip)]
pub extra_cbor: Vec<(String, Vec<u8>)>,
}
impl DraftDefsDraftEmbedRecord {
pub fn to_cbor(&self) -> Result<Vec<u8>, crate::cbor::CborError> {
let mut buf = Vec::new();
self.encode_cbor(&mut buf)?;
Ok(buf)
}
pub fn encode_cbor(&self, buf: &mut Vec<u8>) -> Result<(), crate::cbor::CborError> {
if self.extra_cbor.is_empty() {
let count = 1u64;
crate::cbor::Encoder::new(&mut *buf).encode_map_header(count)?;
crate::cbor::Encoder::new(&mut *buf).encode_text("record")?;
self.record.encode_cbor(buf)?;
} else {
let mut pairs: Vec<(&str, Vec<u8>)> = Vec::new();
{
let mut vbuf = Vec::new();
self.record.encode_cbor(&mut vbuf)?;
pairs.push(("record", vbuf));
}
for (k, v) in &self.extra_cbor {
pairs.push((k.as_str(), v.clone()));
}
pairs.sort_by(|a, b| crate::cbor::cbor_key_cmp(a.0, b.0));
crate::cbor::Encoder::new(&mut *buf).encode_map_header(pairs.len() as u64)?;
for (k, v) in &pairs {
crate::cbor::Encoder::new(&mut *buf).encode_text(k)?;
buf.extend_from_slice(v);
}
}
Ok(())
}
pub fn from_cbor(data: &[u8]) -> Result<Self, crate::cbor::CborError> {
let mut decoder = crate::cbor::Decoder::new(data);
let result = Self::decode_cbor(&mut decoder)?;
if !decoder.is_empty() {
return Err(crate::cbor::CborError::InvalidCbor("trailing data".into()));
}
Ok(result)
}
pub fn decode_cbor(decoder: &mut crate::cbor::Decoder) -> Result<Self, crate::cbor::CborError> {
let val = decoder.decode()?;
let entries = match val {
crate::cbor::Value::Map(entries) => entries,
_ => return Err(crate::cbor::CborError::InvalidCbor("expected map".into())),
};
let mut field_record: Option<crate::api::com::atproto::RepoStrongRef> = None;
let mut extra_cbor: Vec<(String, Vec<u8>)> = Vec::new();
for (key, value) in entries {
match key {
"record" => {
let raw = crate::cbor::encode_value(&value)?;
let mut dec = crate::cbor::Decoder::new(&raw);
field_record = Some(crate::api::com::atproto::RepoStrongRef::decode_cbor(
&mut dec,
)?);
}
_ => {
let raw = crate::cbor::encode_value(&value)?;
extra_cbor.push((key.to_string(), raw));
}
}
}
Ok(DraftDefsDraftEmbedRecord {
record: field_record.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'record'".into())
})?,
extra: std::collections::HashMap::new(),
extra_cbor,
})
}
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DraftDefsDraftEmbedVideo {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub alt: Option<String>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub captions: Vec<DraftDefsDraftEmbedCaption>,
pub local_ref: DraftDefsDraftEmbedLocalRef,
#[serde(flatten)]
pub extra: std::collections::HashMap<String, serde_json::Value>,
#[serde(skip)]
pub extra_cbor: Vec<(String, Vec<u8>)>,
}
impl DraftDefsDraftEmbedVideo {
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.alt.is_some() {
count += 1;
}
if !self.captions.is_empty() {
count += 1;
}
crate::cbor::Encoder::new(&mut *buf).encode_map_header(count)?;
if self.alt.is_some() {
crate::cbor::Encoder::new(&mut *buf).encode_text("alt")?;
if let Some(ref val) = self.alt {
crate::cbor::Encoder::new(&mut *buf).encode_text(val)?;
}
}
if !self.captions.is_empty() {
crate::cbor::Encoder::new(&mut *buf).encode_text("captions")?;
crate::cbor::Encoder::new(&mut *buf)
.encode_array_header(self.captions.len() as u64)?;
for item in &self.captions {
item.encode_cbor(buf)?;
}
}
crate::cbor::Encoder::new(&mut *buf).encode_text("localRef")?;
self.local_ref.encode_cbor(buf)?;
} else {
let mut pairs: Vec<(&str, Vec<u8>)> = Vec::new();
if self.alt.is_some() {
let mut vbuf = Vec::new();
if let Some(ref val) = self.alt {
crate::cbor::Encoder::new(&mut vbuf).encode_text(val)?;
}
pairs.push(("alt", vbuf));
}
if !self.captions.is_empty() {
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf)
.encode_array_header(self.captions.len() as u64)?;
for item in &self.captions {
item.encode_cbor(&mut vbuf)?;
}
pairs.push(("captions", vbuf));
}
{
let mut vbuf = Vec::new();
self.local_ref.encode_cbor(&mut vbuf)?;
pairs.push(("localRef", 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_alt: Option<String> = None;
let mut field_captions: Vec<DraftDefsDraftEmbedCaption> = Vec::new();
let mut field_local_ref: Option<DraftDefsDraftEmbedLocalRef> = None;
let mut extra_cbor: Vec<(String, Vec<u8>)> = Vec::new();
for (key, value) in entries {
match key {
"alt" => {
if let crate::cbor::Value::Text(s) = value {
field_alt = Some(s.to_string());
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
}
}
"captions" => {
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_captions.push(DraftDefsDraftEmbedCaption::decode_cbor(&mut dec)?);
}
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected array".into()));
}
}
"localRef" => {
let raw = crate::cbor::encode_value(&value)?;
let mut dec = crate::cbor::Decoder::new(&raw);
field_local_ref = Some(DraftDefsDraftEmbedLocalRef::decode_cbor(&mut dec)?);
}
_ => {
let raw = crate::cbor::encode_value(&value)?;
extra_cbor.push((key.to_string(), raw));
}
}
}
Ok(DraftDefsDraftEmbedVideo {
alt: field_alt,
captions: field_captions,
local_ref: field_local_ref.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'localRef'".into())
})?,
extra: std::collections::HashMap::new(),
extra_cbor,
})
}
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DraftDefsDraftPost {
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub embed_externals: Vec<DraftDefsDraftEmbedExternal>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub embed_images: Vec<DraftDefsDraftEmbedImage>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub embed_records: Vec<DraftDefsDraftEmbedRecord>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub embed_videos: Vec<DraftDefsDraftEmbedVideo>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub labels: Option<DraftDefsDraftPostLabelsUnion>,
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 DraftDefsDraftPostLabelsUnion {
LabelDefsSelfLabels(Box<crate::api::com::atproto::LabelDefsSelfLabels>),
Unknown(crate::api::UnknownUnionVariant),
}
impl serde::Serialize for DraftDefsDraftPostLabelsUnion {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
match self {
DraftDefsDraftPostLabelsUnion::LabelDefsSelfLabels(inner) => {
let mut map =
serde_json::to_value(inner.as_ref()).map_err(serde::ser::Error::custom)?;
if let serde_json::Value::Object(ref mut m) = map {
m.insert(
"$type".to_string(),
serde_json::Value::String("com.atproto.label.defs#selfLabels".to_string()),
);
}
map.serialize(serializer)
}
DraftDefsDraftPostLabelsUnion::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 DraftDefsDraftPostLabelsUnion {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
let value = serde_json::Value::deserialize(deserializer)?;
let type_str = value
.get("$type")
.and_then(|v| v.as_str())
.unwrap_or_default();
match type_str {
"com.atproto.label.defs#selfLabels" => {
let inner: crate::api::com::atproto::LabelDefsSelfLabels =
serde_json::from_value(value).map_err(serde::de::Error::custom)?;
Ok(DraftDefsDraftPostLabelsUnion::LabelDefsSelfLabels(
Box::new(inner),
))
}
_ => Ok(DraftDefsDraftPostLabelsUnion::Unknown(
crate::api::UnknownUnionVariant {
r#type: type_str.to_string(),
json: Some(value),
cbor: None,
},
)),
}
}
}
impl DraftDefsDraftPostLabelsUnion {
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 {
DraftDefsDraftPostLabelsUnion::LabelDefsSelfLabels(inner) => inner.encode_cbor(buf),
DraftDefsDraftPostLabelsUnion::Unknown(v) => {
if let Some(ref data) = v.cbor {
buf.extend_from_slice(data);
Ok(())
} else {
Err(crate::cbor::CborError::InvalidCbor(
"no CBOR data for unknown union variant".into(),
))
}
}
}
}
pub fn from_cbor(data: &[u8]) -> Result<Self, crate::cbor::CborError> {
let mut decoder = crate::cbor::Decoder::new(data);
let result = Self::decode_cbor(&mut decoder)?;
if !decoder.is_empty() {
return Err(crate::cbor::CborError::InvalidCbor("trailing data".into()));
}
Ok(result)
}
pub fn decode_cbor(decoder: &mut crate::cbor::Decoder) -> Result<Self, crate::cbor::CborError> {
let start = decoder.position();
let val = decoder.decode()?;
let end = decoder.position();
let raw = &decoder.raw_input()[start..end];
let entries = match val {
crate::cbor::Value::Map(entries) => entries,
_ => {
return Err(crate::cbor::CborError::InvalidCbor(
"expected map for union".into(),
));
}
};
let type_str = entries
.iter()
.find(|(k, _)| *k == "$type")
.and_then(|(_, v)| match v {
crate::cbor::Value::Text(s) => Some(*s),
_ => None,
})
.unwrap_or_default();
match type_str {
"com.atproto.label.defs#selfLabels" => {
let mut dec = crate::cbor::Decoder::new(raw);
let inner = crate::api::com::atproto::LabelDefsSelfLabels::decode_cbor(&mut dec)?;
Ok(DraftDefsDraftPostLabelsUnion::LabelDefsSelfLabels(
Box::new(inner),
))
}
_ => Ok(DraftDefsDraftPostLabelsUnion::Unknown(
crate::api::UnknownUnionVariant {
r#type: type_str.to_string(),
json: None,
cbor: Some(raw.to_vec()),
},
)),
}
}
}
impl DraftDefsDraftPost {
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.labels.is_some() {
count += 1;
}
if !self.embed_images.is_empty() {
count += 1;
}
if !self.embed_videos.is_empty() {
count += 1;
}
if !self.embed_records.is_empty() {
count += 1;
}
if !self.embed_externals.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.labels.is_some() {
crate::cbor::Encoder::new(&mut *buf).encode_text("labels")?;
if let Some(ref val) = self.labels {
val.encode_cbor(buf)?;
}
}
if !self.embed_images.is_empty() {
crate::cbor::Encoder::new(&mut *buf).encode_text("embedImages")?;
crate::cbor::Encoder::new(&mut *buf)
.encode_array_header(self.embed_images.len() as u64)?;
for item in &self.embed_images {
item.encode_cbor(buf)?;
}
}
if !self.embed_videos.is_empty() {
crate::cbor::Encoder::new(&mut *buf).encode_text("embedVideos")?;
crate::cbor::Encoder::new(&mut *buf)
.encode_array_header(self.embed_videos.len() as u64)?;
for item in &self.embed_videos {
item.encode_cbor(buf)?;
}
}
if !self.embed_records.is_empty() {
crate::cbor::Encoder::new(&mut *buf).encode_text("embedRecords")?;
crate::cbor::Encoder::new(&mut *buf)
.encode_array_header(self.embed_records.len() as u64)?;
for item in &self.embed_records {
item.encode_cbor(buf)?;
}
}
if !self.embed_externals.is_empty() {
crate::cbor::Encoder::new(&mut *buf).encode_text("embedExternals")?;
crate::cbor::Encoder::new(&mut *buf)
.encode_array_header(self.embed_externals.len() as u64)?;
for item in &self.embed_externals {
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.labels.is_some() {
let mut vbuf = Vec::new();
if let Some(ref val) = self.labels {
val.encode_cbor(&mut vbuf)?;
}
pairs.push(("labels", vbuf));
}
if !self.embed_images.is_empty() {
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf)
.encode_array_header(self.embed_images.len() as u64)?;
for item in &self.embed_images {
item.encode_cbor(&mut vbuf)?;
}
pairs.push(("embedImages", vbuf));
}
if !self.embed_videos.is_empty() {
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf)
.encode_array_header(self.embed_videos.len() as u64)?;
for item in &self.embed_videos {
item.encode_cbor(&mut vbuf)?;
}
pairs.push(("embedVideos", vbuf));
}
if !self.embed_records.is_empty() {
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf)
.encode_array_header(self.embed_records.len() as u64)?;
for item in &self.embed_records {
item.encode_cbor(&mut vbuf)?;
}
pairs.push(("embedRecords", vbuf));
}
if !self.embed_externals.is_empty() {
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf)
.encode_array_header(self.embed_externals.len() as u64)?;
for item in &self.embed_externals {
item.encode_cbor(&mut vbuf)?;
}
pairs.push(("embedExternals", 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_labels: Option<DraftDefsDraftPostLabelsUnion> = None;
let mut field_embed_images: Vec<DraftDefsDraftEmbedImage> = Vec::new();
let mut field_embed_videos: Vec<DraftDefsDraftEmbedVideo> = Vec::new();
let mut field_embed_records: Vec<DraftDefsDraftEmbedRecord> = Vec::new();
let mut field_embed_externals: Vec<DraftDefsDraftEmbedExternal> = 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()));
}
}
"labels" => {
let raw = crate::cbor::encode_value(&value)?;
let mut dec = crate::cbor::Decoder::new(&raw);
field_labels = Some(DraftDefsDraftPostLabelsUnion::decode_cbor(&mut dec)?);
}
"embedImages" => {
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_embed_images
.push(DraftDefsDraftEmbedImage::decode_cbor(&mut dec)?);
}
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected array".into()));
}
}
"embedVideos" => {
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_embed_videos
.push(DraftDefsDraftEmbedVideo::decode_cbor(&mut dec)?);
}
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected array".into()));
}
}
"embedRecords" => {
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_embed_records
.push(DraftDefsDraftEmbedRecord::decode_cbor(&mut dec)?);
}
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected array".into()));
}
}
"embedExternals" => {
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_embed_externals
.push(DraftDefsDraftEmbedExternal::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(DraftDefsDraftPost {
text: field_text.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'text'".into())
})?,
labels: field_labels,
embed_images: field_embed_images,
embed_videos: field_embed_videos,
embed_records: field_embed_records,
embed_externals: field_embed_externals,
extra: std::collections::HashMap::new(),
extra_cbor,
})
}
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DraftDefsDraftView {
pub created_at: crate::syntax::Datetime,
pub draft: DraftDefsDraft,
pub id: crate::syntax::Tid,
pub updated_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 DraftDefsDraftView {
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")?;
{
let __s = self.id.to_string();
crate::cbor::Encoder::new(&mut *buf).encode_text(&__s)?;
}
crate::cbor::Encoder::new(&mut *buf).encode_text("draft")?;
self.draft.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("updatedAt")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(self.updated_at.as_str())?;
} else {
let mut pairs: Vec<(&str, Vec<u8>)> = Vec::new();
{
let mut vbuf = Vec::new();
{
let __s = self.id.to_string();
crate::cbor::Encoder::new(&mut vbuf).encode_text(&__s)?;
}
pairs.push(("id", vbuf));
}
{
let mut vbuf = Vec::new();
self.draft.encode_cbor(&mut vbuf)?;
pairs.push(("draft", 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.updated_at.as_str())?;
pairs.push(("updatedAt", 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<crate::syntax::Tid> = None;
let mut field_draft: Option<DraftDefsDraft> = None;
let mut field_created_at: Option<crate::syntax::Datetime> = None;
let mut field_updated_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(
crate::syntax::Tid::try_from(s)
.map_err(|e| crate::cbor::CborError::InvalidCbor(e.to_string()))?,
);
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
}
}
"draft" => {
let raw = crate::cbor::encode_value(&value)?;
let mut dec = crate::cbor::Decoder::new(&raw);
field_draft = Some(DraftDefsDraft::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()));
}
}
"updatedAt" => {
if let crate::cbor::Value::Text(s) = value {
field_updated_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(DraftDefsDraftView {
id: field_id.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'id'".into())
})?,
draft: field_draft.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'draft'".into())
})?,
created_at: field_created_at.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'createdAt'".into())
})?,
updated_at: field_updated_at.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'updatedAt'".into())
})?,
extra: std::collections::HashMap::new(),
extra_cbor,
})
}
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DraftDefsDraftWithId {
pub draft: DraftDefsDraft,
pub id: crate::syntax::Tid,
#[serde(flatten)]
pub extra: std::collections::HashMap<String, serde_json::Value>,
#[serde(skip)]
pub extra_cbor: Vec<(String, Vec<u8>)>,
}
impl DraftDefsDraftWithId {
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("id")?;
{
let __s = self.id.to_string();
crate::cbor::Encoder::new(&mut *buf).encode_text(&__s)?;
}
crate::cbor::Encoder::new(&mut *buf).encode_text("draft")?;
self.draft.encode_cbor(buf)?;
} else {
let mut pairs: Vec<(&str, Vec<u8>)> = Vec::new();
{
let mut vbuf = Vec::new();
{
let __s = self.id.to_string();
crate::cbor::Encoder::new(&mut vbuf).encode_text(&__s)?;
}
pairs.push(("id", vbuf));
}
{
let mut vbuf = Vec::new();
self.draft.encode_cbor(&mut vbuf)?;
pairs.push(("draft", 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<crate::syntax::Tid> = None;
let mut field_draft: Option<DraftDefsDraft> = 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(
crate::syntax::Tid::try_from(s)
.map_err(|e| crate::cbor::CborError::InvalidCbor(e.to_string()))?,
);
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
}
}
"draft" => {
let raw = crate::cbor::encode_value(&value)?;
let mut dec = crate::cbor::Decoder::new(&raw);
field_draft = Some(DraftDefsDraft::decode_cbor(&mut dec)?);
}
_ => {
let raw = crate::cbor::encode_value(&value)?;
extra_cbor.push((key.to_string(), raw));
}
}
}
Ok(DraftDefsDraftWithId {
id: field_id.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'id'".into())
})?,
draft: field_draft.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'draft'".into())
})?,
extra: std::collections::HashMap::new(),
extra_cbor,
})
}
}