diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2021-12-04 17:03:09 +0100 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2021-12-04 21:16:37 +0100 |
commit | e5d610270bed47a91ecca8d51adc705bed58cda7 (patch) | |
tree | 52a5d63edbbf5f8bbf154757969877bce17c3725 /src/types/payload.rs | |
parent | 3a3b342a4ae5a90003b6ef9faa65ca7f0a025962 (diff) |
Rewrite using "ipfs" crate
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
Diffstat (limited to 'src/types/payload.rs')
-rw-r--r-- | src/types/payload.rs | 58 |
1 files changed, 53 insertions, 5 deletions
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<ipfs::Ipld> 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<ipfs::Ipld> for Payload { + type Error = anyhow::Error; + + fn try_from(ipld: ipfs::Ipld) -> Result<Self> { + 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() } } |