pub mod app;
pub mod chat;
pub mod com;
pub mod tools;
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
pub struct CidLink {
#[serde(rename = "$link")]
pub link: String,
}
impl CidLink {
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> {
let cid = self
.link
.parse::<crate::cbor::Cid>()
.map_err(|e| crate::cbor::CborError::InvalidCbor(format!("invalid CID: {e}")))?;
crate::cbor::Encoder::new(&mut *buf).encode_cid(&cid)?;
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()?;
if let crate::cbor::Value::Cid(c) = val {
Ok(CidLink {
link: c.to_string(),
})
} else {
Err(crate::cbor::CborError::InvalidCbor(
"expected CID for CidLink".into(),
))
}
}
}
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
pub struct Blob {
#[serde(rename = "$type")]
#[serde(default)]
pub r#type: String,
#[serde(default)]
pub r#ref: Option<CidLink>,
#[serde(rename = "mimeType")]
#[serde(default)]
pub mime_type: String,
#[serde(default)]
pub size: i64,
}
impl Blob {
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> {
let mut count = 3u64; if self.r#ref.is_some() {
count += 1;
}
crate::cbor::Encoder::new(&mut *buf).encode_map_header(count)?;
if let Some(ref cid_link) = self.r#ref {
crate::cbor::Encoder::new(&mut *buf).encode_text("ref")?;
cid_link.encode_cbor(buf)?;
}
crate::cbor::Encoder::new(&mut *buf).encode_text("size")?;
crate::cbor::Encoder::new(&mut *buf).encode_i64(self.size)?;
crate::cbor::Encoder::new(&mut *buf).encode_text("$type")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(&self.r#type)?;
crate::cbor::Encoder::new(&mut *buf).encode_text("mimeType")?;
crate::cbor::Encoder::new(&mut *buf).encode_text(&self.mime_type)?;
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 for Blob".into(),
));
}
};
let mut blob = Blob::default();
for (key, value) in entries {
match key {
"$type" => {
if let crate::cbor::Value::Text(s) = value {
blob.r#type = s.to_string();
}
}
"ref" => {
if !matches!(value, crate::cbor::Value::Null) {
let raw = crate::cbor::encode_value(&value)?;
let mut dec = crate::cbor::Decoder::new(&raw);
blob.r#ref = Some(CidLink::decode_cbor(&mut dec)?);
}
}
"mimeType" => {
if let crate::cbor::Value::Text(s) = value {
blob.mime_type = s.to_string();
}
}
"size" => match value {
crate::cbor::Value::Unsigned(n) => blob.size = n as i64,
crate::cbor::Value::Signed(n) => blob.size = n,
_ => {}
},
_ => {}
}
}
Ok(blob)
}
}
#[derive(Debug, Clone)]
pub struct UnknownUnionVariant {
pub r#type: String,
pub json: Option<serde_json::Value>,
pub cbor: Option<Vec<u8>>,
}