diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2021-04-06 17:06:11 +0200 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2021-04-06 17:06:11 +0200 |
commit | fa655bbe8acae9c7f5d46e977ebea01ac0572327 (patch) | |
tree | ba1617c161214df4e589f931cf205ef383a3e52c /src/backend | |
parent | a70d760dd4d24d2d2963f6e48e3fec027f1e8305 (diff) |
Implement first CLI
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
Diffstat (limited to 'src/backend')
-rw-r--r-- | src/backend/backend.rs | 7 | ||||
-rw-r--r-- | src/backend/datetime.rs | 7 | ||||
-rw-r--r-- | src/backend/mime.rs | 6 | ||||
-rw-r--r-- | src/backend/node.rs | 1 | ||||
-rw-r--r-- | src/backend/payload.rs | 28 |
5 files changed, 49 insertions, 0 deletions
diff --git a/src/backend/backend.rs b/src/backend/backend.rs index 3e00f0f..33a8906 100644 --- a/src/backend/backend.rs +++ b/src/backend/backend.rs @@ -43,6 +43,13 @@ impl IpfsEmbedBackend { ipfs_embed::Ipfs::new(config).await.map(Arc::new).map(|ipfs| IpfsEmbedBackend { ipfs }) } + pub async fn new_with_config(cfg: ipfs_embed::Config) -> Result<Self> { + ipfs_embed::Ipfs::new(cfg) + .await + .map(Arc::new) + .map(|ipfs| IpfsEmbedBackend { ipfs }) + } + pub async fn write_payload(&self, payload: &crate::backend::Payload) -> Result<cid::Cid> { let block = libipld::block::Block::encode(libipld::cbor::DagCborCodec, libipld::multihash::Code::Blake3_256, &payload)?; self.ipfs diff --git a/src/backend/datetime.rs b/src/backend/datetime.rs index 650a159..ec99282 100644 --- a/src/backend/datetime.rs +++ b/src/backend/datetime.rs @@ -3,6 +3,13 @@ use anyhow::Error; #[derive(Debug, Eq, PartialEq)] pub struct DateTime(chrono::DateTime<chrono::Utc>); +impl From<chrono::DateTime<chrono::Utc>> for DateTime { + fn from(dt: chrono::DateTime<chrono::Utc>) -> Self { + DateTime(dt) + } +} + + 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) diff --git a/src/backend/mime.rs b/src/backend/mime.rs index d65761e..9ea1cb8 100644 --- a/src/backend/mime.rs +++ b/src/backend/mime.rs @@ -3,6 +3,12 @@ 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) diff --git a/src/backend/node.rs b/src/backend/node.rs index 800d503..a068405 100644 --- a/src/backend/node.rs +++ b/src/backend/node.rs @@ -33,4 +33,5 @@ impl Node { payload } } + } diff --git a/src/backend/payload.rs b/src/backend/payload.rs index fcddb50..856fcda 100644 --- a/src/backend/payload.rs +++ b/src/backend/payload.rs @@ -8,3 +8,31 @@ pub struct Payload { 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.as_bytes().to_vec()) + } + + 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 + } +} + |