From e5d610270bed47a91ecca8d51adc705bed58cda7 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sat, 4 Dec 2021 17:03:09 +0100 Subject: Rewrite using "ipfs" crate Signed-off-by: Matthias Beyer --- src/types/payload.rs | 58 +++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 53 insertions(+), 5 deletions(-) (limited to 'src/types/payload.rs') diff --git a/src/types/payload.rs b/src/types/payload.rs index cb12845..a11b215 100644 --- a/src/types/payload.rs +++ b/src/types/payload.rs @@ -1,6 +1,10 @@ +use std::convert::TryFrom; + +use anyhow::Result; + use crate::types::DateTime; -#[derive(Debug, Eq, PartialEq, serde::Serialize, serde::Deserialize, getset::Getters)] +#[derive(Debug, Eq, PartialEq, getset::Getters)] pub struct Payload { // TODO: Make this a mime::Mime, but as this type does not impl Serialize/Deserialize, we // cannot do this trivially yet @@ -10,15 +14,59 @@ pub struct Payload { #[getset(get = "pub")] timestamp: DateTime, - content: crate::types::encodable_cid::EncodableCid, + content: ipfs::Cid, +} + +impl Into for Payload { + fn into(self) -> ipfs::Ipld { + let mut map = std::collections::BTreeMap::new(); + map.insert(String::from("mime"), ipfs::Ipld::String(self.mime)); + map.insert(String::from("timestamp"), self.timestamp.into()); + map.insert(String::from("content"), ipfs::Ipld::Link(self.content)); + ipfs::Ipld::Map(map) + } +} + +impl TryFrom for Payload { + type Error = anyhow::Error; + + fn try_from(ipld: ipfs::Ipld) -> Result { + let missing_field = |name: &'static str| move || anyhow::anyhow!("Missing field {}", name); + let field_wrong_type = |name: &str, expty: &str| anyhow::bail!("Field {} has wrong type, expected {}", name, expty); + match ipld { + ipfs::Ipld::Map(map) => { + let mime = match map.get("mime").ok_or_else(missing_field("mime"))? { + ipfs::Ipld::String(s) => s.to_owned(), + _ => return field_wrong_type("mime", "String") + }; + + let timestamp = map.get("timestamp") + .ok_or_else(missing_field("timestamp"))?; + let timestamp = DateTime::try_from(timestamp.clone())?; // TODO dont clone + + let content = match map.get("content").ok_or_else(missing_field("content"))? { + ipfs::Ipld::Link(cid) => cid.clone(), + _ => return field_wrong_type("content", "Link") + }; + + Ok(Payload { + mime, + timestamp, + content + }) + }, + + _ => anyhow::bail!("Unexpected type, expected map"), + } + } } impl Payload { - pub fn new(mime: String, timestamp: DateTime, content: crate::cid::Cid) -> Self { + pub fn new(mime: String, timestamp: DateTime, content: ipfs::Cid) -> Self { Self { mime, timestamp, content: content.into() } } - pub fn content(&self) -> crate::cid::Cid { - self.content.clone().into() + pub fn content(&self) -> ipfs::Cid { + self.content.clone() } } -- cgit v1.2.3