summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Cargo.toml4
-rw-r--r--src/cid.rs6
-rw-r--r--src/types/datetime.rs23
-rw-r--r--src/types/id.rs16
-rw-r--r--src/types/mime.rs28
-rw-r--r--src/types/mod.rs6
-rw-r--r--src/types/node.rs25
-rw-r--r--src/types/payload.rs42
8 files changed, 20 insertions, 130 deletions
diff --git a/Cargo.toml b/Cargo.toml
index 7a23d4b..7da53ef 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -18,7 +18,7 @@ edition = "2018"
[dependencies]
anyhow = "1"
async-trait = "0.1"
-chrono = "0.4"
+chrono = { version = "0.4", features = ["serde"] }
cid = "0.7"
clap-v3 = "3.0.0-beta.1"
daglib = { git = "https://git.sr.ht/~matthiasbeyer/daglib", branch = "master" }
@@ -39,3 +39,5 @@ rand_core = { version = "0.6", features = ["getrandom"] }
rand_os = "0.2"
ed25519-dalek = "*"
http = "0.2"
+serde = "1"
+serde_json = "1"
diff --git a/src/cid.rs b/src/cid.rs
index 5a137df..2b89b91 100644
--- a/src/cid.rs
+++ b/src/cid.rs
@@ -7,7 +7,8 @@ use anyhow::Result;
///
/// Hence we just create our own "Cid type" and use that as long as the crate API is stringly
/// typed.
-#[derive(Debug, Eq, PartialEq, Hash)]
+#[derive(Clone, Debug, Eq, PartialEq, Hash, serde::Serialize, serde::Deserialize)]
+#[serde(transparent)]
pub struct Cid(String);
#[cfg(test)]
@@ -28,6 +29,9 @@ impl TryToCid for ipfs_api_backend_hyper::response::AddResponse {
}
}
+impl daglib::NodeId for Cid {
+}
+
/// Helper function that can be tested
///
/// Converts a String to a Cid
diff --git a/src/types/datetime.rs b/src/types/datetime.rs
index ec99282..9034c26 100644
--- a/src/types/datetime.rs
+++ b/src/types/datetime.rs
@@ -1,6 +1,7 @@
use anyhow::Error;
-#[derive(Debug, Eq, PartialEq)]
+#[derive(Debug, Eq, PartialEq, serde::Serialize, serde::Deserialize)]
+#[serde(transparent)]
pub struct DateTime(chrono::DateTime<chrono::Utc>);
impl From<chrono::DateTime<chrono::Utc>> for DateTime {
@@ -9,23 +10,3 @@ impl From<chrono::DateTime<chrono::Utc>> for DateTime {
}
}
-
-impl libipld::codec::Encode<libipld_cbor::DagCborCodec> for DateTime {
- fn encode<W: std::io::Write>(&self, c: libipld_cbor::DagCborCodec, w: &mut W) -> libipld::error::Result<()> {
- self.0.to_rfc3339().encode(c, w).map_err(Error::from)
- }
-}
-
-impl libipld::codec::Decode<libipld_cbor::DagCborCodec> for DateTime {
- fn decode<R: std::io::Read + std::io::Seek>(c: libipld_cbor::DagCborCodec, r: &mut R) -> libipld::error::Result<Self> {
- String::decode(c, r)
- .map_err(Error::from)
- .and_then(|s| {
- chrono::DateTime::parse_from_rfc3339(&s)
- .map(|dt| dt.with_timezone(&chrono::Utc))
- .map_err(Error::from)
- })
- .map(DateTime)
- }
-}
-
diff --git a/src/types/id.rs b/src/types/id.rs
deleted file mode 100644
index 425d077..0000000
--- a/src/types/id.rs
+++ /dev/null
@@ -1,16 +0,0 @@
-#[derive(Clone, Debug, Eq, PartialEq, Hash, libipld::DagCbor)]
-pub struct Id(cid::Cid);
-
-impl daglib::NodeId for Id { }
-
-impl From<cid::Cid> for Id {
- fn from(cid: cid::Cid) -> Self {
- Id(cid)
- }
-}
-
-impl AsRef<cid::Cid> for Id {
- fn as_ref(&self) -> &cid::Cid {
- &self.0
- }
-}
diff --git a/src/types/mime.rs b/src/types/mime.rs
deleted file mode 100644
index 9ea1cb8..0000000
--- a/src/types/mime.rs
+++ /dev/null
@@ -1,28 +0,0 @@
-use anyhow::Error;
-
-#[derive(Debug, Eq, PartialEq)]
-pub struct MimeType(mime::Mime);
-
-impl From<mime::Mime> for MimeType {
- fn from(mime: mime::Mime) -> Self {
- MimeType(mime)
- }
-}
-
-impl<C: libipld::codec::Codec> libipld::codec::Encode<C> for MimeType {
- fn encode<W: std::io::Write>(&self, _c: C, w: &mut W) -> libipld::error::Result<()> {
- w.write_all(self.0.essence_str().as_bytes()).map_err(Error::from)
- }
-}
-
-impl libipld::codec::Decode<libipld_cbor::DagCborCodec> for MimeType {
- fn decode<R: std::io::Read + std::io::Seek>(c: libipld_cbor::DagCborCodec, r: &mut R) -> libipld::error::Result<Self> {
- use std::str::FromStr;
-
- String::decode(c, r)
- .map_err(Error::from)
- .and_then(|s| mime::Mime::from_str(&s).map_err(Error::from))
- .map(MimeType)
- }
-}
-
diff --git a/src/types/mod.rs b/src/types/mod.rs
index afd3168..7382f16 100644
--- a/src/types/mod.rs
+++ b/src/types/mod.rs
@@ -1,14 +1,8 @@
-mod id;
-pub use id::*;
-
mod node;
pub use node::*;
mod datetime;
pub use datetime::*;
-mod mime;
-pub use self::mime::*;
-
mod payload;
pub use payload::*;
diff --git a/src/types/node.rs b/src/types/node.rs
index 89a5c0d..1fce691 100644
--- a/src/types/node.rs
+++ b/src/types/node.rs
@@ -1,37 +1,20 @@
-#[derive(Debug, Eq, PartialEq, libipld::DagCbor)]
+#[derive(Debug, Eq, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct Node {
/// Version
v: String,
/// Parent Nodes, identified by cid
- parents: Vec<crate::types::Id>,
+ parents: Vec<crate::cid::Cid>,
/// The actual payload of the node, which is stored in another document identified by this cid
- payload: cid::Cid,
+ payload: crate::cid::Cid,
}
impl daglib::Node for Node {
- type Id = crate::types::Id;
+ type Id = crate::cid::Cid;
fn parent_ids(&self) -> Vec<Self::Id> {
self.parents.clone()
}
}
-impl Node {
- pub fn version(&self) -> &str {
- &self.v
- }
- pub fn payload_id(&self) -> &cid::Cid {
- &self.payload
- }
-
- pub fn new(version: String, parents: Vec<crate::types::Id>, payload: cid::Cid) -> Self {
- Node {
- v: version,
- parents,
- payload
- }
- }
-
-}
diff --git a/src/types/payload.rs b/src/types/payload.rs
index fd34aab..6f74deb 100644
--- a/src/types/payload.rs
+++ b/src/types/payload.rs
@@ -1,42 +1,12 @@
use crate::types::DateTime;
-use crate::types::MimeType;
-#[derive(Debug, Eq, PartialEq, libipld::DagCbor)]
+#[derive(Debug, Eq, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct Payload {
- mime: MimeType,
- timestamp: DateTime,
- content: Vec<u8>,
-}
-
-impl Payload {
- pub fn new(mime: MimeType, timestamp: DateTime) -> Self {
- Payload { mime, timestamp, content: Vec::new() }
- }
-
- pub fn now_from_text(text: String) -> Payload {
- let mime = MimeType::from(mime::TEXT_PLAIN_UTF_8);
- let timestamp = DateTime::from(chrono::offset::Utc::now());
-
- Self::new(mime, timestamp).with_content(text.into_bytes())
- }
+ // TODO: Make this a mime::Mime, but as this type does not impl Serialize/Deserialize, we
+ // cannot do this trivially yet
+ mime: String,
- pub fn with_content(mut self, v: Vec<u8>) -> Self {
- self.content = v;
- self
- }
-
- pub fn with_mimetype(mut self, mime: MimeType) -> Self {
- self.mime = mime;
- self
- }
-
- pub fn with_timestamp(mut self, ts: DateTime) -> Self {
- self.timestamp = ts;
- self
- }
-
- pub fn content(&self) -> &Vec<u8> {
- &self.content
- }
+ timestamp: DateTime,
+ content: crate::cid::Cid,
}