summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Beyer <mail@beyermatthias.de>2021-11-27 11:28:15 +0100
committerMatthias Beyer <mail@beyermatthias.de>2021-11-27 11:30:25 +0100
commit3442920b3f5b53fb04ceb82d12506249a746480e (patch)
treef3f08eb0c40f997c53f8a1f855aa912537f961a4
parent874e9008b25e3183b5b9a9cf697ffd4f1f49af05 (diff)
Introduce own Cid type until ipfs_api API is not stringly typed anymore
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
-rw-r--r--Cargo.toml2
-rw-r--r--src/cid.rs48
-rw-r--r--src/client.rs31
-rw-r--r--src/ipfs_client.rs4
-rw-r--r--src/main.rs1
5 files changed, 76 insertions, 10 deletions
diff --git a/Cargo.toml b/Cargo.toml
index 91ed946..7a23d4b 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -26,7 +26,7 @@ env_logger = "0.8"
futures = "0.3"
ipfs-api-backend-hyper = { version = "0.2", features = [ "with-hyper-tls" ] }
log = "0.4"
-tokio = { version = "1", features = ["full"] }
+tokio = { version = "1", features = ["full", "rt", "macros"] }
mime = "0.3"
libipld-cbor = "0.12"
libipld = "0.12"
diff --git a/src/cid.rs b/src/cid.rs
new file mode 100644
index 0000000..5a137df
--- /dev/null
+++ b/src/cid.rs
@@ -0,0 +1,48 @@
+use anyhow::Result;
+
+/// Our own CID type
+///
+/// Right now the ipfs_api crate does not use a CID type in its interface... hence we would need to
+/// convert back-and-forth between String and cid::Cid,... but that's tedious.
+///
+/// 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)]
+pub struct Cid(String);
+
+#[cfg(test)]
+impl AsRef<str> for Cid {
+ fn as_ref(&self) -> &str {
+ self.0.as_ref()
+ }
+}
+
+pub trait TryToCid {
+ fn try_to_cid(self) -> Result<Cid>;
+}
+
+impl TryToCid for ipfs_api_backend_hyper::response::AddResponse {
+ fn try_to_cid(self) -> Result<Cid> {
+ log::debug!("Transforming to CID => {:?}", self);
+ string_to_cid(self.hash)
+ }
+}
+
+/// Helper function that can be tested
+///
+/// Converts a String to a Cid
+fn string_to_cid(s: String) -> Result<Cid> {
+ Ok(Cid(s))
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn test_string_to_cid() {
+ let s = String::from("QmY2T5EfgLn8qWCt8eus6VX1gJuAp1nmUSdmoehgMxznAf");
+ let r = string_to_cid(s);
+ assert!(r.is_ok(), "Not OK = {:?}", r);
+ }
+}
diff --git a/src/client.rs b/src/client.rs
index e89268a..edc53c4 100644
--- a/src/client.rs
+++ b/src/client.rs
@@ -1,6 +1,12 @@
+use std::io::Cursor;
+
use anyhow::Result;
-use cid::Cid;
+use futures::FutureExt;
+use futures::TryFutureExt;
+use ipfs_api_backend_hyper::IpfsApi;
+use crate::cid::Cid;
+use crate::cid::TryToCid;
use crate::config::Config;
use crate::ipfs_client::IpfsClient;
@@ -22,15 +28,22 @@ impl Client {
config
}
}
- fn post_text_blob_impl(&self, text: String) -> Result<Cid> {
- unimplemented!()
+
+ async fn post_text_blob_impl(&self, text: String) -> Result<Cid> {
+ let reader = Cursor::new(text);
+
+ self.ipfs
+ .add(reader)
+ .await
+ .map_err(anyhow::Error::from)
+ .and_then(crate::ipfs_client::backend::response::AddResponse::try_to_cid)
}
}
#[cfg(test)]
impl Client {
- pub fn post_text_blob(&self, text: String) -> Result<Cid> {
- self.post_text_blob_impl(text)
+ pub async fn post_text_blob(&self, text: String) -> Result<Cid> {
+ self.post_text_blob_impl(text).await
}
}
@@ -41,14 +54,16 @@ mod tests {
use crate::config::Config;
use crate::ipfs_client::IpfsClient;
- #[test]
- fn test_post_text_blob() {
+ #[tokio::test]
+ async fn test_post_text_blob() {
+ let _ = env_logger::try_init();
let ipfs = IpfsClient::from_str("http://localhost:5001").unwrap();
let config = Config::default();
let client = Client::new(ipfs, config);
- let cid = client.post_text_blob(String::from("text"));
+ let cid = client.post_text_blob(String::from("text")).await;
assert!(cid.is_ok());
+ assert_eq!(cid.unwrap().as_ref(), "QmY2T5EfgLn8qWCt8eus6VX1gJuAp1nmUSdmoehgMxznAf");
}
}
diff --git a/src/ipfs_client.rs b/src/ipfs_client.rs
index 1bf014f..13d8bde 100644
--- a/src/ipfs_client.rs
+++ b/src/ipfs_client.rs
@@ -1,4 +1,6 @@
-pub type IpfsClient = ipfs_api_backend_hyper::IpfsClient;
+pub use ipfs_api_backend_hyper as backend;
+pub type IpfsClient = backend::IpfsClient;
+
#[cfg(test)]
mod tests {
diff --git a/src/main.rs b/src/main.rs
index 73e30e1..a672ae0 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -2,6 +2,7 @@ use anyhow::Result;
extern crate clap_v3 as clap;
+pub mod cid;
pub mod cli;
pub mod client;
pub mod config;