From 542defd29c7d153a4268bba8a81319af0bb787c3 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Tue, 28 Apr 2020 21:01:26 +0200 Subject: Simplify Repository interface by implementing dereferencing Signed-off-by: Matthias Beyer --- src/repository/client.rs | 34 ++++++++++++++++------------------ src/repository/repository.rs | 26 ++++++++++++-------------- 2 files changed, 28 insertions(+), 32 deletions(-) (limited to 'src') diff --git a/src/repository/client.rs b/src/repository/client.rs index 3cd023d..0ec42da 100644 --- a/src/repository/client.rs +++ b/src/repository/client.rs @@ -28,7 +28,7 @@ use crate::types::util::IPNSHash; /// /// Abstracts the procedural interface of IpfsClient calls. #[derive(Clone)] -struct ClientFassade(Arc); +pub struct ClientFassade(Arc); impl ClientFassade { fn new(host: &str, port: u16) -> Result { @@ -39,7 +39,7 @@ impl ClientFassade { .map_err(Into::into) } - async fn get>(&self, hash: H) -> Result, Error> { + pub async fn get_raw_bytes>(&self, hash: H) -> Result, Error> { debug!("Get: {}", hash.as_ref()); self.0 .clone() @@ -50,7 +50,7 @@ impl ClientFassade { .await } - async fn put(&self, data: Vec) -> Result { + pub async fn put_raw_bytes(&self, data: Vec) -> Result { debug!("Put: {:?}", data); self.0 .clone() @@ -60,7 +60,7 @@ impl ClientFassade { .map_err(Into::into) } - async fn publish(&self, key: &str, hash: &str) -> Result { + pub async fn publish(&self, key: &str, hash: &str) -> Result { debug!("Publish: {:?} -> {:?}", key, hash); self.0 .clone() @@ -75,24 +75,26 @@ impl ClientFassade { #[derive(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 { ClientFassade::new(host, port).map(TypedClientFassade) } - pub async fn get_raw_bytes(&self, hash: H) -> Result, Error> - where H: AsRef - { - self.0.get(hash).await - } - - pub async fn get(&self, hash: H) -> Result + pub async fn get_typed(&self, hash: H) -> Result where H: AsRef, D: DeserializeOwned { self.0 .clone() - .get(hash) + .get_raw_bytes(hash) .await .and_then(|data| { debug!("Got data, building object: {:?}", data); @@ -101,18 +103,14 @@ impl TypedClientFassade { }) } - pub async fn put(&self, data: &S) -> Result + pub async fn put_typed(&self, data: &S) -> Result where S: AsRef, Ser: Serialize { let client = self.0.clone(); let data = serde_json_to_str(data.as_ref())?; - client.put(data.into_bytes()).await - } - - pub async fn publish(&self, key: &str, hash: &str) -> Result { - self.0.publish(key, hash).await + client.put_raw_bytes(data.into_bytes()).await } } diff --git a/src/repository/repository.rs b/src/repository/repository.rs index 35c83a2..85dca8d 100644 --- a/src/repository/repository.rs +++ b/src/repository/repository.rs @@ -31,43 +31,41 @@ use crate::repository::client::TypedClientFassade; #[derive(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 { TypedClientFassade::new(host, port).map(Repository) } - pub async fn get_raw_bytes(&self, hash: H) -> Result, Error> - where H: AsRef - { - self.0.get_raw_bytes(hash).await - } - pub async fn get_block(&self, hash: H) -> Result where H: AsRef { - self.0.get(hash).await + self.0.get_typed(hash).await } pub async fn put_block(&self, b: B) -> Result where B: AsRef { - self.0.put(b.as_ref()).await + self.0.put_typed(b.as_ref()).await } pub async fn get_content(&self, hash: H) -> Result where H: AsRef { - self.0.get(hash).await + self.0.get_typed(hash).await } pub async fn put_content(&self, c: C) -> Result where C: AsRef { - self.0.put(c.as_ref()).await - } - - pub async fn publish(&self, key: &str, hash: &str) -> Result { - self.0.publish(key, hash).await + self.0.put_typed(c.as_ref()).await } } -- cgit v1.2.3