diff options
-rw-r--r-- | src/main.rs | 4 | ||||
-rw-r--r-- | src/middleware.rs | 8 | ||||
-rw-r--r-- | src/model.rs (renamed from src/repository/client.rs) | 114 | ||||
-rw-r--r-- | src/profile.rs | 6 | ||||
-rw-r--r-- | src/repository/mod.rs | 2 | ||||
-rw-r--r-- | src/repository/repository.rs | 71 | ||||
-rw-r--r-- | src/types/block.rs | 4 | ||||
-rw-r--r-- | src/types/content.rs | 4 | ||||
-rw-r--r-- | src/types/payload.rs | 4 |
9 files changed, 78 insertions, 139 deletions
diff --git a/src/main.rs b/src/main.rs index 8cc2a8f..80e6ec9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -32,8 +32,8 @@ extern crate pidlock; mod cli; mod configuration; mod gui; +mod model; mod middleware; -mod repository; mod server; mod types; mod version; @@ -56,8 +56,8 @@ use env_logger::Env; use crate::cli::*; use crate::configuration::Configuration; +use crate::model::Model; use crate::middleware::Middleware; -use crate::repository::repository::Repository; use crate::types::block::Block; use crate::types::content::Content; use crate::types::payload::Payload; diff --git a/src/middleware.rs b/src/middleware.rs index 37ea7a2..61fe716 100644 --- a/src/middleware.rs +++ b/src/middleware.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; @@ -17,7 +17,7 @@ use crate::version::protocol_version; /// The "Middleware" type implements the actual application logic #[derive(Debug, Clone)] pub struct Middleware { - repo: Repository, + repo: Model, device_name: IPNSHash, publishing_key: String } @@ -25,10 +25,10 @@ pub struct Middleware { impl Middleware { pub fn load(device_name: IPNSHash, publishing_key: String, host: &str, port: u16) -> Result<Self, Error> { - Repository::new(host, port).map(|repo| Middleware { repo, device_name, publishing_key }) + Model::new(host, port).map(|repo| Middleware { 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/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/profile.rs b/src/profile.rs deleted file mode 100644 index 870db08..0000000 --- a/src/profile.rs +++ /dev/null @@ -1,6 +0,0 @@ - -#[derive(Serialize, Deserialize, Debug)] -struct Profile { - device_name: String, - other_devices: Vec<String>, -} 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 { |