#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct RepoApplyWritesCreate {
pub collection: crate::syntax::Nsid,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub rkey: Option<crate::syntax::RecordKey>,
pub value: serde_json::Value,
#[serde(flatten)]
pub extra: std::collections::HashMap<String, serde_json::Value>,
#[serde(skip)]
pub extra_cbor: Vec<(String, Vec<u8>)>,
}
impl RepoApplyWritesCreate {
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.rkey.is_some() {
count += 1;
}
crate::cbor::Encoder::new(&mut *buf).encode_map_header(count)?;
if self.rkey.is_some() {
crate::cbor::Encoder::new(&mut *buf).encode_text("rkey")?;
if let Some(ref val) = self.rkey {
crate::cbor::Encoder::new(&mut *buf).encode_text(val.as_str())?;
}
}
crate::cbor::Encoder::new(&mut *buf).encode_text("collection")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(self.collection.as_str())?;
} else {
let mut pairs: Vec<(&str, Vec<u8>)> = Vec::new();
if self.rkey.is_some() {
let mut vbuf = Vec::new();
if let Some(ref val) = self.rkey {
crate::cbor::Encoder::new(&mut vbuf).encode_text(val.as_str())?;
}
pairs.push(("rkey", vbuf));
}
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(self.collection.as_str())?;
pairs.push(("collection", 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_rkey: Option<crate::syntax::RecordKey> = None;
let mut field_collection: Option<crate::syntax::Nsid> = None;
let mut extra_cbor: Vec<(String, Vec<u8>)> = Vec::new();
for (key, value) in entries {
match key {
"rkey" => {
if let crate::cbor::Value::Text(s) = value {
field_rkey = Some(
crate::syntax::RecordKey::try_from(s)
.map_err(|e| crate::cbor::CborError::InvalidCbor(e.to_string()))?,
);
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
}
}
"collection" => {
if let crate::cbor::Value::Text(s) = value {
field_collection = Some(
crate::syntax::Nsid::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(RepoApplyWritesCreate {
rkey: field_rkey,
value: Default::default(),
collection: field_collection.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'collection'".into())
})?,
extra: std::collections::HashMap::new(),
extra_cbor,
})
}
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct RepoApplyWritesCreateResult {
pub cid: String,
pub uri: crate::syntax::AtUri,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub validation_status: Option<String>,
#[serde(flatten)]
pub extra: std::collections::HashMap<String, serde_json::Value>,
#[serde(skip)]
pub extra_cbor: Vec<(String, Vec<u8>)>,
}
impl RepoApplyWritesCreateResult {
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.validation_status.is_some() {
count += 1;
}
crate::cbor::Encoder::new(&mut *buf).encode_map_header(count)?;
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("uri")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(self.uri.as_str())?;
if self.validation_status.is_some() {
crate::cbor::Encoder::new(&mut *buf).encode_text("validationStatus")?;
if let Some(ref val) = self.validation_status {
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.cid)?;
pairs.push(("cid", vbuf));
}
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(self.uri.as_str())?;
pairs.push(("uri", vbuf));
}
if self.validation_status.is_some() {
let mut vbuf = Vec::new();
if let Some(ref val) = self.validation_status {
crate::cbor::Encoder::new(&mut vbuf).encode_text(val)?;
}
pairs.push(("validationStatus", 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_cid: Option<String> = None;
let mut field_uri: Option<crate::syntax::AtUri> = None;
let mut field_validation_status: Option<String> = None;
let mut extra_cbor: Vec<(String, Vec<u8>)> = Vec::new();
for (key, value) in entries {
match key {
"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()));
}
}
"uri" => {
if let crate::cbor::Value::Text(s) = value {
field_uri = Some(
crate::syntax::AtUri::try_from(s)
.map_err(|e| crate::cbor::CborError::InvalidCbor(e.to_string()))?,
);
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
}
}
"validationStatus" => {
if let crate::cbor::Value::Text(s) = value {
field_validation_status = 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(RepoApplyWritesCreateResult {
cid: field_cid.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'cid'".into())
})?,
uri: field_uri.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'uri'".into())
})?,
validation_status: field_validation_status,
extra: std::collections::HashMap::new(),
extra_cbor,
})
}
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct RepoApplyWritesDelete {
pub collection: crate::syntax::Nsid,
pub rkey: crate::syntax::RecordKey,
#[serde(flatten)]
pub extra: std::collections::HashMap<String, serde_json::Value>,
#[serde(skip)]
pub extra_cbor: Vec<(String, Vec<u8>)>,
}
impl RepoApplyWritesDelete {
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("rkey")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(self.rkey.as_str())?;
crate::cbor::Encoder::new(&mut *buf).encode_text("collection")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(self.collection.as_str())?;
} else {
let mut pairs: Vec<(&str, Vec<u8>)> = Vec::new();
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(self.rkey.as_str())?;
pairs.push(("rkey", vbuf));
}
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(self.collection.as_str())?;
pairs.push(("collection", 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_rkey: Option<crate::syntax::RecordKey> = None;
let mut field_collection: Option<crate::syntax::Nsid> = None;
let mut extra_cbor: Vec<(String, Vec<u8>)> = Vec::new();
for (key, value) in entries {
match key {
"rkey" => {
if let crate::cbor::Value::Text(s) = value {
field_rkey = Some(
crate::syntax::RecordKey::try_from(s)
.map_err(|e| crate::cbor::CborError::InvalidCbor(e.to_string()))?,
);
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
}
}
"collection" => {
if let crate::cbor::Value::Text(s) = value {
field_collection = Some(
crate::syntax::Nsid::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(RepoApplyWritesDelete {
rkey: field_rkey.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'rkey'".into())
})?,
collection: field_collection.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'collection'".into())
})?,
extra: std::collections::HashMap::new(),
extra_cbor,
})
}
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct RepoApplyWritesDeleteResult {
#[serde(flatten)]
pub extra: std::collections::HashMap<String, serde_json::Value>,
#[serde(skip)]
pub extra_cbor: Vec<(String, Vec<u8>)>,
}
impl RepoApplyWritesDeleteResult {
pub fn to_cbor(&self) -> Result<Vec<u8>, crate::cbor::CborError> {
let mut buf = Vec::new();
self.encode_cbor(&mut buf)?;
Ok(buf)
}
pub fn encode_cbor(&self, buf: &mut Vec<u8>) -> Result<(), crate::cbor::CborError> {
if self.extra_cbor.is_empty() {
let count = 0u64;
crate::cbor::Encoder::new(&mut *buf).encode_map_header(count)?;
} else {
let mut pairs: Vec<(&str, Vec<u8>)> = Vec::new();
for (k, v) in &self.extra_cbor {
pairs.push((k.as_str(), v.clone()));
}
pairs.sort_by(|a, b| crate::cbor::cbor_key_cmp(a.0, b.0));
crate::cbor::Encoder::new(&mut *buf).encode_map_header(pairs.len() as u64)?;
for (k, v) in &pairs {
crate::cbor::Encoder::new(&mut *buf).encode_text(k)?;
buf.extend_from_slice(v);
}
}
Ok(())
}
pub fn from_cbor(data: &[u8]) -> Result<Self, crate::cbor::CborError> {
let mut decoder = crate::cbor::Decoder::new(data);
let result = Self::decode_cbor(&mut decoder)?;
if !decoder.is_empty() {
return Err(crate::cbor::CborError::InvalidCbor("trailing data".into()));
}
Ok(result)
}
pub fn decode_cbor(decoder: &mut crate::cbor::Decoder) -> Result<Self, crate::cbor::CborError> {
let val = decoder.decode()?;
let entries = match val {
crate::cbor::Value::Map(entries) => entries,
_ => return Err(crate::cbor::CborError::InvalidCbor("expected map".into())),
};
let mut extra_cbor: Vec<(String, Vec<u8>)> = Vec::new();
for (key, value) in entries {
match key {
_ => {
let raw = crate::cbor::encode_value(&value)?;
extra_cbor.push((key.to_string(), raw));
}
}
}
Ok(RepoApplyWritesDeleteResult {
extra: std::collections::HashMap::new(),
extra_cbor,
})
}
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct RepoApplyWritesInput {
pub repo: crate::syntax::AtIdentifier,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub swap_commit: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub validate: Option<bool>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub writes: Vec<RepoApplyWritesInputWritesUnion>,
#[serde(flatten)]
pub extra: std::collections::HashMap<String, serde_json::Value>,
}
#[derive(Debug, Clone)]
pub enum RepoApplyWritesInputWritesUnion {
RepoApplyWritesCreate(Box<RepoApplyWritesCreate>),
RepoApplyWritesUpdate(Box<RepoApplyWritesUpdate>),
RepoApplyWritesDelete(Box<RepoApplyWritesDelete>),
}
impl serde::Serialize for RepoApplyWritesInputWritesUnion {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
match self {
RepoApplyWritesInputWritesUnion::RepoApplyWritesCreate(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.repo.applyWrites#create".to_string(),
),
);
}
map.serialize(serializer)
}
RepoApplyWritesInputWritesUnion::RepoApplyWritesUpdate(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.repo.applyWrites#update".to_string(),
),
);
}
map.serialize(serializer)
}
RepoApplyWritesInputWritesUnion::RepoApplyWritesDelete(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.repo.applyWrites#delete".to_string(),
),
);
}
map.serialize(serializer)
}
}
}
}
impl<'de> serde::Deserialize<'de> for RepoApplyWritesInputWritesUnion {
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.repo.applyWrites#create" => {
let inner: RepoApplyWritesCreate =
serde_json::from_value(value).map_err(serde::de::Error::custom)?;
Ok(RepoApplyWritesInputWritesUnion::RepoApplyWritesCreate(
Box::new(inner),
))
}
"com.atproto.repo.applyWrites#update" => {
let inner: RepoApplyWritesUpdate =
serde_json::from_value(value).map_err(serde::de::Error::custom)?;
Ok(RepoApplyWritesInputWritesUnion::RepoApplyWritesUpdate(
Box::new(inner),
))
}
"com.atproto.repo.applyWrites#delete" => {
let inner: RepoApplyWritesDelete =
serde_json::from_value(value).map_err(serde::de::Error::custom)?;
Ok(RepoApplyWritesInputWritesUnion::RepoApplyWritesDelete(
Box::new(inner),
))
}
other => Err(serde::de::Error::custom(format!(
"unknown type {:?} in closed union RepoApplyWritesInputWritesUnion",
other
))),
}
}
}
impl RepoApplyWritesInputWritesUnion {
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 {
RepoApplyWritesInputWritesUnion::RepoApplyWritesCreate(inner) => inner.encode_cbor(buf),
RepoApplyWritesInputWritesUnion::RepoApplyWritesUpdate(inner) => inner.encode_cbor(buf),
RepoApplyWritesInputWritesUnion::RepoApplyWritesDelete(inner) => inner.encode_cbor(buf),
}
}
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.repo.applyWrites#create" => {
let mut dec = crate::cbor::Decoder::new(raw);
let inner = RepoApplyWritesCreate::decode_cbor(&mut dec)?;
Ok(RepoApplyWritesInputWritesUnion::RepoApplyWritesCreate(
Box::new(inner),
))
}
"com.atproto.repo.applyWrites#update" => {
let mut dec = crate::cbor::Decoder::new(raw);
let inner = RepoApplyWritesUpdate::decode_cbor(&mut dec)?;
Ok(RepoApplyWritesInputWritesUnion::RepoApplyWritesUpdate(
Box::new(inner),
))
}
"com.atproto.repo.applyWrites#delete" => {
let mut dec = crate::cbor::Decoder::new(raw);
let inner = RepoApplyWritesDelete::decode_cbor(&mut dec)?;
Ok(RepoApplyWritesInputWritesUnion::RepoApplyWritesDelete(
Box::new(inner),
))
}
other => Err(crate::cbor::CborError::InvalidCbor(format!(
"unknown type {:?} in closed union RepoApplyWritesInputWritesUnion",
other
))),
}
}
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct RepoApplyWritesOutput {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub commit: Option<crate::api::com::atproto::RepoDefsCommitMeta>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub results: Vec<RepoApplyWritesOutputResultsUnion>,
#[serde(flatten)]
pub extra: std::collections::HashMap<String, serde_json::Value>,
}
#[derive(Debug, Clone)]
pub enum RepoApplyWritesOutputResultsUnion {
RepoApplyWritesCreateResult(Box<RepoApplyWritesCreateResult>),
RepoApplyWritesUpdateResult(Box<RepoApplyWritesUpdateResult>),
RepoApplyWritesDeleteResult(Box<RepoApplyWritesDeleteResult>),
}
impl serde::Serialize for RepoApplyWritesOutputResultsUnion {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
match self {
RepoApplyWritesOutputResultsUnion::RepoApplyWritesCreateResult(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.repo.applyWrites#createResult".to_string(),
),
);
}
map.serialize(serializer)
}
RepoApplyWritesOutputResultsUnion::RepoApplyWritesUpdateResult(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.repo.applyWrites#updateResult".to_string(),
),
);
}
map.serialize(serializer)
}
RepoApplyWritesOutputResultsUnion::RepoApplyWritesDeleteResult(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.repo.applyWrites#deleteResult".to_string(),
),
);
}
map.serialize(serializer)
}
}
}
}
impl<'de> serde::Deserialize<'de> for RepoApplyWritesOutputResultsUnion {
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.repo.applyWrites#createResult" => {
let inner: RepoApplyWritesCreateResult =
serde_json::from_value(value).map_err(serde::de::Error::custom)?;
Ok(RepoApplyWritesOutputResultsUnion::RepoApplyWritesCreateResult(Box::new(inner)))
}
"com.atproto.repo.applyWrites#updateResult" => {
let inner: RepoApplyWritesUpdateResult =
serde_json::from_value(value).map_err(serde::de::Error::custom)?;
Ok(RepoApplyWritesOutputResultsUnion::RepoApplyWritesUpdateResult(Box::new(inner)))
}
"com.atproto.repo.applyWrites#deleteResult" => {
let inner: RepoApplyWritesDeleteResult =
serde_json::from_value(value).map_err(serde::de::Error::custom)?;
Ok(RepoApplyWritesOutputResultsUnion::RepoApplyWritesDeleteResult(Box::new(inner)))
}
other => Err(serde::de::Error::custom(format!(
"unknown type {:?} in closed union RepoApplyWritesOutputResultsUnion",
other
))),
}
}
}
impl RepoApplyWritesOutputResultsUnion {
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 {
RepoApplyWritesOutputResultsUnion::RepoApplyWritesCreateResult(inner) => {
inner.encode_cbor(buf)
}
RepoApplyWritesOutputResultsUnion::RepoApplyWritesUpdateResult(inner) => {
inner.encode_cbor(buf)
}
RepoApplyWritesOutputResultsUnion::RepoApplyWritesDeleteResult(inner) => {
inner.encode_cbor(buf)
}
}
}
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.repo.applyWrites#createResult" => {
let mut dec = crate::cbor::Decoder::new(raw);
let inner = RepoApplyWritesCreateResult::decode_cbor(&mut dec)?;
Ok(RepoApplyWritesOutputResultsUnion::RepoApplyWritesCreateResult(Box::new(inner)))
}
"com.atproto.repo.applyWrites#updateResult" => {
let mut dec = crate::cbor::Decoder::new(raw);
let inner = RepoApplyWritesUpdateResult::decode_cbor(&mut dec)?;
Ok(RepoApplyWritesOutputResultsUnion::RepoApplyWritesUpdateResult(Box::new(inner)))
}
"com.atproto.repo.applyWrites#deleteResult" => {
let mut dec = crate::cbor::Decoder::new(raw);
let inner = RepoApplyWritesDeleteResult::decode_cbor(&mut dec)?;
Ok(RepoApplyWritesOutputResultsUnion::RepoApplyWritesDeleteResult(Box::new(inner)))
}
other => Err(crate::cbor::CborError::InvalidCbor(format!(
"unknown type {:?} in closed union RepoApplyWritesOutputResultsUnion",
other
))),
}
}
}
pub async fn repo_apply_writes(
client: &crate::xrpc::Client,
input: &RepoApplyWritesInput,
) -> Result<RepoApplyWritesOutput, crate::xrpc::Error> {
client
.procedure("com.atproto.repo.applyWrites", input)
.await
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct RepoApplyWritesUpdate {
pub collection: crate::syntax::Nsid,
pub rkey: crate::syntax::RecordKey,
pub value: serde_json::Value,
#[serde(flatten)]
pub extra: std::collections::HashMap<String, serde_json::Value>,
#[serde(skip)]
pub extra_cbor: Vec<(String, Vec<u8>)>,
}
impl RepoApplyWritesUpdate {
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("rkey")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(self.rkey.as_str())?;
crate::cbor::Encoder::new(&mut *buf).encode_text("collection")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(self.collection.as_str())?;
} else {
let mut pairs: Vec<(&str, Vec<u8>)> = Vec::new();
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(self.rkey.as_str())?;
pairs.push(("rkey", vbuf));
}
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(self.collection.as_str())?;
pairs.push(("collection", 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_rkey: Option<crate::syntax::RecordKey> = None;
let mut field_collection: Option<crate::syntax::Nsid> = None;
let mut extra_cbor: Vec<(String, Vec<u8>)> = Vec::new();
for (key, value) in entries {
match key {
"rkey" => {
if let crate::cbor::Value::Text(s) = value {
field_rkey = Some(
crate::syntax::RecordKey::try_from(s)
.map_err(|e| crate::cbor::CborError::InvalidCbor(e.to_string()))?,
);
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
}
}
"collection" => {
if let crate::cbor::Value::Text(s) = value {
field_collection = Some(
crate::syntax::Nsid::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(RepoApplyWritesUpdate {
rkey: field_rkey.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'rkey'".into())
})?,
value: Default::default(),
collection: field_collection.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'collection'".into())
})?,
extra: std::collections::HashMap::new(),
extra_cbor,
})
}
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct RepoApplyWritesUpdateResult {
pub cid: String,
pub uri: crate::syntax::AtUri,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub validation_status: Option<String>,
#[serde(flatten)]
pub extra: std::collections::HashMap<String, serde_json::Value>,
#[serde(skip)]
pub extra_cbor: Vec<(String, Vec<u8>)>,
}
impl RepoApplyWritesUpdateResult {
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.validation_status.is_some() {
count += 1;
}
crate::cbor::Encoder::new(&mut *buf).encode_map_header(count)?;
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("uri")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(self.uri.as_str())?;
if self.validation_status.is_some() {
crate::cbor::Encoder::new(&mut *buf).encode_text("validationStatus")?;
if let Some(ref val) = self.validation_status {
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.cid)?;
pairs.push(("cid", vbuf));
}
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(self.uri.as_str())?;
pairs.push(("uri", vbuf));
}
if self.validation_status.is_some() {
let mut vbuf = Vec::new();
if let Some(ref val) = self.validation_status {
crate::cbor::Encoder::new(&mut vbuf).encode_text(val)?;
}
pairs.push(("validationStatus", 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_cid: Option<String> = None;
let mut field_uri: Option<crate::syntax::AtUri> = None;
let mut field_validation_status: Option<String> = None;
let mut extra_cbor: Vec<(String, Vec<u8>)> = Vec::new();
for (key, value) in entries {
match key {
"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()));
}
}
"uri" => {
if let crate::cbor::Value::Text(s) = value {
field_uri = Some(
crate::syntax::AtUri::try_from(s)
.map_err(|e| crate::cbor::CborError::InvalidCbor(e.to_string()))?,
);
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
}
}
"validationStatus" => {
if let crate::cbor::Value::Text(s) = value {
field_validation_status = 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(RepoApplyWritesUpdateResult {
cid: field_cid.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'cid'".into())
})?,
uri: field_uri.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'uri'".into())
})?,
validation_status: field_validation_status,
extra: std::collections::HashMap::new(),
extra_cbor,
})
}
}