#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct EmbedExternalColorRGB {
pub b: i64,
pub g: i64,
pub r: i64,
#[serde(flatten)]
pub extra: std::collections::HashMap<String, serde_json::Value>,
#[serde(skip)]
pub extra_cbor: Vec<(String, Vec<u8>)>,
}
impl EmbedExternalColorRGB {
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("b")?;
crate::cbor::Encoder::new(&mut *buf).encode_i64(self.b)?;
crate::cbor::Encoder::new(&mut *buf).encode_text("g")?;
crate::cbor::Encoder::new(&mut *buf).encode_i64(self.g)?;
crate::cbor::Encoder::new(&mut *buf).encode_text("r")?;
crate::cbor::Encoder::new(&mut *buf).encode_i64(self.r)?;
} else {
let mut pairs: Vec<(&str, Vec<u8>)> = Vec::new();
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_i64(self.b)?;
pairs.push(("b", vbuf));
}
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_i64(self.g)?;
pairs.push(("g", vbuf));
}
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_i64(self.r)?;
pairs.push(("r", 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_b: Option<i64> = None;
let mut field_g: Option<i64> = None;
let mut field_r: Option<i64> = None;
let mut extra_cbor: Vec<(String, Vec<u8>)> = Vec::new();
for (key, value) in entries {
match key {
"b" => match value {
crate::cbor::Value::Unsigned(n) => {
field_b = Some(n as i64);
}
crate::cbor::Value::Signed(n) => {
field_b = Some(n);
}
_ => {
return Err(crate::cbor::CborError::InvalidCbor(
"expected integer".into(),
));
}
},
"g" => match value {
crate::cbor::Value::Unsigned(n) => {
field_g = Some(n as i64);
}
crate::cbor::Value::Signed(n) => {
field_g = Some(n);
}
_ => {
return Err(crate::cbor::CborError::InvalidCbor(
"expected integer".into(),
));
}
},
"r" => match value {
crate::cbor::Value::Unsigned(n) => {
field_r = Some(n as i64);
}
crate::cbor::Value::Signed(n) => {
field_r = 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(EmbedExternalColorRGB {
b: field_b.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'b'".into())
})?,
g: field_g.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'g'".into())
})?,
r: field_r.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'r'".into())
})?,
extra: std::collections::HashMap::new(),
extra_cbor,
})
}
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct EmbedExternalExternal {
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub associated_refs: Vec<crate::api::com::atproto::RepoStrongRef>,
pub description: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub thumb: Option<crate::api::Blob>,
pub title: String,
pub uri: String,
#[serde(flatten)]
pub extra: std::collections::HashMap<String, serde_json::Value>,
#[serde(skip)]
pub extra_cbor: Vec<(String, Vec<u8>)>,
}
impl EmbedExternalExternal {
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 = 3u64;
if self.thumb.is_some() {
count += 1;
}
if !self.associated_refs.is_empty() {
count += 1;
}
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)?;
if self.thumb.is_some() {
crate::cbor::Encoder::new(&mut *buf).encode_text("thumb")?;
if let Some(ref val) = self.thumb {
val.encode_cbor(buf)?;
}
}
crate::cbor::Encoder::new(&mut *buf).encode_text("title")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(&self.title)?;
crate::cbor::Encoder::new(&mut *buf).encode_text("description")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(&self.description)?;
if !self.associated_refs.is_empty() {
crate::cbor::Encoder::new(&mut *buf).encode_text("associatedRefs")?;
crate::cbor::Encoder::new(&mut *buf)
.encode_array_header(self.associated_refs.len() as u64)?;
for item in &self.associated_refs {
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.uri)?;
pairs.push(("uri", vbuf));
}
if self.thumb.is_some() {
let mut vbuf = Vec::new();
if let Some(ref val) = self.thumb {
val.encode_cbor(&mut vbuf)?;
}
pairs.push(("thumb", vbuf));
}
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(&self.title)?;
pairs.push(("title", vbuf));
}
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(&self.description)?;
pairs.push(("description", vbuf));
}
if !self.associated_refs.is_empty() {
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf)
.encode_array_header(self.associated_refs.len() as u64)?;
for item in &self.associated_refs {
item.encode_cbor(&mut vbuf)?;
}
pairs.push(("associatedRefs", 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 field_thumb: Option<crate::api::Blob> = None;
let mut field_title: Option<String> = None;
let mut field_description: Option<String> = None;
let mut field_associated_refs: Vec<crate::api::com::atproto::RepoStrongRef> = Vec::new();
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()));
}
}
"thumb" => {
let raw = crate::cbor::encode_value(&value)?;
let mut dec = crate::cbor::Decoder::new(&raw);
field_thumb = Some(crate::api::Blob::decode_cbor(&mut dec)?);
}
"title" => {
if let crate::cbor::Value::Text(s) = value {
field_title = Some(s.to_string());
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
}
}
"description" => {
if let crate::cbor::Value::Text(s) = value {
field_description = Some(s.to_string());
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
}
}
"associatedRefs" => {
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_associated_refs.push(
crate::api::com::atproto::RepoStrongRef::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(EmbedExternalExternal {
uri: field_uri.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'uri'".into())
})?,
thumb: field_thumb,
title: field_title.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'title'".into())
})?,
description: field_description.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'description'".into())
})?,
associated_refs: field_associated_refs,
extra: std::collections::HashMap::new(),
extra_cbor,
})
}
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct EmbedExternal {
pub external: EmbedExternalExternal,
#[serde(flatten)]
pub extra: std::collections::HashMap<String, serde_json::Value>,
#[serde(skip)]
pub extra_cbor: Vec<(String, Vec<u8>)>,
}
impl EmbedExternal {
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("external")?;
self.external.encode_cbor(buf)?;
} else {
let mut pairs: Vec<(&str, Vec<u8>)> = Vec::new();
{
let mut vbuf = Vec::new();
self.external.encode_cbor(&mut vbuf)?;
pairs.push(("external", 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_external: Option<EmbedExternalExternal> = None;
let mut extra_cbor: Vec<(String, Vec<u8>)> = Vec::new();
for (key, value) in entries {
match key {
"external" => {
let raw = crate::cbor::encode_value(&value)?;
let mut dec = crate::cbor::Decoder::new(&raw);
field_external = Some(EmbedExternalExternal::decode_cbor(&mut dec)?);
}
_ => {
let raw = crate::cbor::encode_value(&value)?;
extra_cbor.push((key.to_string(), raw));
}
}
}
Ok(EmbedExternal {
external: field_external.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'external'".into())
})?,
extra: std::collections::HashMap::new(),
extra_cbor,
})
}
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct EmbedExternalView {
pub external: EmbedExternalViewExternal,
#[serde(flatten)]
pub extra: std::collections::HashMap<String, serde_json::Value>,
#[serde(skip)]
pub extra_cbor: Vec<(String, Vec<u8>)>,
}
impl EmbedExternalView {
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("external")?;
self.external.encode_cbor(buf)?;
} else {
let mut pairs: Vec<(&str, Vec<u8>)> = Vec::new();
{
let mut vbuf = Vec::new();
self.external.encode_cbor(&mut vbuf)?;
pairs.push(("external", 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_external: Option<EmbedExternalViewExternal> = None;
let mut extra_cbor: Vec<(String, Vec<u8>)> = Vec::new();
for (key, value) in entries {
match key {
"external" => {
let raw = crate::cbor::encode_value(&value)?;
let mut dec = crate::cbor::Decoder::new(&raw);
field_external = Some(EmbedExternalViewExternal::decode_cbor(&mut dec)?);
}
_ => {
let raw = crate::cbor::encode_value(&value)?;
extra_cbor.push((key.to_string(), raw));
}
}
}
Ok(EmbedExternalView {
external: field_external.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'external'".into())
})?,
extra: std::collections::HashMap::new(),
extra_cbor,
})
}
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct EmbedExternalViewExternal {
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub associated_profiles: Vec<crate::api::app::bsky::ActorDefsProfileViewBasic>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub associated_refs: Vec<crate::api::com::atproto::RepoStrongRef>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub created_at: Option<crate::syntax::Datetime>,
pub description: String,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub labels: Vec<crate::api::com::atproto::LabelDefsLabel>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub reading_time: Option<i64>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub source: Option<EmbedExternalViewExternalSource>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub thumb: Option<String>,
pub title: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub updated_at: Option<crate::syntax::Datetime>,
pub uri: String,
#[serde(flatten)]
pub extra: std::collections::HashMap<String, serde_json::Value>,
#[serde(skip)]
pub extra_cbor: Vec<(String, Vec<u8>)>,
}
impl EmbedExternalViewExternal {
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 = 3u64;
if self.thumb.is_some() {
count += 1;
}
if !self.labels.is_empty() {
count += 1;
}
if self.source.is_some() {
count += 1;
}
if self.created_at.is_some() {
count += 1;
}
if self.updated_at.is_some() {
count += 1;
}
if self.reading_time.is_some() {
count += 1;
}
if !self.associated_refs.is_empty() {
count += 1;
}
if !self.associated_profiles.is_empty() {
count += 1;
}
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)?;
if self.thumb.is_some() {
crate::cbor::Encoder::new(&mut *buf).encode_text("thumb")?;
if let Some(ref val) = self.thumb {
crate::cbor::Encoder::new(&mut *buf).encode_text(val)?;
}
}
crate::cbor::Encoder::new(&mut *buf).encode_text("title")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(&self.title)?;
if !self.labels.is_empty() {
crate::cbor::Encoder::new(&mut *buf).encode_text("labels")?;
crate::cbor::Encoder::new(&mut *buf)
.encode_array_header(self.labels.len() as u64)?;
for item in &self.labels {
item.encode_cbor(buf)?;
}
}
if self.source.is_some() {
crate::cbor::Encoder::new(&mut *buf).encode_text("source")?;
if let Some(ref val) = self.source {
val.encode_cbor(buf)?;
}
}
if self.created_at.is_some() {
crate::cbor::Encoder::new(&mut *buf).encode_text("createdAt")?;
if let Some(ref val) = self.created_at {
crate::cbor::Encoder::new(&mut *buf).encode_text(val.as_str())?;
}
}
if self.updated_at.is_some() {
crate::cbor::Encoder::new(&mut *buf).encode_text("updatedAt")?;
if let Some(ref val) = self.updated_at {
crate::cbor::Encoder::new(&mut *buf).encode_text(val.as_str())?;
}
}
crate::cbor::Encoder::new(&mut *buf).encode_text("description")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(&self.description)?;
if self.reading_time.is_some() {
crate::cbor::Encoder::new(&mut *buf).encode_text("readingTime")?;
if let Some(ref val) = self.reading_time {
crate::cbor::Encoder::new(&mut *buf).encode_i64(*val)?;
}
}
if !self.associated_refs.is_empty() {
crate::cbor::Encoder::new(&mut *buf).encode_text("associatedRefs")?;
crate::cbor::Encoder::new(&mut *buf)
.encode_array_header(self.associated_refs.len() as u64)?;
for item in &self.associated_refs {
item.encode_cbor(buf)?;
}
}
if !self.associated_profiles.is_empty() {
crate::cbor::Encoder::new(&mut *buf).encode_text("associatedProfiles")?;
crate::cbor::Encoder::new(&mut *buf)
.encode_array_header(self.associated_profiles.len() as u64)?;
for item in &self.associated_profiles {
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.uri)?;
pairs.push(("uri", vbuf));
}
if self.thumb.is_some() {
let mut vbuf = Vec::new();
if let Some(ref val) = self.thumb {
crate::cbor::Encoder::new(&mut vbuf).encode_text(val)?;
}
pairs.push(("thumb", vbuf));
}
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(&self.title)?;
pairs.push(("title", vbuf));
}
if !self.labels.is_empty() {
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf)
.encode_array_header(self.labels.len() as u64)?;
for item in &self.labels {
item.encode_cbor(&mut vbuf)?;
}
pairs.push(("labels", vbuf));
}
if self.source.is_some() {
let mut vbuf = Vec::new();
if let Some(ref val) = self.source {
val.encode_cbor(&mut vbuf)?;
}
pairs.push(("source", vbuf));
}
if self.created_at.is_some() {
let mut vbuf = Vec::new();
if let Some(ref val) = self.created_at {
crate::cbor::Encoder::new(&mut vbuf).encode_text(val.as_str())?;
}
pairs.push(("createdAt", vbuf));
}
if self.updated_at.is_some() {
let mut vbuf = Vec::new();
if let Some(ref val) = self.updated_at {
crate::cbor::Encoder::new(&mut vbuf).encode_text(val.as_str())?;
}
pairs.push(("updatedAt", vbuf));
}
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(&self.description)?;
pairs.push(("description", vbuf));
}
if self.reading_time.is_some() {
let mut vbuf = Vec::new();
if let Some(ref val) = self.reading_time {
crate::cbor::Encoder::new(&mut vbuf).encode_i64(*val)?;
}
pairs.push(("readingTime", vbuf));
}
if !self.associated_refs.is_empty() {
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf)
.encode_array_header(self.associated_refs.len() as u64)?;
for item in &self.associated_refs {
item.encode_cbor(&mut vbuf)?;
}
pairs.push(("associatedRefs", vbuf));
}
if !self.associated_profiles.is_empty() {
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf)
.encode_array_header(self.associated_profiles.len() as u64)?;
for item in &self.associated_profiles {
item.encode_cbor(&mut vbuf)?;
}
pairs.push(("associatedProfiles", 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 field_thumb: Option<String> = None;
let mut field_title: Option<String> = None;
let mut field_labels: Vec<crate::api::com::atproto::LabelDefsLabel> = Vec::new();
let mut field_source: Option<EmbedExternalViewExternalSource> = None;
let mut field_created_at: Option<crate::syntax::Datetime> = None;
let mut field_updated_at: Option<crate::syntax::Datetime> = None;
let mut field_description: Option<String> = None;
let mut field_reading_time: Option<i64> = None;
let mut field_associated_refs: Vec<crate::api::com::atproto::RepoStrongRef> = Vec::new();
let mut field_associated_profiles: Vec<crate::api::app::bsky::ActorDefsProfileViewBasic> =
Vec::new();
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()));
}
}
"thumb" => {
if let crate::cbor::Value::Text(s) = value {
field_thumb = Some(s.to_string());
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
}
}
"title" => {
if let crate::cbor::Value::Text(s) = value {
field_title = Some(s.to_string());
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
}
}
"labels" => {
if let crate::cbor::Value::Array(items) = value {
for item in items {
let raw = crate::cbor::encode_value(&item)?;
let mut dec = crate::cbor::Decoder::new(&raw);
field_labels.push(
crate::api::com::atproto::LabelDefsLabel::decode_cbor(&mut dec)?,
);
}
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected array".into()));
}
}
"source" => {
let raw = crate::cbor::encode_value(&value)?;
let mut dec = crate::cbor::Decoder::new(&raw);
field_source = Some(EmbedExternalViewExternalSource::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()));
}
}
"description" => {
if let crate::cbor::Value::Text(s) = value {
field_description = Some(s.to_string());
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
}
}
"readingTime" => match value {
crate::cbor::Value::Unsigned(n) => {
field_reading_time = Some(n as i64);
}
crate::cbor::Value::Signed(n) => {
field_reading_time = Some(n);
}
_ => {
return Err(crate::cbor::CborError::InvalidCbor(
"expected integer".into(),
));
}
},
"associatedRefs" => {
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_associated_refs.push(
crate::api::com::atproto::RepoStrongRef::decode_cbor(&mut dec)?,
);
}
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected array".into()));
}
}
"associatedProfiles" => {
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_associated_profiles.push(
crate::api::app::bsky::ActorDefsProfileViewBasic::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(EmbedExternalViewExternal {
uri: field_uri.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'uri'".into())
})?,
thumb: field_thumb,
title: field_title.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'title'".into())
})?,
labels: field_labels,
source: field_source,
created_at: field_created_at,
updated_at: field_updated_at,
description: field_description.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'description'".into())
})?,
reading_time: field_reading_time,
associated_refs: field_associated_refs,
associated_profiles: field_associated_profiles,
extra: std::collections::HashMap::new(),
extra_cbor,
})
}
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct EmbedExternalViewExternalSource {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub icon: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub theme: Option<EmbedExternalViewExternalSourceTheme>,
pub title: String,
pub uri: String,
#[serde(flatten)]
pub extra: std::collections::HashMap<String, serde_json::Value>,
#[serde(skip)]
pub extra_cbor: Vec<(String, Vec<u8>)>,
}
impl EmbedExternalViewExternalSource {
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.icon.is_some() {
count += 1;
}
if self.theme.is_some() {
count += 1;
}
if self.description.is_some() {
count += 1;
}
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)?;
if self.icon.is_some() {
crate::cbor::Encoder::new(&mut *buf).encode_text("icon")?;
if let Some(ref val) = self.icon {
crate::cbor::Encoder::new(&mut *buf).encode_text(val)?;
}
}
if self.theme.is_some() {
crate::cbor::Encoder::new(&mut *buf).encode_text("theme")?;
if let Some(ref val) = self.theme {
val.encode_cbor(buf)?;
}
}
crate::cbor::Encoder::new(&mut *buf).encode_text("title")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(&self.title)?;
if self.description.is_some() {
crate::cbor::Encoder::new(&mut *buf).encode_text("description")?;
if let Some(ref val) = self.description {
crate::cbor::Encoder::new(&mut *buf).encode_text(val)?;
}
}
} else {
let mut pairs: Vec<(&str, Vec<u8>)> = Vec::new();
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(&self.uri)?;
pairs.push(("uri", vbuf));
}
if self.icon.is_some() {
let mut vbuf = Vec::new();
if let Some(ref val) = self.icon {
crate::cbor::Encoder::new(&mut vbuf).encode_text(val)?;
}
pairs.push(("icon", vbuf));
}
if self.theme.is_some() {
let mut vbuf = Vec::new();
if let Some(ref val) = self.theme {
val.encode_cbor(&mut vbuf)?;
}
pairs.push(("theme", vbuf));
}
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(&self.title)?;
pairs.push(("title", vbuf));
}
if self.description.is_some() {
let mut vbuf = Vec::new();
if let Some(ref val) = self.description {
crate::cbor::Encoder::new(&mut vbuf).encode_text(val)?;
}
pairs.push(("description", 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 field_icon: Option<String> = None;
let mut field_theme: Option<EmbedExternalViewExternalSourceTheme> = None;
let mut field_title: Option<String> = None;
let mut field_description: 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()));
}
}
"icon" => {
if let crate::cbor::Value::Text(s) = value {
field_icon = Some(s.to_string());
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
}
}
"theme" => {
let raw = crate::cbor::encode_value(&value)?;
let mut dec = crate::cbor::Decoder::new(&raw);
field_theme =
Some(EmbedExternalViewExternalSourceTheme::decode_cbor(&mut dec)?);
}
"title" => {
if let crate::cbor::Value::Text(s) = value {
field_title = Some(s.to_string());
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
}
}
"description" => {
if let crate::cbor::Value::Text(s) = value {
field_description = 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(EmbedExternalViewExternalSource {
uri: field_uri.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'uri'".into())
})?,
icon: field_icon,
theme: field_theme,
title: field_title.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'title'".into())
})?,
description: field_description,
extra: std::collections::HashMap::new(),
extra_cbor,
})
}
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct EmbedExternalViewExternalSourceTheme {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub accent_foreground_r_g_b: Option<EmbedExternalColorRGB>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub accent_r_g_b: Option<EmbedExternalColorRGB>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub background_r_g_b: Option<EmbedExternalColorRGB>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub foreground_r_g_b: Option<EmbedExternalColorRGB>,
#[serde(flatten)]
pub extra: std::collections::HashMap<String, serde_json::Value>,
#[serde(skip)]
pub extra_cbor: Vec<(String, Vec<u8>)>,
}
impl EmbedExternalViewExternalSourceTheme {
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 = 0u64;
if self.accent_r_g_b.is_some() {
count += 1;
}
if self.background_r_g_b.is_some() {
count += 1;
}
if self.foreground_r_g_b.is_some() {
count += 1;
}
if self.accent_foreground_r_g_b.is_some() {
count += 1;
}
crate::cbor::Encoder::new(&mut *buf).encode_map_header(count)?;
if self.accent_r_g_b.is_some() {
crate::cbor::Encoder::new(&mut *buf).encode_text("accentRGB")?;
if let Some(ref val) = self.accent_r_g_b {
val.encode_cbor(buf)?;
}
}
if self.background_r_g_b.is_some() {
crate::cbor::Encoder::new(&mut *buf).encode_text("backgroundRGB")?;
if let Some(ref val) = self.background_r_g_b {
val.encode_cbor(buf)?;
}
}
if self.foreground_r_g_b.is_some() {
crate::cbor::Encoder::new(&mut *buf).encode_text("foregroundRGB")?;
if let Some(ref val) = self.foreground_r_g_b {
val.encode_cbor(buf)?;
}
}
if self.accent_foreground_r_g_b.is_some() {
crate::cbor::Encoder::new(&mut *buf).encode_text("accentForegroundRGB")?;
if let Some(ref val) = self.accent_foreground_r_g_b {
val.encode_cbor(buf)?;
}
}
} else {
let mut pairs: Vec<(&str, Vec<u8>)> = Vec::new();
if self.accent_r_g_b.is_some() {
let mut vbuf = Vec::new();
if let Some(ref val) = self.accent_r_g_b {
val.encode_cbor(&mut vbuf)?;
}
pairs.push(("accentRGB", vbuf));
}
if self.background_r_g_b.is_some() {
let mut vbuf = Vec::new();
if let Some(ref val) = self.background_r_g_b {
val.encode_cbor(&mut vbuf)?;
}
pairs.push(("backgroundRGB", vbuf));
}
if self.foreground_r_g_b.is_some() {
let mut vbuf = Vec::new();
if let Some(ref val) = self.foreground_r_g_b {
val.encode_cbor(&mut vbuf)?;
}
pairs.push(("foregroundRGB", vbuf));
}
if self.accent_foreground_r_g_b.is_some() {
let mut vbuf = Vec::new();
if let Some(ref val) = self.accent_foreground_r_g_b {
val.encode_cbor(&mut vbuf)?;
}
pairs.push(("accentForegroundRGB", 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_accent_r_g_b: Option<EmbedExternalColorRGB> = None;
let mut field_background_r_g_b: Option<EmbedExternalColorRGB> = None;
let mut field_foreground_r_g_b: Option<EmbedExternalColorRGB> = None;
let mut field_accent_foreground_r_g_b: Option<EmbedExternalColorRGB> = None;
let mut extra_cbor: Vec<(String, Vec<u8>)> = Vec::new();
for (key, value) in entries {
match key {
"accentRGB" => {
let raw = crate::cbor::encode_value(&value)?;
let mut dec = crate::cbor::Decoder::new(&raw);
field_accent_r_g_b = Some(EmbedExternalColorRGB::decode_cbor(&mut dec)?);
}
"backgroundRGB" => {
let raw = crate::cbor::encode_value(&value)?;
let mut dec = crate::cbor::Decoder::new(&raw);
field_background_r_g_b = Some(EmbedExternalColorRGB::decode_cbor(&mut dec)?);
}
"foregroundRGB" => {
let raw = crate::cbor::encode_value(&value)?;
let mut dec = crate::cbor::Decoder::new(&raw);
field_foreground_r_g_b = Some(EmbedExternalColorRGB::decode_cbor(&mut dec)?);
}
"accentForegroundRGB" => {
let raw = crate::cbor::encode_value(&value)?;
let mut dec = crate::cbor::Decoder::new(&raw);
field_accent_foreground_r_g_b =
Some(EmbedExternalColorRGB::decode_cbor(&mut dec)?);
}
_ => {
let raw = crate::cbor::encode_value(&value)?;
extra_cbor.push((key.to_string(), raw));
}
}
}
Ok(EmbedExternalViewExternalSourceTheme {
accent_r_g_b: field_accent_r_g_b,
background_r_g_b: field_background_r_g_b,
foreground_r_g_b: field_foreground_r_g_b,
accent_foreground_r_g_b: field_accent_foreground_r_g_b,
extra: std::collections::HashMap::new(),
extra_cbor,
})
}
}