From 532088e26d5abca89cec53f4340a4ff3f1380837 Mon Sep 17 00:00:00 2001 From: Victor Roest Date: Mon, 3 Aug 2020 12:27:21 +0200 Subject: [PATCH] Fixed fmt and clippy --- src/dspfs/api.rs | 36 ++++++++------- src/dspfs/mod.rs | 101 ++++++++++++++++++++++++++---------------- src/fs/file.rs | 3 +- src/fs/group/heed.rs | 7 +-- src/fs/group/mod.rs | 34 +++++++------- src/fs/group/store.rs | 2 +- src/main.rs | 2 - src/user/mod.rs | 4 +- 8 files changed, 109 insertions(+), 80 deletions(-) diff --git a/src/dspfs/api.rs b/src/dspfs/api.rs index d25a9c6..3103a2f 100644 --- a/src/dspfs/api.rs +++ b/src/dspfs/api.rs @@ -1,18 +1,18 @@ -use crate::user::PublicUser; use crate::fs::hash::Hash; +use crate::user::PublicUser; -use std::path::Path; -use uuid::Uuid; -use anyhow::Result; use crate::fs::file::SimpleFile; +use anyhow::Result; use async_trait::async_trait; -use std::collections::{HashSet, BTreeSet}; -use std::time::SystemTime; -use std::fs::DirEntry; +use serde::{Deserialize, Serialize}; +use std::collections::{BTreeSet, HashSet}; use std::ffi::OsString; -use serde::{Serialize,Deserialize}; +use std::fs::DirEntry; +use std::path::Path; +use std::time::SystemTime; +use uuid::Uuid; -#[derive(Serialize,Deserialize,Debug,Clone, Eq, PartialEq, Hash)] +#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq, Hash)] pub struct LocalFile { name: OsString, modtime: SystemTime, @@ -37,7 +37,7 @@ impl LocalFile { pub trait Api { /// Equivalent of `git init`. Creates a new group with only you in it. async fn init_group(&self, path: &Path) -> Result; - + /// Equivalent of `git add`. Adds a file to the group and makes it visible for others in the group. async fn add_file(&self, guuid: Uuid, path: &Path) -> Result; @@ -54,13 +54,20 @@ pub trait Api { async fn get_available_files(&self, guuid: Uuid, path: &Path) -> Result>; /// gets a certain level of filetree as seen by another user - async fn get_files(&self, guuid: Uuid, user: &PublicUser, path: &Path) -> Result>; + async fn get_files( + &self, + guuid: Uuid, + user: &PublicUser, + path: &Path, + ) -> Result>; /// requests a file from other users in the group async fn request_file(&self, hash: Hash, to: &Path) -> Result<()>; /// Lists current download/uploads. - async fn status(&self) {todo!()} + async fn status(&self) { + todo!() + } /// Refreshes internal state. /// may do any of the following things: @@ -69,12 +76,9 @@ pub trait Api { /// * Check for new files from other users. async fn refresh(&self); - // TODO: - async fn add_folder(&self, guuid: Uuid, path: &Path) -> Result<()> { + async fn add_folder(&self, _guuid: Uuid, path: &Path) -> Result<()> { assert!(path.is_dir()); todo!() } } - - diff --git a/src/dspfs/mod.rs b/src/dspfs/mod.rs index 00dbfd4..968e45a 100644 --- a/src/dspfs/mod.rs +++ b/src/dspfs/mod.rs @@ -1,27 +1,27 @@ use crate::dspfs::builder::DspfsBuilder; use crate::dspfs::client::Client; use crate::dspfs::server::{Server, ServerHandle}; +use crate::fs::file::File; +use crate::fs::file::SimpleFile; use crate::fs::group::StoredGroup; +use crate::fs::hash::Hash; use crate::global_store::{SharedStore, Store}; use crate::user::{PrivateUser, PublicUser}; -use anyhow::{Result, Context}; +use anyhow::{Context, Result}; +use api::Api; +use api::LocalFile; +use async_trait::async_trait; use log::*; -use std::collections::{HashMap, HashSet, BTreeSet}; +use std::collections::{BTreeSet, HashMap, HashSet}; use std::fs; use std::mem; use std::path::Path; use uuid::Uuid; -use crate::fs::file::SimpleFile; -use crate::fs::file::File; -use crate::fs::hash::Hash; -use async_trait::async_trait; -use api::LocalFile; -use api::Api; +pub mod api; pub mod builder; pub mod client; pub mod server; -pub mod api; pub struct Dspfs { pub(self) store: SharedStore, @@ -63,7 +63,6 @@ impl Dspfs { } } - #[async_trait] impl Api for Dspfs { async fn init_group(&self, path: &Path) -> Result { @@ -72,7 +71,7 @@ impl Api for Dspfs { if !group.dspfs_folder().exists() { // New folder fs::create_dir_all(group.dspfs_folder())?; - let uuid = group.uuid.clone(); + let uuid = group.uuid; self.store.write().await.add_group(group)?; Ok(uuid) @@ -82,8 +81,13 @@ impl Api for Dspfs { } async fn add_file(&self, guuid: Uuid, path: &Path) -> Result { - let mut group = self.store.read().await.get_group(guuid)? - .context("There is no group with this uuid.")?.reload(self.store.clone())?; + let mut group = self + .store + .read() + .await + .get_group(guuid)? + .context("There is no group with this uuid.")? + .reload(self.store.clone())?; let us = self.store.read().await.get_self_user()?.context("")?; @@ -91,66 +95,91 @@ impl Api for Dspfs { location.push(path); if !location.is_file() { - return Err(anyhow::anyhow!("Path does not point to a file")) + return Err(anyhow::anyhow!("Path does not point to a file")); } - let file = File::new(location).await - .context("Indexing file failed")?; + let file = File::new(location).await.context("Indexing file failed")?; let simple_file = file.simplify(); - group.add_file(&us, file).await.context("Adding file to group failed")?; + group + .add_file(&us, file) + .await + .context("Adding file to group failed")?; Ok(simple_file) } - async fn join_group(&self, guuid: Uuid, bootstrap_user: &PublicUser) -> Result<()> { + async fn join_group(&self, _guuid: Uuid, _bootstrap_user: &PublicUser) -> Result<()> { unimplemented!("procrastination is life") } async fn list_files(&self, guuid: Uuid) -> Result> { - let group = self.store.read().await.get_group(guuid)? - .context("There is no group with this uuid.")?.reload(self.store.clone())?; + let group = self + .store + .read() + .await + .get_group(guuid)? + .context("There is no group with this uuid.")? + .reload(self.store.clone())?; - let f = group.list_files().await? - .into_iter() - .map(|f| f.simplify()); + let f = group.list_files().await?.into_iter().map(|f| f.simplify()); Ok(f.collect()) } async fn get_users(&self, guuid: Uuid) -> Result> { - let group = self.store.read().await.get_group(guuid)? + let group = self + .store + .read() + .await + .get_group(guuid)? .context("There is no group with this uuid.")?; Ok(group.users) } async fn get_available_files(&self, guuid: Uuid, path: &Path) -> Result> { - let group = self.store.read().await.get_group(guuid)? + let group = self + .store + .read() + .await + .get_group(guuid)? .context("There is no group with this uuid.")?; let mut location = group.dspfs_root().to_path_buf(); location.push(path); - let set = location.read_dir().context("Reading directory failed")? - .map(|i| i.map(|direntry| { - LocalFile::from_direntry(direntry) - })).flatten().collect::>()?; + let set = location + .read_dir() + .context("Reading directory failed")? + .map(|i| i.map(LocalFile::from_direntry)) + .flatten() + .collect::>()?; Ok(set) } - async fn get_files(&self, guuid: Uuid, user: &PublicUser, path: &Path) -> Result> { - let group = self.store.read().await.get_group(guuid)? - .context("There is no group with this uuid.")?.reload(self.store.clone())?; - - let files = group.get_files_from_user(user, path).await; + async fn get_files( + &self, + guuid: Uuid, + user: &PublicUser, + path: &Path, + ) -> Result> { + let group = self + .store + .read() + .await + .get_group(guuid)? + .context("There is no group with this uuid.")? + .reload(self.store.clone())?; + + let _files = group.get_files_from_user(user, path).await; todo!() } - async fn request_file(&self, hash: Hash, to: &Path) -> Result<()> { + async fn request_file(&self, _hash: Hash, _to: &Path) -> Result<()> { unimplemented!() } @@ -158,5 +187,3 @@ impl Api for Dspfs { unimplemented!() } } - - diff --git a/src/fs/file.rs b/src/fs/file.rs index 6c3ac45..1e3bb41 100644 --- a/src/fs/file.rs +++ b/src/fs/file.rs @@ -65,14 +65,13 @@ impl File { } } - /// TODO: maybe avoid cloning everything here pub fn simplify(&self) -> SimpleFile { SimpleFile { path: self.path.clone(), hash: self.hash.clone(), users: self.users.clone(), - file_size: self.file_size.clone(), + file_size: self.file_size, } } diff --git a/src/fs/group/heed.rs b/src/fs/group/heed.rs index ca533d4..0402357 100644 --- a/src/fs/group/heed.rs +++ b/src/fs/group/heed.rs @@ -73,7 +73,9 @@ impl GroupStore for HeedGroupStore { fn list_files(&self) -> Result> { let rtxn = self.env.read_txn()?; - let files = self.files.iter(&rtxn)? + let files = self + .files + .iter(&rtxn)? .map(|i| i.map(|(_hash, file)| file)) .flatten() .collect(); @@ -88,8 +90,7 @@ impl GroupStore for HeedGroupStore { .filetrees .get(&rtxn, user) .context("error getting filetree from db")? - .context("no filetree found for this user")? - ) + .context("no filetree found for this user")?) } fn delete_file(&mut self, user: &PublicUser, file: &File) -> Result<()> { diff --git a/src/fs/group/mod.rs b/src/fs/group/mod.rs index 5f606af..f245c2b 100644 --- a/src/fs/group/mod.rs +++ b/src/fs/group/mod.rs @@ -2,6 +2,7 @@ mod heed; mod store; use crate::fs::file::{File, SimpleFile}; +use crate::fs::filetree::FileTree; use crate::fs::group::heed::HeedGroupStore; use crate::fs::group::store::{GroupStore, SharedGroupStore}; use crate::fs::hash::Hash; @@ -9,6 +10,7 @@ use crate::global_store::{SharedStore, Store}; use crate::user::PublicUser; use anyhow::{Context, Result}; use serde::{Deserialize, Serialize}; +use std::collections::{BTreeSet, HashSet}; use std::io::SeekFrom; use std::ops::{Deref, DerefMut}; use std::path::{Path, PathBuf}; @@ -17,8 +19,6 @@ use tokio::fs; use tokio::io::AsyncReadExt; use tokio::sync::RwLock; use uuid::Uuid; -use std::collections::{BTreeSet, HashSet}; -use crate::fs::filetree::FileTree; /// A *StoredGroup* is a reduced version of a [Group], which can safely be stored in a database. /// For documentation on what a DSPFS *Group* is, refer to the documentation of [Group]. @@ -221,30 +221,28 @@ impl Group { /// Returns all files in the directory pointed to by path, from a certain user. /// This function only returns the files directly in that folder, and not recursively. - pub async fn get_files_from_user(&self, user: &PublicUser, path: &Path) -> Result> { + pub async fn get_files_from_user( + &self, + user: &PublicUser, + path: &Path, + ) -> Result> { let filetree = self.group_store.read().await.get_filetree(user)?; - let dir = filetree.find(path) - .context("no file exists at this path")?; + let dir = filetree.find(path).context("no file exists at this path")?; Ok(match dir { FileTree::Leaf { file, .. } => { let mut hs = HashSet::new(); hs.insert(file.simplify()); hs - }, - FileTree::Node { children, .. } => { - children.iter() - .map(|node| match node { - FileTree::Leaf { file, .. } => { - file.simplify() - }, - FileTree::Node { .. } => { - todo!() - }, - }) - .collect() - }, + } + FileTree::Node { children, .. } => children + .iter() + .map(|node| match node { + FileTree::Leaf { file, .. } => file.simplify(), + FileTree::Node { .. } => todo!(), + }) + .collect(), }) } } diff --git a/src/fs/group/store.rs b/src/fs/group/store.rs index 9703aeb..3d37eda 100644 --- a/src/fs/group/store.rs +++ b/src/fs/group/store.rs @@ -1,10 +1,10 @@ use crate::fs::file::File; +use crate::fs::filetree::FileTree; use crate::fs::hash::Hash; use crate::user::PublicUser; use anyhow::Result; use std::sync::Arc; use tokio::sync::RwLock; -use crate::fs::filetree::FileTree; /// Thread safe group store pub type SharedGroupStore = Arc>>; diff --git a/src/main.rs b/src/main.rs index a61e45e..22c5254 100644 --- a/src/main.rs +++ b/src/main.rs @@ -41,7 +41,5 @@ async fn main() -> Result<(), Box> { // Run program - - Ok(()) } diff --git a/src/user/mod.rs b/src/user/mod.rs index cf5644d..bb38e44 100644 --- a/src/user/mod.rs +++ b/src/user/mod.rs @@ -8,7 +8,9 @@ use ring::signature::{Ed25519KeyPair, KeyPair, UnparsedPublicKey, ED25519}; use serde::export::TryFrom; use zerocopy::{AsBytes, LayoutVerified}; -#[derive(serde::Serialize, serde::Deserialize, Clone, Eq, PartialEq, Debug, AsBytes, Ord, PartialOrd)] +#[derive( + serde::Serialize, serde::Deserialize, Clone, Eq, PartialEq, Debug, AsBytes, Ord, PartialOrd, +)] #[repr(packed)] pub struct PublicKey(pub(self) [u8; 32]);