#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AdminDefsAccountView {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub deactivated_at: Option<crate::syntax::Datetime>,
pub did: crate::syntax::Did,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub email: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub email_confirmed_at: Option<crate::syntax::Datetime>,
pub handle: crate::syntax::Handle,
pub indexed_at: crate::syntax::Datetime,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub invite_note: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub invited_by: Option<crate::api::com::atproto::ServerDefsInviteCode>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub invites: Vec<crate::api::com::atproto::ServerDefsInviteCode>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub invites_disabled: Option<bool>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub related_records: Vec<serde_json::Value>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub threat_signatures: Vec<AdminDefsThreatSignature>,
#[serde(flatten)]
pub extra: std::collections::HashMap<String, serde_json::Value>,
#[serde(skip)]
pub extra_cbor: Vec<(String, Vec<u8>)>,
}
impl AdminDefsAccountView {
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.email.is_some() {
count += 1;
}
if !self.invites.is_empty() {
count += 1;
}
if self.invited_by.is_some() {
count += 1;
}
if self.invite_note.is_some() {
count += 1;
}
if self.deactivated_at.is_some() {
count += 1;
}
if self.invites_disabled.is_some() {
count += 1;
}
if self.email_confirmed_at.is_some() {
count += 1;
}
if !self.threat_signatures.is_empty() {
count += 1;
}
crate::cbor::Encoder::new(&mut *buf).encode_map_header(count)?;
crate::cbor::Encoder::new(&mut *buf).encode_text("did")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(self.did.as_str())?;
if self.email.is_some() {
crate::cbor::Encoder::new(&mut *buf).encode_text("email")?;
if let Some(ref val) = self.email {
crate::cbor::Encoder::new(&mut *buf).encode_text(val)?;
}
}
crate::cbor::Encoder::new(&mut *buf).encode_text("handle")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(self.handle.as_str())?;
if !self.invites.is_empty() {
crate::cbor::Encoder::new(&mut *buf).encode_text("invites")?;
crate::cbor::Encoder::new(&mut *buf)
.encode_array_header(self.invites.len() as u64)?;
for item in &self.invites {
item.encode_cbor(buf)?;
}
}
crate::cbor::Encoder::new(&mut *buf).encode_text("indexedAt")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(self.indexed_at.as_str())?;
if self.invited_by.is_some() {
crate::cbor::Encoder::new(&mut *buf).encode_text("invitedBy")?;
if let Some(ref val) = self.invited_by {
val.encode_cbor(buf)?;
}
}
if self.invite_note.is_some() {
crate::cbor::Encoder::new(&mut *buf).encode_text("inviteNote")?;
if let Some(ref val) = self.invite_note {
crate::cbor::Encoder::new(&mut *buf).encode_text(val)?;
}
}
if self.deactivated_at.is_some() {
crate::cbor::Encoder::new(&mut *buf).encode_text("deactivatedAt")?;
if let Some(ref val) = self.deactivated_at {
crate::cbor::Encoder::new(&mut *buf).encode_text(val.as_str())?;
}
}
if self.invites_disabled.is_some() {
crate::cbor::Encoder::new(&mut *buf).encode_text("invitesDisabled")?;
if let Some(ref val) = self.invites_disabled {
crate::cbor::Encoder::new(&mut *buf).encode_bool(*val)?;
}
}
if self.email_confirmed_at.is_some() {
crate::cbor::Encoder::new(&mut *buf).encode_text("emailConfirmedAt")?;
if let Some(ref val) = self.email_confirmed_at {
crate::cbor::Encoder::new(&mut *buf).encode_text(val.as_str())?;
}
}
if !self.threat_signatures.is_empty() {
crate::cbor::Encoder::new(&mut *buf).encode_text("threatSignatures")?;
crate::cbor::Encoder::new(&mut *buf)
.encode_array_header(self.threat_signatures.len() as u64)?;
for item in &self.threat_signatures {
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.did.as_str())?;
pairs.push(("did", vbuf));
}
if self.email.is_some() {
let mut vbuf = Vec::new();
if let Some(ref val) = self.email {
crate::cbor::Encoder::new(&mut vbuf).encode_text(val)?;
}
pairs.push(("email", vbuf));
}
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(self.handle.as_str())?;
pairs.push(("handle", vbuf));
}
if !self.invites.is_empty() {
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf)
.encode_array_header(self.invites.len() as u64)?;
for item in &self.invites {
item.encode_cbor(&mut vbuf)?;
}
pairs.push(("invites", vbuf));
}
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(self.indexed_at.as_str())?;
pairs.push(("indexedAt", vbuf));
}
if self.invited_by.is_some() {
let mut vbuf = Vec::new();
if let Some(ref val) = self.invited_by {
val.encode_cbor(&mut vbuf)?;
}
pairs.push(("invitedBy", vbuf));
}
if self.invite_note.is_some() {
let mut vbuf = Vec::new();
if let Some(ref val) = self.invite_note {
crate::cbor::Encoder::new(&mut vbuf).encode_text(val)?;
}
pairs.push(("inviteNote", vbuf));
}
if self.deactivated_at.is_some() {
let mut vbuf = Vec::new();
if let Some(ref val) = self.deactivated_at {
crate::cbor::Encoder::new(&mut vbuf).encode_text(val.as_str())?;
}
pairs.push(("deactivatedAt", vbuf));
}
if self.invites_disabled.is_some() {
let mut vbuf = Vec::new();
if let Some(ref val) = self.invites_disabled {
crate::cbor::Encoder::new(&mut vbuf).encode_bool(*val)?;
}
pairs.push(("invitesDisabled", vbuf));
}
if self.email_confirmed_at.is_some() {
let mut vbuf = Vec::new();
if let Some(ref val) = self.email_confirmed_at {
crate::cbor::Encoder::new(&mut vbuf).encode_text(val.as_str())?;
}
pairs.push(("emailConfirmedAt", vbuf));
}
if !self.threat_signatures.is_empty() {
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf)
.encode_array_header(self.threat_signatures.len() as u64)?;
for item in &self.threat_signatures {
item.encode_cbor(&mut vbuf)?;
}
pairs.push(("threatSignatures", 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_did: Option<crate::syntax::Did> = None;
let mut field_email: Option<String> = None;
let mut field_handle: Option<crate::syntax::Handle> = None;
let mut field_invites: Vec<crate::api::com::atproto::ServerDefsInviteCode> = Vec::new();
let mut field_indexed_at: Option<crate::syntax::Datetime> = None;
let mut field_invited_by: Option<crate::api::com::atproto::ServerDefsInviteCode> = None;
let mut field_invite_note: Option<String> = None;
let mut field_deactivated_at: Option<crate::syntax::Datetime> = None;
let mut field_invites_disabled: Option<bool> = None;
let mut field_email_confirmed_at: Option<crate::syntax::Datetime> = None;
let mut field_threat_signatures: Vec<AdminDefsThreatSignature> = Vec::new();
let mut extra_cbor: Vec<(String, Vec<u8>)> = Vec::new();
for (key, value) in entries {
match key {
"did" => {
if let crate::cbor::Value::Text(s) = value {
field_did = Some(
crate::syntax::Did::try_from(s)
.map_err(|e| crate::cbor::CborError::InvalidCbor(e.to_string()))?,
);
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
}
}
"email" => {
if let crate::cbor::Value::Text(s) = value {
field_email = Some(s.to_string());
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
}
}
"handle" => {
if let crate::cbor::Value::Text(s) = value {
field_handle = Some(
crate::syntax::Handle::try_from(s)
.map_err(|e| crate::cbor::CborError::InvalidCbor(e.to_string()))?,
);
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
}
}
"invites" => {
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_invites.push(
crate::api::com::atproto::ServerDefsInviteCode::decode_cbor(
&mut dec,
)?,
);
}
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected array".into()));
}
}
"indexedAt" => {
if let crate::cbor::Value::Text(s) = value {
field_indexed_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()));
}
}
"invitedBy" => {
let raw = crate::cbor::encode_value(&value)?;
let mut dec = crate::cbor::Decoder::new(&raw);
field_invited_by = Some(
crate::api::com::atproto::ServerDefsInviteCode::decode_cbor(&mut dec)?,
);
}
"inviteNote" => {
if let crate::cbor::Value::Text(s) = value {
field_invite_note = Some(s.to_string());
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
}
}
"deactivatedAt" => {
if let crate::cbor::Value::Text(s) = value {
field_deactivated_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()));
}
}
"invitesDisabled" => {
if let crate::cbor::Value::Bool(b) = value {
field_invites_disabled = Some(b);
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected bool".into()));
}
}
"emailConfirmedAt" => {
if let crate::cbor::Value::Text(s) = value {
field_email_confirmed_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()));
}
}
"threatSignatures" => {
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_threat_signatures
.push(AdminDefsThreatSignature::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(AdminDefsAccountView {
did: field_did.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'did'".into())
})?,
email: field_email,
handle: field_handle.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'handle'".into())
})?,
invites: field_invites,
indexed_at: field_indexed_at.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'indexedAt'".into())
})?,
invited_by: field_invited_by,
invite_note: field_invite_note,
deactivated_at: field_deactivated_at,
related_records: Default::default(),
invites_disabled: field_invites_disabled,
email_confirmed_at: field_email_confirmed_at,
threat_signatures: field_threat_signatures,
extra: std::collections::HashMap::new(),
extra_cbor,
})
}
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AdminDefsRepoBlobRef {
pub cid: String,
pub did: crate::syntax::Did,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub record_uri: Option<crate::syntax::AtUri>,
#[serde(flatten)]
pub extra: std::collections::HashMap<String, serde_json::Value>,
#[serde(skip)]
pub extra_cbor: Vec<(String, Vec<u8>)>,
}
impl AdminDefsRepoBlobRef {
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.record_uri.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("did")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(self.did.as_str())?;
if self.record_uri.is_some() {
crate::cbor::Encoder::new(&mut *buf).encode_text("recordUri")?;
if let Some(ref val) = self.record_uri {
crate::cbor::Encoder::new(&mut *buf).encode_text(val.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.cid)?;
pairs.push(("cid", vbuf));
}
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(self.did.as_str())?;
pairs.push(("did", vbuf));
}
if self.record_uri.is_some() {
let mut vbuf = Vec::new();
if let Some(ref val) = self.record_uri {
crate::cbor::Encoder::new(&mut vbuf).encode_text(val.as_str())?;
}
pairs.push(("recordUri", 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_did: Option<crate::syntax::Did> = None;
let mut field_record_uri: Option<crate::syntax::AtUri> = 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()));
}
}
"did" => {
if let crate::cbor::Value::Text(s) = value {
field_did = Some(
crate::syntax::Did::try_from(s)
.map_err(|e| crate::cbor::CborError::InvalidCbor(e.to_string()))?,
);
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
}
}
"recordUri" => {
if let crate::cbor::Value::Text(s) = value {
field_record_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()));
}
}
_ => {
let raw = crate::cbor::encode_value(&value)?;
extra_cbor.push((key.to_string(), raw));
}
}
}
Ok(AdminDefsRepoBlobRef {
cid: field_cid.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'cid'".into())
})?,
did: field_did.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'did'".into())
})?,
record_uri: field_record_uri,
extra: std::collections::HashMap::new(),
extra_cbor,
})
}
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AdminDefsRepoRef {
pub did: crate::syntax::Did,
#[serde(flatten)]
pub extra: std::collections::HashMap<String, serde_json::Value>,
#[serde(skip)]
pub extra_cbor: Vec<(String, Vec<u8>)>,
}
impl AdminDefsRepoRef {
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("did")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(self.did.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.did.as_str())?;
pairs.push(("did", 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_did: Option<crate::syntax::Did> = None;
let mut extra_cbor: Vec<(String, Vec<u8>)> = Vec::new();
for (key, value) in entries {
match key {
"did" => {
if let crate::cbor::Value::Text(s) = value {
field_did = Some(
crate::syntax::Did::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(AdminDefsRepoRef {
did: field_did.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'did'".into())
})?,
extra: std::collections::HashMap::new(),
extra_cbor,
})
}
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AdminDefsStatusAttr {
pub applied: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub r#ref: Option<String>,
#[serde(flatten)]
pub extra: std::collections::HashMap<String, serde_json::Value>,
#[serde(skip)]
pub extra_cbor: Vec<(String, Vec<u8>)>,
}
impl AdminDefsStatusAttr {
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.r#ref.is_some() {
count += 1;
}
crate::cbor::Encoder::new(&mut *buf).encode_map_header(count)?;
if self.r#ref.is_some() {
crate::cbor::Encoder::new(&mut *buf).encode_text("ref")?;
if let Some(ref val) = self.r#ref {
crate::cbor::Encoder::new(&mut *buf).encode_text(val)?;
}
}
crate::cbor::Encoder::new(&mut *buf).encode_text("applied")?;
crate::cbor::Encoder::new(&mut *buf).encode_bool(self.applied)?;
} else {
let mut pairs: Vec<(&str, Vec<u8>)> = Vec::new();
if self.r#ref.is_some() {
let mut vbuf = Vec::new();
if let Some(ref val) = self.r#ref {
crate::cbor::Encoder::new(&mut vbuf).encode_text(val)?;
}
pairs.push(("ref", vbuf));
}
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_bool(self.applied)?;
pairs.push(("applied", 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_ref: Option<String> = None;
let mut field_applied: Option<bool> = None;
let mut extra_cbor: Vec<(String, Vec<u8>)> = Vec::new();
for (key, value) in entries {
match key {
"ref" => {
if let crate::cbor::Value::Text(s) = value {
field_ref = Some(s.to_string());
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
}
}
"applied" => {
if let crate::cbor::Value::Bool(b) = value {
field_applied = Some(b);
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected bool".into()));
}
}
_ => {
let raw = crate::cbor::encode_value(&value)?;
extra_cbor.push((key.to_string(), raw));
}
}
}
Ok(AdminDefsStatusAttr {
r#ref: field_ref,
applied: field_applied.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'applied'".into())
})?,
extra: std::collections::HashMap::new(),
extra_cbor,
})
}
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AdminDefsThreatSignature {
pub property: String,
pub value: String,
#[serde(flatten)]
pub extra: std::collections::HashMap<String, serde_json::Value>,
#[serde(skip)]
pub extra_cbor: Vec<(String, Vec<u8>)>,
}
impl AdminDefsThreatSignature {
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("value")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(&self.value)?;
crate::cbor::Encoder::new(&mut *buf).encode_text("property")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(&self.property)?;
} else {
let mut pairs: Vec<(&str, Vec<u8>)> = Vec::new();
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(&self.value)?;
pairs.push(("value", vbuf));
}
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(&self.property)?;
pairs.push(("property", 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_value: Option<String> = None;
let mut field_property: Option<String> = None;
let mut extra_cbor: Vec<(String, Vec<u8>)> = Vec::new();
for (key, value) in entries {
match key {
"value" => {
if let crate::cbor::Value::Text(s) = value {
field_value = Some(s.to_string());
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
}
}
"property" => {
if let crate::cbor::Value::Text(s) = value {
field_property = 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(AdminDefsThreatSignature {
value: field_value.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'value'".into())
})?,
property: field_property.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'property'".into())
})?,
extra: std::collections::HashMap::new(),
extra_cbor,
})
}
}