#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct LabelDefsLabel {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub cid: Option<String>,
pub cts: crate::syntax::Datetime,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub exp: Option<crate::syntax::Datetime>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub neg: Option<bool>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub sig: Option<String>,
pub src: crate::syntax::Did,
pub uri: String,
pub val: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub ver: Option<i64>,
#[serde(flatten)]
pub extra: std::collections::HashMap<String, serde_json::Value>,
#[serde(skip)]
pub extra_cbor: Vec<(String, Vec<u8>)>,
}
impl LabelDefsLabel {
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 = 4u64;
if self.cid.is_some() {
count += 1;
}
if self.exp.is_some() {
count += 1;
}
if self.neg.is_some() {
count += 1;
}
if self.sig.is_some() {
count += 1;
}
if self.ver.is_some() {
count += 1;
}
crate::cbor::Encoder::new(&mut *buf).encode_map_header(count)?;
if self.cid.is_some() {
crate::cbor::Encoder::new(&mut *buf).encode_text("cid")?;
if let Some(ref val) = self.cid {
crate::cbor::Encoder::new(&mut *buf).encode_text(val)?;
}
}
crate::cbor::Encoder::new(&mut *buf).encode_text("cts")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(self.cts.as_str())?;
if self.exp.is_some() {
crate::cbor::Encoder::new(&mut *buf).encode_text("exp")?;
if let Some(ref val) = self.exp {
crate::cbor::Encoder::new(&mut *buf).encode_text(val.as_str())?;
}
}
if self.neg.is_some() {
crate::cbor::Encoder::new(&mut *buf).encode_text("neg")?;
if let Some(ref val) = self.neg {
crate::cbor::Encoder::new(&mut *buf).encode_bool(*val)?;
}
}
if self.sig.is_some() {
crate::cbor::Encoder::new(&mut *buf).encode_text("sig")?;
if let Some(ref val) = self.sig {
crate::cbor::Encoder::new(&mut *buf).encode_text(val)?;
}
}
crate::cbor::Encoder::new(&mut *buf).encode_text("src")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(self.src.as_str())?;
crate::cbor::Encoder::new(&mut *buf).encode_text("uri")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(&self.uri)?;
crate::cbor::Encoder::new(&mut *buf).encode_text("val")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(&self.val)?;
if self.ver.is_some() {
crate::cbor::Encoder::new(&mut *buf).encode_text("ver")?;
if let Some(ref val) = self.ver {
crate::cbor::Encoder::new(&mut *buf).encode_i64(*val)?;
}
}
} else {
let mut pairs: Vec<(&str, Vec<u8>)> = Vec::new();
if self.cid.is_some() {
let mut vbuf = Vec::new();
if let Some(ref val) = self.cid {
crate::cbor::Encoder::new(&mut vbuf).encode_text(val)?;
}
pairs.push(("cid", vbuf));
}
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(self.cts.as_str())?;
pairs.push(("cts", vbuf));
}
if self.exp.is_some() {
let mut vbuf = Vec::new();
if let Some(ref val) = self.exp {
crate::cbor::Encoder::new(&mut vbuf).encode_text(val.as_str())?;
}
pairs.push(("exp", vbuf));
}
if self.neg.is_some() {
let mut vbuf = Vec::new();
if let Some(ref val) = self.neg {
crate::cbor::Encoder::new(&mut vbuf).encode_bool(*val)?;
}
pairs.push(("neg", vbuf));
}
if self.sig.is_some() {
let mut vbuf = Vec::new();
if let Some(ref val) = self.sig {
crate::cbor::Encoder::new(&mut vbuf).encode_text(val)?;
}
pairs.push(("sig", vbuf));
}
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(self.src.as_str())?;
pairs.push(("src", vbuf));
}
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(&self.uri)?;
pairs.push(("uri", vbuf));
}
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(&self.val)?;
pairs.push(("val", vbuf));
}
if self.ver.is_some() {
let mut vbuf = Vec::new();
if let Some(ref val) = self.ver {
crate::cbor::Encoder::new(&mut vbuf).encode_i64(*val)?;
}
pairs.push(("ver", 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_cts: Option<crate::syntax::Datetime> = None;
let mut field_exp: Option<crate::syntax::Datetime> = None;
let mut field_neg: Option<bool> = None;
let mut field_sig: Option<String> = None;
let mut field_src: Option<crate::syntax::Did> = None;
let mut field_uri: Option<String> = None;
let mut field_val: Option<String> = None;
let mut field_ver: Option<i64> = 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()));
}
}
"cts" => {
if let crate::cbor::Value::Text(s) = value {
field_cts = 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()));
}
}
"exp" => {
if let crate::cbor::Value::Text(s) = value {
field_exp = 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()));
}
}
"neg" => {
if let crate::cbor::Value::Bool(b) = value {
field_neg = Some(b);
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected bool".into()));
}
}
"sig" => {
if let crate::cbor::Value::Text(s) = value {
field_sig = Some(s.to_string());
} else {
return Err(crate::cbor::CborError::InvalidCbor(
"expected text for bytes field".into(),
));
}
}
"src" => {
if let crate::cbor::Value::Text(s) = value {
field_src = 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()));
}
}
"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()));
}
}
"val" => {
if let crate::cbor::Value::Text(s) = value {
field_val = Some(s.to_string());
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
}
}
"ver" => match value {
crate::cbor::Value::Unsigned(n) => {
field_ver = Some(n as i64);
}
crate::cbor::Value::Signed(n) => {
field_ver = 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(LabelDefsLabel {
cid: field_cid,
cts: field_cts.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'cts'".into())
})?,
exp: field_exp,
neg: field_neg,
sig: field_sig,
src: field_src.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'src'".into())
})?,
uri: field_uri.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'uri'".into())
})?,
val: field_val.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'val'".into())
})?,
ver: field_ver,
extra: std::collections::HashMap::new(),
extra_cbor,
})
}
}
pub type LabelDefsLabelValue = String;
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct LabelDefsLabelValueDefinition {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub adult_only: Option<bool>,
pub blurs: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub default_setting: Option<String>,
pub identifier: String,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub locales: Vec<LabelDefsLabelValueDefinitionStrings>,
pub severity: String,
#[serde(flatten)]
pub extra: std::collections::HashMap<String, serde_json::Value>,
#[serde(skip)]
pub extra_cbor: Vec<(String, Vec<u8>)>,
}
impl LabelDefsLabelValueDefinition {
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 = 4u64;
if self.adult_only.is_some() {
count += 1;
}
if self.default_setting.is_some() {
count += 1;
}
crate::cbor::Encoder::new(&mut *buf).encode_map_header(count)?;
crate::cbor::Encoder::new(&mut *buf).encode_text("blurs")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(&self.blurs)?;
crate::cbor::Encoder::new(&mut *buf).encode_text("locales")?;
crate::cbor::Encoder::new(&mut *buf).encode_array_header(self.locales.len() as u64)?;
for item in &self.locales {
item.encode_cbor(buf)?;
}
crate::cbor::Encoder::new(&mut *buf).encode_text("severity")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(&self.severity)?;
if self.adult_only.is_some() {
crate::cbor::Encoder::new(&mut *buf).encode_text("adultOnly")?;
if let Some(ref val) = self.adult_only {
crate::cbor::Encoder::new(&mut *buf).encode_bool(*val)?;
}
}
crate::cbor::Encoder::new(&mut *buf).encode_text("identifier")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(&self.identifier)?;
if self.default_setting.is_some() {
crate::cbor::Encoder::new(&mut *buf).encode_text("defaultSetting")?;
if let Some(ref val) = self.default_setting {
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.blurs)?;
pairs.push(("blurs", vbuf));
}
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf)
.encode_array_header(self.locales.len() as u64)?;
for item in &self.locales {
item.encode_cbor(&mut vbuf)?;
}
pairs.push(("locales", vbuf));
}
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(&self.severity)?;
pairs.push(("severity", vbuf));
}
if self.adult_only.is_some() {
let mut vbuf = Vec::new();
if let Some(ref val) = self.adult_only {
crate::cbor::Encoder::new(&mut vbuf).encode_bool(*val)?;
}
pairs.push(("adultOnly", vbuf));
}
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(&self.identifier)?;
pairs.push(("identifier", vbuf));
}
if self.default_setting.is_some() {
let mut vbuf = Vec::new();
if let Some(ref val) = self.default_setting {
crate::cbor::Encoder::new(&mut vbuf).encode_text(val)?;
}
pairs.push(("defaultSetting", 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_blurs: Option<String> = None;
let mut field_locales: Vec<LabelDefsLabelValueDefinitionStrings> = Vec::new();
let mut field_severity: Option<String> = None;
let mut field_adult_only: Option<bool> = None;
let mut field_identifier: Option<String> = None;
let mut field_default_setting: Option<String> = None;
let mut extra_cbor: Vec<(String, Vec<u8>)> = Vec::new();
for (key, value) in entries {
match key {
"blurs" => {
if let crate::cbor::Value::Text(s) = value {
field_blurs = Some(s.to_string());
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
}
}
"locales" => {
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_locales
.push(LabelDefsLabelValueDefinitionStrings::decode_cbor(&mut dec)?);
}
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected array".into()));
}
}
"severity" => {
if let crate::cbor::Value::Text(s) = value {
field_severity = Some(s.to_string());
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
}
}
"adultOnly" => {
if let crate::cbor::Value::Bool(b) = value {
field_adult_only = Some(b);
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected bool".into()));
}
}
"identifier" => {
if let crate::cbor::Value::Text(s) = value {
field_identifier = Some(s.to_string());
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
}
}
"defaultSetting" => {
if let crate::cbor::Value::Text(s) = value {
field_default_setting = 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(LabelDefsLabelValueDefinition {
blurs: field_blurs.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'blurs'".into())
})?,
locales: field_locales,
severity: field_severity.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'severity'".into())
})?,
adult_only: field_adult_only,
identifier: field_identifier.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'identifier'".into())
})?,
default_setting: field_default_setting,
extra: std::collections::HashMap::new(),
extra_cbor,
})
}
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct LabelDefsLabelValueDefinitionStrings {
pub description: String,
pub lang: crate::syntax::Language,
pub name: String,
#[serde(flatten)]
pub extra: std::collections::HashMap<String, serde_json::Value>,
#[serde(skip)]
pub extra_cbor: Vec<(String, Vec<u8>)>,
}
impl LabelDefsLabelValueDefinitionStrings {
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("lang")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(self.lang.as_str())?;
crate::cbor::Encoder::new(&mut *buf).encode_text("name")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(&self.name)?;
crate::cbor::Encoder::new(&mut *buf).encode_text("description")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(&self.description)?;
} else {
let mut pairs: Vec<(&str, Vec<u8>)> = Vec::new();
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(self.lang.as_str())?;
pairs.push(("lang", vbuf));
}
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(&self.name)?;
pairs.push(("name", vbuf));
}
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(&self.description)?;
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_lang: Option<crate::syntax::Language> = None;
let mut field_name: 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 {
"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()));
}
}
"name" => {
if let crate::cbor::Value::Text(s) = value {
field_name = 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(LabelDefsLabelValueDefinitionStrings {
lang: field_lang.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'lang'".into())
})?,
name: field_name.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'name'".into())
})?,
description: field_description.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'description'".into())
})?,
extra: std::collections::HashMap::new(),
extra_cbor,
})
}
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct LabelDefsSelfLabel {
pub val: String,
#[serde(flatten)]
pub extra: std::collections::HashMap<String, serde_json::Value>,
#[serde(skip)]
pub extra_cbor: Vec<(String, Vec<u8>)>,
}
impl LabelDefsSelfLabel {
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("val")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(&self.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.val)?;
pairs.push(("val", 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_val: Option<String> = None;
let mut extra_cbor: Vec<(String, Vec<u8>)> = Vec::new();
for (key, value) in entries {
match key {
"val" => {
if let crate::cbor::Value::Text(s) = value {
field_val = 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(LabelDefsSelfLabel {
val: field_val.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'val'".into())
})?,
extra: std::collections::HashMap::new(),
extra_cbor,
})
}
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct LabelDefsSelfLabels {
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub values: Vec<LabelDefsSelfLabel>,
#[serde(flatten)]
pub extra: std::collections::HashMap<String, serde_json::Value>,
#[serde(skip)]
pub extra_cbor: Vec<(String, Vec<u8>)>,
}
impl LabelDefsSelfLabels {
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("values")?;
crate::cbor::Encoder::new(&mut *buf).encode_array_header(self.values.len() as u64)?;
for item in &self.values {
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_array_header(self.values.len() as u64)?;
for item in &self.values {
item.encode_cbor(&mut vbuf)?;
}
pairs.push(("values", 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_values: Vec<LabelDefsSelfLabel> = Vec::new();
let mut extra_cbor: Vec<(String, Vec<u8>)> = Vec::new();
for (key, value) in entries {
match key {
"values" => {
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_values.push(LabelDefsSelfLabel::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(LabelDefsSelfLabels {
values: field_values,
extra: std::collections::HashMap::new(),
extra_cbor,
})
}
}