pub type SafelinkDefsActionType = String;
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SafelinkDefsEvent {
pub action: SafelinkDefsActionType,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub comment: Option<String>,
pub created_at: crate::syntax::Datetime,
pub created_by: crate::syntax::Did,
pub event_type: SafelinkDefsEventType,
pub id: i64,
pub pattern: SafelinkDefsPatternType,
pub reason: SafelinkDefsReasonType,
pub url: String,
#[serde(flatten)]
pub extra: std::collections::HashMap<String, serde_json::Value>,
#[serde(skip)]
pub extra_cbor: Vec<(String, Vec<u8>)>,
}
impl SafelinkDefsEvent {
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 = 8u64;
if self.comment.is_some() {
count += 1;
}
crate::cbor::Encoder::new(&mut *buf).encode_map_header(count)?;
crate::cbor::Encoder::new(&mut *buf).encode_text("id")?;
crate::cbor::Encoder::new(&mut *buf).encode_i64(self.id)?;
crate::cbor::Encoder::new(&mut *buf).encode_text("url")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(&self.url)?;
crate::cbor::Encoder::new(&mut *buf).encode_text("action")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(&self.action)?;
crate::cbor::Encoder::new(&mut *buf).encode_text("reason")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(&self.reason)?;
if self.comment.is_some() {
crate::cbor::Encoder::new(&mut *buf).encode_text("comment")?;
if let Some(ref val) = self.comment {
crate::cbor::Encoder::new(&mut *buf).encode_text(val)?;
}
}
crate::cbor::Encoder::new(&mut *buf).encode_text("pattern")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(&self.pattern)?;
crate::cbor::Encoder::new(&mut *buf).encode_text("createdAt")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(self.created_at.as_str())?;
crate::cbor::Encoder::new(&mut *buf).encode_text("createdBy")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(self.created_by.as_str())?;
crate::cbor::Encoder::new(&mut *buf).encode_text("eventType")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(&self.event_type)?;
} else {
let mut pairs: Vec<(&str, Vec<u8>)> = Vec::new();
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_i64(self.id)?;
pairs.push(("id", vbuf));
}
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(&self.url)?;
pairs.push(("url", vbuf));
}
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(&self.action)?;
pairs.push(("action", vbuf));
}
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(&self.reason)?;
pairs.push(("reason", vbuf));
}
if self.comment.is_some() {
let mut vbuf = Vec::new();
if let Some(ref val) = self.comment {
crate::cbor::Encoder::new(&mut vbuf).encode_text(val)?;
}
pairs.push(("comment", vbuf));
}
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(&self.pattern)?;
pairs.push(("pattern", vbuf));
}
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(self.created_at.as_str())?;
pairs.push(("createdAt", vbuf));
}
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(self.created_by.as_str())?;
pairs.push(("createdBy", vbuf));
}
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(&self.event_type)?;
pairs.push(("eventType", 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_id: Option<i64> = None;
let mut field_url: Option<String> = None;
let mut field_action: Option<SafelinkDefsActionType> = None;
let mut field_reason: Option<SafelinkDefsReasonType> = None;
let mut field_comment: Option<String> = None;
let mut field_pattern: Option<SafelinkDefsPatternType> = None;
let mut field_created_at: Option<crate::syntax::Datetime> = None;
let mut field_created_by: Option<crate::syntax::Did> = None;
let mut field_event_type: Option<SafelinkDefsEventType> = None;
let mut extra_cbor: Vec<(String, Vec<u8>)> = Vec::new();
for (key, value) in entries {
match key {
"id" => match value {
crate::cbor::Value::Unsigned(n) => {
field_id = Some(n as i64);
}
crate::cbor::Value::Signed(n) => {
field_id = Some(n);
}
_ => {
return Err(crate::cbor::CborError::InvalidCbor(
"expected integer".into(),
));
}
},
"url" => {
if let crate::cbor::Value::Text(s) = value {
field_url = Some(s.to_string());
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
}
}
"action" => {
if let crate::cbor::Value::Text(s) = value {
field_action = Some(s.to_string());
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
}
}
"reason" => {
if let crate::cbor::Value::Text(s) = value {
field_reason = Some(s.to_string());
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
}
}
"comment" => {
if let crate::cbor::Value::Text(s) = value {
field_comment = Some(s.to_string());
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
}
}
"pattern" => {
if let crate::cbor::Value::Text(s) = value {
field_pattern = Some(s.to_string());
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
}
}
"createdAt" => {
if let crate::cbor::Value::Text(s) = value {
field_created_at = Some(
crate::syntax::Datetime::try_from(s)
.map_err(|e| crate::cbor::CborError::InvalidCbor(e.to_string()))?,
);
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
}
}
"createdBy" => {
if let crate::cbor::Value::Text(s) = value {
field_created_by = 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()));
}
}
"eventType" => {
if let crate::cbor::Value::Text(s) = value {
field_event_type = 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(SafelinkDefsEvent {
id: field_id.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'id'".into())
})?,
url: field_url.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'url'".into())
})?,
action: field_action.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'action'".into())
})?,
reason: field_reason.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'reason'".into())
})?,
comment: field_comment,
pattern: field_pattern.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'pattern'".into())
})?,
created_at: field_created_at.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'createdAt'".into())
})?,
created_by: field_created_by.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'createdBy'".into())
})?,
event_type: field_event_type.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'eventType'".into())
})?,
extra: std::collections::HashMap::new(),
extra_cbor,
})
}
}
pub type SafelinkDefsEventType = String;
pub type SafelinkDefsPatternType = String;
pub type SafelinkDefsReasonType = String;
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SafelinkDefsUrlRule {
pub action: SafelinkDefsActionType,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub comment: Option<String>,
pub created_at: crate::syntax::Datetime,
pub created_by: crate::syntax::Did,
pub pattern: SafelinkDefsPatternType,
pub reason: SafelinkDefsReasonType,
pub updated_at: crate::syntax::Datetime,
pub url: String,
#[serde(flatten)]
pub extra: std::collections::HashMap<String, serde_json::Value>,
#[serde(skip)]
pub extra_cbor: Vec<(String, Vec<u8>)>,
}
impl SafelinkDefsUrlRule {
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 = 7u64;
if self.comment.is_some() {
count += 1;
}
crate::cbor::Encoder::new(&mut *buf).encode_map_header(count)?;
crate::cbor::Encoder::new(&mut *buf).encode_text("url")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(&self.url)?;
crate::cbor::Encoder::new(&mut *buf).encode_text("action")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(&self.action)?;
crate::cbor::Encoder::new(&mut *buf).encode_text("reason")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(&self.reason)?;
if self.comment.is_some() {
crate::cbor::Encoder::new(&mut *buf).encode_text("comment")?;
if let Some(ref val) = self.comment {
crate::cbor::Encoder::new(&mut *buf).encode_text(val)?;
}
}
crate::cbor::Encoder::new(&mut *buf).encode_text("pattern")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(&self.pattern)?;
crate::cbor::Encoder::new(&mut *buf).encode_text("createdAt")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(self.created_at.as_str())?;
crate::cbor::Encoder::new(&mut *buf).encode_text("createdBy")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(self.created_by.as_str())?;
crate::cbor::Encoder::new(&mut *buf).encode_text("updatedAt")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(self.updated_at.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.url)?;
pairs.push(("url", vbuf));
}
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(&self.action)?;
pairs.push(("action", vbuf));
}
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(&self.reason)?;
pairs.push(("reason", vbuf));
}
if self.comment.is_some() {
let mut vbuf = Vec::new();
if let Some(ref val) = self.comment {
crate::cbor::Encoder::new(&mut vbuf).encode_text(val)?;
}
pairs.push(("comment", vbuf));
}
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(&self.pattern)?;
pairs.push(("pattern", vbuf));
}
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(self.created_at.as_str())?;
pairs.push(("createdAt", vbuf));
}
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(self.created_by.as_str())?;
pairs.push(("createdBy", vbuf));
}
{
let mut vbuf = Vec::new();
crate::cbor::Encoder::new(&mut vbuf).encode_text(self.updated_at.as_str())?;
pairs.push(("updatedAt", 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_url: Option<String> = None;
let mut field_action: Option<SafelinkDefsActionType> = None;
let mut field_reason: Option<SafelinkDefsReasonType> = None;
let mut field_comment: Option<String> = None;
let mut field_pattern: Option<SafelinkDefsPatternType> = None;
let mut field_created_at: Option<crate::syntax::Datetime> = None;
let mut field_created_by: Option<crate::syntax::Did> = None;
let mut field_updated_at: Option<crate::syntax::Datetime> = None;
let mut extra_cbor: Vec<(String, Vec<u8>)> = Vec::new();
for (key, value) in entries {
match key {
"url" => {
if let crate::cbor::Value::Text(s) = value {
field_url = Some(s.to_string());
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
}
}
"action" => {
if let crate::cbor::Value::Text(s) = value {
field_action = Some(s.to_string());
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
}
}
"reason" => {
if let crate::cbor::Value::Text(s) = value {
field_reason = Some(s.to_string());
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
}
}
"comment" => {
if let crate::cbor::Value::Text(s) = value {
field_comment = Some(s.to_string());
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
}
}
"pattern" => {
if let crate::cbor::Value::Text(s) = value {
field_pattern = Some(s.to_string());
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
}
}
"createdAt" => {
if let crate::cbor::Value::Text(s) = value {
field_created_at = Some(
crate::syntax::Datetime::try_from(s)
.map_err(|e| crate::cbor::CborError::InvalidCbor(e.to_string()))?,
);
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
}
}
"createdBy" => {
if let crate::cbor::Value::Text(s) = value {
field_created_by = 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()));
}
}
"updatedAt" => {
if let crate::cbor::Value::Text(s) = value {
field_updated_at = Some(
crate::syntax::Datetime::try_from(s)
.map_err(|e| crate::cbor::CborError::InvalidCbor(e.to_string()))?,
);
} else {
return Err(crate::cbor::CborError::InvalidCbor("expected text".into()));
}
}
_ => {
let raw = crate::cbor::encode_value(&value)?;
extra_cbor.push((key.to_string(), raw));
}
}
}
Ok(SafelinkDefsUrlRule {
url: field_url.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'url'".into())
})?,
action: field_action.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'action'".into())
})?,
reason: field_reason.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'reason'".into())
})?,
comment: field_comment,
pattern: field_pattern.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'pattern'".into())
})?,
created_at: field_created_at.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'createdAt'".into())
})?,
created_by: field_created_by.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'createdBy'".into())
})?,
updated_at: field_updated_at.ok_or_else(|| {
crate::cbor::CborError::InvalidCbor("missing required field 'updatedAt'".into())
})?,
extra: std::collections::HashMap::new(),
extra_cbor,
})
}
}