diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2020-04-28 21:01:26 +0200 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2020-04-28 21:22:54 +0200 |
commit | 542defd29c7d153a4268bba8a81319af0bb787c3 (patch) | |
tree | d0ad52781876903e91f2e2689a6367979d093a65 | |
parent | 1b5c13abf7b944f0f03a9bfd41b2ddf2afe14382 (diff) |
Simplify Repository interface by implementing dereferencing
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
-rw-r--r-- | src/repository/client.rs | 34 | ||||
-rw-r--r-- | src/repository/repository.rs | 26 |
2 files changed, 28 insertions, 32 deletions
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<IpfsClient>); +pub struct ClientFassade(Arc<IpfsClient>); impl ClientFassade { fn new(host: &str, port: u16) -> Result<ClientFassade, Error> { @@ -39,7 +39,7 @@ impl ClientFassade { .map_err(Into::into) } - async fn get<H: AsRef<IPFSHash>>(&self, hash: H) -> Result<Vec<u8>, Error> { + pub async fn get_raw_bytes<H: AsRef<IPFSHash>>(&self, hash: H) -> Result<Vec<u8>, Error> { debug!("Get: {}", hash.as_ref()); self.0 .clone() @@ -50,7 +50,7 @@ impl ClientFassade { .await } - async fn put(&self, data: Vec<u8>) -> Result<IPFSHash, Error> { + pub async fn put_raw_bytes(&self, data: Vec<u8>) -> Result<IPFSHash, Error> { 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<IPNSHash, Error> { + pub async fn publish(&self, key: &str, hash: &str) -> Result<IPNSHash, Error> { 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<TypedClientFassade, Error> { ClientFassade::new(host, port).map(TypedClientFassade) } - pub async fn get_raw_bytes<H>(&self, hash: H) -> Result<Vec<u8>, Error> - where H: AsRef<IPFSHash> - { - self.0.get(hash).await - } - - pub async fn get<H, D>(&self, hash: H) -> Result<D, Error> + pub async fn get_typed<H, D>(&self, hash: H) -> Result<D, Error> where H: AsRef<IPFSHash>, 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<S, Ser>(&self, data: &S) -> Result<IPFSHash, Error> + 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(data.into_bytes()).await - } - - pub async fn publish(&self, key: &str, hash: &str) -> Result<IPNSHash, Error> { - 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<Repository, Error> { TypedClientFassade::new(host, port).map(Repository) } - pub async fn get_raw_bytes<H>(&self, hash: H) -> Result<Vec<u8>, Error> - where H: AsRef<IPFSHash> - { - self.0.get_raw_bytes(hash).await - } - pub async fn get_block<H>(&self, hash: H) -> Result<Block, Error> where H: AsRef<IPFSHash> { - self.0.get(hash).await + 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(b.as_ref()).await + 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(hash).await + 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(c.as_ref()).await - } - - pub async fn publish(&self, key: &str, hash: &str) -> Result<IPNSHash, Error> { - self.0.publish(key, hash).await + self.0.put_typed(c.as_ref()).await } } |