summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Beyer <mail@beyermatthias.de>2020-05-20 11:20:12 +0200
committerMatthias Beyer <mail@beyermatthias.de>2020-05-20 11:20:26 +0200
commit685e35d302873d3047ef86dd8904e769202d23cf (patch)
tree21db7d442201704bfd146327f11e0411ef2f5b95
parentc44d69dcde20d6b200f7c2782a4ebbb848e2ed1f (diff)
downloaddistrox-685e35d302873d3047ef86dd8904e769202d23cf.tar.gz
distrox-685e35d302873d3047ef86dd8904e769202d23cf.tar.xz
Unite ipfs-api client abstraction into one "Model" type
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
-rw-r--r--src/app.rs8
-rw-r--r--src/main.rs4
-rw-r--r--src/model.rs (renamed from src/repository/client.rs)114
-rw-r--r--src/repository/mod.rs2
-rw-r--r--src/repository/repository.rs71
-rw-r--r--src/types/block.rs4
-rw-r--r--src/types/content.rs4
-rw-r--r--src/types/payload.rs4
8 files changed, 78 insertions, 133 deletions
diff --git a/src/app.rs b/src/app.rs
index b31839f..d356dca 100644
--- a/src/app.rs
+++ b/src/app.rs
@@ -8,7 +8,7 @@ use crate::types::util::IPFSHash;
use crate::types::util::MimeType;
use crate::types::util::IPNSHash;
use crate::types::block::Block;
-use crate::repository::repository::Repository;
+use crate::model::Model;
use crate::types::content::Content;
use crate::types::payload::Payload;
use crate::types::util::Timestamp;
@@ -16,7 +16,7 @@ use crate::version::protocol_version;
#[derive(Debug, Clone)]
pub struct App {
- repo: Repository,
+ repo: Model,
device_name: IPNSHash,
publishing_key: String
}
@@ -24,10 +24,10 @@ pub struct App {
impl App {
pub fn load(device_name: IPNSHash, publishing_key: String, host: &str, port: u16) -> Result<Self, Error> {
- Repository::new(host, port).map(|repo| App { repo, device_name, publishing_key })
+ Model::new(host, port).map(|repo| App { repo, device_name, publishing_key })
}
- pub async fn new_profile(repo: Repository, publishing_key: &str, names: Vec<String>) -> Result<Self, Error> {
+ pub async fn new_profile(repo: Model, publishing_key: &str, names: Vec<String>) -> Result<Self, Error> {
let payload = Payload::Profile {
names,
picture: None,
diff --git a/src/main.rs b/src/main.rs
index b624bd8..0e91afd 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -33,7 +33,7 @@ mod app;
mod cli;
mod configuration;
mod gui;
-mod repository;
+mod model;
mod server;
mod types;
mod version;
@@ -57,7 +57,7 @@ use env_logger::Env;
use crate::app::App;
use crate::cli::*;
use crate::configuration::Configuration;
-use crate::repository::repository::Repository;
+use crate::model::Model;
use crate::types::block::Block;
use crate::types::content::Content;
use crate::types::payload::Payload;
diff --git a/src/repository/client.rs b/src/model.rs
index 4466e05..b81ea17 100644
--- a/src/repository/client.rs
+++ b/src/model.rs
@@ -1,23 +1,24 @@
+//! The "model" module implements the Database-layer of the application
+
use std::io::Cursor;
-use std::sync::Arc;
use std::ops::Deref;
use std::result::Result as RResult;
+use std::sync::Arc;
-use ipfs_api::IpfsClient;
-use ipfs_api::TryFromUri;
use anyhow::Error;
+use chrono::NaiveDateTime;
+use failure::Fail;
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 ipfs_api::IpfsClient;
+use ipfs_api::TryFromUri;
use serde::Serialize;
use serde::de::DeserializeOwned;
-use chrono::NaiveDateTime;
+use serde_json::from_str as serde_json_from_str;
+use serde_json::to_string as serde_json_to_str;
use crate::types::block::Block;
use crate::types::content::Content;
@@ -25,29 +26,32 @@ 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>);
+pub struct Model(Arc<IpfsClient>);
-impl std::fmt::Debug for ClientFassade {
+impl std::fmt::Debug for Model{
fn fmt(&self, f: &mut std::fmt::Formatter) -> RResult<(), std::fmt::Error> {
- write!(f, "ClientFassade")
+ write!(f, "Model")
}
}
-impl ClientFassade {
- fn new(host: &str, port: u16) -> Result<ClientFassade, Error> {
- debug!("Creating new ClientFassade object: {}:{}", host, port);
+impl Model {
+ pub fn new(host: &str, port: u16) -> Result<Model, Error> {
+ debug!("Creating new Model object: {}:{}", host, port);
IpfsClient::from_str(&format!("{}:{}", host, port))
.map(Arc::new)
- .map(|c| ClientFassade(c))
+ .map(|c| Model(c))
.map_err(|e| Error::from(e.compat()))
}
- pub async fn get_raw_bytes<H: AsRef<IPFSHash>>(&self, hash: H) -> Result<Vec<u8>, Error> {
+
+ //
+ //
+ // Low-level interface to the ipfs-api
+ //
+ //
+
+ pub(crate) async fn get_raw_bytes<H: AsRef<IPFSHash>>(&self, hash: H) -> Result<Vec<u8>, Error> {
debug!("Get: {}", hash.as_ref());
self.0
.clone()
@@ -58,7 +62,7 @@ impl ClientFassade {
.await
}
- pub async fn put_raw_bytes(&self, data: Vec<u8>) -> Result<IPFSHash, Error> {
+ pub(crate) async fn put_raw_bytes(&self, data: Vec<u8>) -> Result<IPFSHash, Error> {
debug!("Put: {:?}", data);
self.0
.clone()
@@ -68,7 +72,7 @@ impl ClientFassade {
.map_err(|e| anyhow!("UNIMPLEMENTED!()"))
}
- pub async fn publish(&self, key: &str, hash: &str) -> Result<IPNSHash, Error> {
+ pub(crate) async fn publish(&self, key: &str, hash: &str) -> Result<IPNSHash, Error> {
debug!("Publish: {:?} -> {:?}", key, hash);
self.0
.clone()
@@ -78,7 +82,7 @@ impl ClientFassade {
.map_err(|e| anyhow!("UNIMPLEMENTED!()"))
}
- pub async fn resolve(&self, ipns: IPNSHash) -> Result<IPFSHash, Error> {
+ pub(crate) async fn resolve(&self, ipns: IPNSHash) -> Result<IPFSHash, Error> {
self.0
.clone()
.name_resolve(Some(&ipns), true, false)
@@ -86,32 +90,18 @@ impl ClientFassade {
.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)
- }
+ //
+ //
+ // Generic typed interface
+ //
+ //
- pub async fn get_typed<H, D>(&self, hash: H) -> Result<D, Error>
+ pub(crate) 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)
+ self.get_raw_bytes(hash)
.await
.and_then(|data| {
debug!("Got data, building object: {:?}", data);
@@ -120,14 +110,42 @@ impl TypedClientFassade {
})
}
- pub async fn put_typed<S, Ser>(&self, data: &S) -> Result<IPFSHash, Error>
+ pub(crate) 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
+ self.put_raw_bytes(data.into_bytes()).await
+ }
+
+ //
+ //
+ // Typed interface
+ //
+ //
+
+ pub async fn get_block<H>(&self, hash: H) -> Result<Block, Error>
+ where H: AsRef<IPFSHash>
+ {
+ self.get_typed(hash).await
+ }
+
+ pub async fn put_block<B>(&self, b: B) -> Result<IPFSHash, Error>
+ where B: AsRef<Block>
+ {
+ self.put_typed(b.as_ref()).await
+ }
+
+ pub async fn get_content<H>(&self, hash: H) -> Result<Content, Error>
+ where H: AsRef<IPFSHash>
+ {
+ self.get_typed(hash).await
+ }
+
+ pub async fn put_content<C>(&self, c: C) -> Result<IPFSHash, Error>
+ where C: AsRef<Content>
+ {
+ self.put_typed(c.as_ref()).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
- }
-
-}
-
diff --git a/src/types/block.rs b/src/types/block.rs
index 1927d55..c70ceef 100644
--- a/src/types/block.rs
+++ b/src/types/block.rs
@@ -3,7 +3,7 @@ use crate::types::util::Version;
use crate::types::content::Content;
use crate::types::payload::*;
use crate::types::content::LoadedContent;
-use crate::repository::repository::Repository;
+use crate::model::Model;
use anyhow::Error;
@@ -42,7 +42,7 @@ impl Block {
&self.content
}
- pub async fn load(self, r: &Repository) -> Result<LoadedBlock, Error> {
+ pub async fn load(self, r: &Model) -> Result<LoadedBlock, Error> {
Ok({
LoadedBlock {
version: self.version,
diff --git a/src/types/content.rs b/src/types/content.rs
index 4812abd..8b6eb4d 100644
--- a/src/types/content.rs
+++ b/src/types/content.rs
@@ -8,7 +8,7 @@ use crate::types::util::MimeType;
use crate::types::util::Timestamp;
use crate::types::payload::Payload;
use crate::types::payload::LoadedPayload;
-use crate::repository::repository::Repository;
+use crate::model::Model;
#[derive(Serialize, Deserialize, Debug)]
pub struct Content {
@@ -63,7 +63,7 @@ impl Content {
self.devices.push(dev);
}
- pub async fn load(self, r: &Repository) -> Result<LoadedContent, Error> {
+ pub async fn load(self, r: &Model) -> Result<LoadedContent, Error> {
Ok({
LoadedContent {
devices: self.devices,
diff --git a/src/types/payload.rs b/src/types/payload.rs
index 2d85c22..47f28a6 100644
--- a/src/types/payload.rs
+++ b/src/types/payload.rs
@@ -6,7 +6,7 @@ use crate::types::util::IPFSHash;
use crate::types::util::IPNSHash;
use crate::types::util::MimeType;
use crate::types::util::Timestamp;
-use crate::repository::repository::Repository;
+use crate::model::Model;
/// The Payload type represents the Payload of a Content object
@@ -166,7 +166,7 @@ impl Payload {
}
}
- pub async fn load(self, r: &Repository) -> Result<LoadedPayload, Error> {
+ pub async fn load(self, r: &Model) -> Result<LoadedPayload, Error> {
match self {
Payload::None => Ok(LoadedPayload::None),
Payload::Post {