diff options
Diffstat (limited to 'src/repository')
-rw-r--r-- | src/repository/client.rs | 133 | ||||
-rw-r--r-- | src/repository/mod.rs | 2 | ||||
-rw-r--r-- | src/repository/repository.rs | 71 |
3 files changed, 0 insertions, 206 deletions
diff --git a/src/repository/client.rs b/src/repository/client.rs deleted file mode 100644 index 4466e05..0000000 --- a/src/repository/client.rs +++ /dev/null @@ -1,133 +0,0 @@ -use std::io::Cursor; -use std::sync::Arc; -use std::ops::Deref; -use std::result::Result as RResult; - -use ipfs_api::IpfsClient; -use ipfs_api::TryFromUri; -use anyhow::Error; -use futures::future::Future; -use futures::future::FutureExt; -use futures::stream::Stream; -use futures::stream::StreamExt; -use futures::stream::TryStreamExt; -use failure::Fail; - -use serde_json::from_str as serde_json_from_str; -use serde_json::to_string as serde_json_to_str; -use serde::Serialize; -use serde::de::DeserializeOwned; -use chrono::NaiveDateTime; - -use crate::types::block::Block; -use crate::types::content::Content; -use crate::types::payload::Payload; -use crate::types::util::IPFSHash; -use crate::types::util::IPNSHash; - - -/// Internal ClientFassade types -/// -/// Abstracts the procedural interface of IpfsClient calls. -#[derive(Clone)] -pub struct ClientFassade(Arc<IpfsClient>); - -impl std::fmt::Debug for ClientFassade { - fn fmt(&self, f: &mut std::fmt::Formatter) -> RResult<(), std::fmt::Error> { - write!(f, "ClientFassade") - } -} - -impl ClientFassade { - fn new(host: &str, port: u16) -> Result<ClientFassade, Error> { - debug!("Creating new ClientFassade object: {}:{}", host, port); - IpfsClient::from_str(&format!("{}:{}", host, port)) - .map(Arc::new) - .map(|c| ClientFassade(c)) - .map_err(|e| Error::from(e.compat())) - } - - pub async fn get_raw_bytes<H: AsRef<IPFSHash>>(&self, hash: H) -> Result<Vec<u8>, Error> { - debug!("Get: {}", hash.as_ref()); - self.0 - .clone() - .cat(hash.as_ref()) - .map_ok(|b| b.to_vec()) - .try_concat() - .map(|r| r.map_err(|e| anyhow!("UNIMPLEMENTED!()"))) - .await - } - - pub async fn put_raw_bytes(&self, data: Vec<u8>) -> Result<IPFSHash, Error> { - debug!("Put: {:?}", data); - self.0 - .clone() - .add(Cursor::new(data)) - .await - .map(|res| IPFSHash::from(res.hash)) - .map_err(|e| anyhow!("UNIMPLEMENTED!()")) - } - - pub async fn publish(&self, key: &str, hash: &str) -> Result<IPNSHash, Error> { - debug!("Publish: {:?} -> {:?}", key, hash); - self.0 - .clone() - .name_publish(hash, false, None, None, Some(key)) - .await - .map(|res| IPNSHash::from(res.value)) - .map_err(|e| anyhow!("UNIMPLEMENTED!()")) - } - - pub async fn resolve(&self, ipns: IPNSHash) -> Result<IPFSHash, Error> { - self.0 - .clone() - .name_resolve(Some(&ipns), true, false) - .await - .map(|res| IPFSHash::from(res.path)) - .map_err(|e| anyhow!("UNIMPLEMENTED!()")) - } -} - -/// Client wrapper for working with types directly on the client -#[derive(Debug, Clone)] -pub struct TypedClientFassade(ClientFassade); - -impl Deref for TypedClientFassade { - type Target = ClientFassade; - - fn deref(&self) -> &Self::Target { - &self.0 - } -} - -impl TypedClientFassade { - pub fn new(host: &str, port: u16) -> Result<TypedClientFassade, Error> { - ClientFassade::new(host, port).map(TypedClientFassade) - } - - pub async fn get_typed<H, D>(&self, hash: H) -> Result<D, Error> - where H: AsRef<IPFSHash>, - D: DeserializeOwned - { - self.0 - .clone() - .get_raw_bytes(hash) - .await - .and_then(|data| { - debug!("Got data, building object: {:?}", data); - - serde_json::from_slice(&data).map_err(|e| Error::from(e.compat())) - }) - } - - pub async fn put_typed<S, Ser>(&self, data: &S) -> Result<IPFSHash, Error> - where S: AsRef<Ser>, - Ser: Serialize - { - let client = self.0.clone(); - - let data = serde_json_to_str(data.as_ref())?; - client.put_raw_bytes(data.into_bytes()).await - } - -} diff --git a/src/repository/mod.rs b/src/repository/mod.rs deleted file mode 100644 index dc00895..0000000 --- a/src/repository/mod.rs +++ /dev/null @@ -1,2 +0,0 @@ -pub mod client; -pub mod repository; diff --git a/src/repository/repository.rs b/src/repository/repository.rs deleted file mode 100644 index cc940c6..0000000 --- a/src/repository/repository.rs +++ /dev/null @@ -1,71 +0,0 @@ -use std::io::Cursor; -use std::sync::Arc; -use std::ops::Deref; - -use ipfs_api::IpfsClient; -use anyhow::Error; -use futures::future::Future; -use futures::stream::Stream; - -use serde_json::from_str as serde_json_from_str; -use serde_json::to_string as serde_json_to_str; -use serde::Serialize; -use serde::de::DeserializeOwned; -use chrono::NaiveDateTime; - -use crate::types::block::Block; -use crate::types::content::Content; -use crate::types::payload::Payload; -use crate::types::util::IPFSHash; -use crate::types::util::IPNSHash; -use crate::repository::client::TypedClientFassade; - - -/// High-level Client abstraction -/// -/// Still a low-level interface though, because we're still operating on the repository directly. -/// -/// Should not be used too extensively, but rather through the "Profile" type, which represents the -/// profile of a user. -#[derive(Debug, Clone)] -pub struct Repository(TypedClientFassade); - -impl Deref for Repository { - type Target = TypedClientFassade; - - fn deref(&self) -> &Self::Target { - &self.0 - } -} - -impl Repository { - pub fn new(host: &str, port: u16) -> Result<Repository, Error> { - TypedClientFassade::new(host, port).map(Repository) - } - - pub async fn get_block<H>(&self, hash: H) -> Result<Block, Error> - where H: AsRef<IPFSHash> - { - self.0.get_typed(hash).await - } - - pub async fn put_block<B>(&self, b: B) -> Result<IPFSHash, Error> - where B: AsRef<Block> - { - self.0.put_typed(b.as_ref()).await - } - - pub async fn get_content<H>(&self, hash: H) -> Result<Content, Error> - where H: AsRef<IPFSHash> - { - self.0.get_typed(hash).await - } - - pub async fn put_content<C>(&self, c: C) -> Result<IPFSHash, Error> - where C: AsRef<Content> - { - self.0.put_typed(c.as_ref()).await - } - -} - |