diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2021-11-27 12:12:17 +0100 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2021-11-27 12:12:17 +0100 |
commit | d2fbb50aba8d88aa6a18e36e46905356cfeb30b9 (patch) | |
tree | 0d2c2c4c57f0aef20757ce9f8595c5a4c4eb1ff7 | |
parent | 3e3e89c78196410228f2d0fdfc6554a05c114779 (diff) |
Remove everything from datatypes which is unusable right now
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
-rw-r--r-- | Cargo.toml | 4 | ||||
-rw-r--r-- | src/cid.rs | 6 | ||||
-rw-r--r-- | src/types/datetime.rs | 23 | ||||
-rw-r--r-- | src/types/id.rs | 16 | ||||
-rw-r--r-- | src/types/mime.rs | 28 | ||||
-rw-r--r-- | src/types/mod.rs | 6 | ||||
-rw-r--r-- | src/types/node.rs | 25 | ||||
-rw-r--r-- | src/types/payload.rs | 42 |
8 files changed, 20 insertions, 130 deletions
@@ -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" @@ -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, } |