#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct EmbedVideoCaption {
pub file: crate::api::Blob,
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 EmbedVideoCaption {
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("file")?;
self.file.encode_cbor(buf)?;
crate::cbor::Encoder::new(&mut *buf).encode_text("lang")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(self.lang.as_str())?;
} else {
let mut pairs: Vec<(&str, Vec<u8>)> = Vec::new();
{
let mut vbuf = Vec::new();
self.file.encode_cbor(&mut vbuf)?;
pairs.push(("file", vbuf));
}
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(self.lang.as_str())?;
pairs.push(("lang", 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_file: Option<crate::api::Blob> = None;
let mut field_lang: Option<crate::syntax::Language> = None;
let mut extra_cbor: Vec<(String, Vec<u8>)> = Vec::new();
for (key, value) in entries {
match key {
"file" => {
let raw = crate::cbor::encode_value(&value)?;
let mut dec = crate::cbor::Decoder::new(&raw);
field_file = Some(crate::api::Blob::decode_cbor(&mut dec)?);
}
"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()));
}
}
_ => {
let raw = crate::cbor::encode_value(&value)?;
extra_cbor.push((key.to_string(), raw));
}
}
}
Ok(EmbedVideoCaption {
file: field_file.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'file'".into())
})?,
lang: field_lang.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'lang'".into())
})?,
extra: std::collections::HashMap::new(),
extra_cbor,
})
}
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct EmbedVideo {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub alt: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub aspect_ratio: Option<crate::api::app::bsky::EmbedDefsAspectRatio>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub captions: Vec<EmbedVideoCaption>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub presentation: Option<String>,
pub video: crate::api::Blob,
#[serde(flatten)]
pub extra: std::collections::HashMap<String, serde_json::Value>,
#[serde(skip)]
pub extra_cbor: Vec<(String, Vec<u8>)>,
}
impl EmbedVideo {
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;
}
if self.aspect_ratio.is_some() {
count += 1;
}
if self.presentation.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("video")?;
self.video.encode_cbor(buf)?;
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)?;
}
}
if self.aspect_ratio.is_some() {
crate::cbor::Encoder::new(&mut *buf).encode_text("aspectRatio")?;
if let Some(ref val) = self.aspect_ratio {
val.encode_cbor(buf)?;
}
}
if self.presentation.is_some() {
crate::cbor::Encoder::new(&mut *buf).encode_text("presentation")?;
if let Some(ref val) = self.presentation {
crate::cbor::Encoder::new(&mut *buf).encode_text(val)?;
}
}
} 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.video.encode_cbor(&mut vbuf)?;
pairs.push(("video", 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));
}
if self.aspect_ratio.is_some() {
let mut vbuf = Vec::new();
if let Some(ref val) = self.aspect_ratio {
val.encode_cbor(&mut vbuf)?;
}
pairs.push(("aspectRatio", vbuf));
}
if self.presentation.is_some() {
let mut vbuf = Vec::new();
if let Some(ref val) = self.presentation {
crate::cbor::Encoder::new(&mut vbuf).encode_text(val)?;
}
pairs.push(("presentation", 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_video: Option<crate::api::Blob> = None;
let mut field_captions: Vec<EmbedVideoCaption> = Vec::new();
let mut field_aspect_ratio: Option<crate::api::app::bsky::EmbedDefsAspectRatio> = None;
let mut field_presentation: Option<String> = 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()));
}
}
"video" => {
let raw = crate::cbor::encode_value(&value)?;
let mut dec = crate::cbor::Decoder::new(&raw);
field_video = Some(crate::api::Blob::decode_cbor(&mut dec)?);
}
"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(EmbedVideoCaption::decode_cbor(&mut dec)?);
}
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected array".into()));
}
}
"aspectRatio" => {
let raw = crate::cbor::encode_value(&value)?;
let mut dec = crate::cbor::Decoder::new(&raw);
field_aspect_ratio = Some(
crate::api::app::bsky::EmbedDefsAspectRatio::decode_cbor(&mut dec)?,
);
}
"presentation" => {
if let crate::cbor::Value::Text(s) = value {
field_presentation = 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(EmbedVideo {
alt: field_alt,
video: field_video.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'video'".into())
})?,
captions: field_captions,
aspect_ratio: field_aspect_ratio,
presentation: field_presentation,
extra: std::collections::HashMap::new(),
extra_cbor,
})
}
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct EmbedVideoView {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub alt: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub aspect_ratio: Option<crate::api::app::bsky::EmbedDefsAspectRatio>,
pub cid: String,
pub playlist: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub presentation: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub thumbnail: Option<String>,
#[serde(flatten)]
pub extra: std::collections::HashMap<String, serde_json::Value>,
#[serde(skip)]
pub extra_cbor: Vec<(String, Vec<u8>)>,
}
impl EmbedVideoView {
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.alt.is_some() {
count += 1;
}
if self.thumbnail.is_some() {
count += 1;
}
if self.aspect_ratio.is_some() {
count += 1;
}
if self.presentation.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("cid")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(&self.cid)?;
crate::cbor::Encoder::new(&mut *buf).encode_text("playlist")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(&self.playlist)?;
if self.thumbnail.is_some() {
crate::cbor::Encoder::new(&mut *buf).encode_text("thumbnail")?;
if let Some(ref val) = self.thumbnail {
crate::cbor::Encoder::new(&mut *buf).encode_text(val)?;
}
}
if self.aspect_ratio.is_some() {
crate::cbor::Encoder::new(&mut *buf).encode_text("aspectRatio")?;
if let Some(ref val) = self.aspect_ratio {
val.encode_cbor(buf)?;
}
}
if self.presentation.is_some() {
crate::cbor::Encoder::new(&mut *buf).encode_text("presentation")?;
if let Some(ref val) = self.presentation {
crate::cbor::Encoder::new(&mut *buf).encode_text(val)?;
}
}
} 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();
crate::cbor::Encoder::new(&mut vbuf).encode_text(&self.cid)?;
pairs.push(("cid", vbuf));
}
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(&self.playlist)?;
pairs.push(("playlist", vbuf));
}
if self.thumbnail.is_some() {
let mut vbuf = Vec::new();
if let Some(ref val) = self.thumbnail {
crate::cbor::Encoder::new(&mut vbuf).encode_text(val)?;
}
pairs.push(("thumbnail", vbuf));
}
if self.aspect_ratio.is_some() {
let mut vbuf = Vec::new();
if let Some(ref val) = self.aspect_ratio {
val.encode_cbor(&mut vbuf)?;
}
pairs.push(("aspectRatio", vbuf));
}
if self.presentation.is_some() {
let mut vbuf = Vec::new();
if let Some(ref val) = self.presentation {
crate::cbor::Encoder::new(&mut vbuf).encode_text(val)?;
}
pairs.push(("presentation", 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_cid: Option<String> = None;
let mut field_playlist: Option<String> = None;
let mut field_thumbnail: Option<String> = None;
let mut field_aspect_ratio: Option<crate::api::app::bsky::EmbedDefsAspectRatio> = None;
let mut field_presentation: Option<String> = 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()));
}
}
"cid" => {
if let crate::cbor::Value::Text(s) = value {
field_cid = Some(s.to_string());
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
}
}
"playlist" => {
if let crate::cbor::Value::Text(s) = value {
field_playlist = Some(s.to_string());
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
}
}
"thumbnail" => {
if let crate::cbor::Value::Text(s) = value {
field_thumbnail = Some(s.to_string());
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
}
}
"aspectRatio" => {
let raw = crate::cbor::encode_value(&value)?;
let mut dec = crate::cbor::Decoder::new(&raw);
field_aspect_ratio = Some(
crate::api::app::bsky::EmbedDefsAspectRatio::decode_cbor(&mut dec)?,
);
}
"presentation" => {
if let crate::cbor::Value::Text(s) = value {
field_presentation = 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(EmbedVideoView {
alt: field_alt,
cid: field_cid.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'cid'".into())
})?,
playlist: field_playlist.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'playlist'".into())
})?,
thumbnail: field_thumbnail,
aspect_ratio: field_aspect_ratio,
presentation: field_presentation,
extra: std::collections::HashMap::new(),
extra_cbor,
})
}
}