Skip to content

Commit 532088e

Browse files
author
Victor Roest
committed
Fixed fmt and clippy
1 parent 796e48b commit 532088e

File tree

8 files changed

+109
-80
lines changed

8 files changed

+109
-80
lines changed

src/dspfs/api.rs

+20-16
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
use crate::user::PublicUser;
21
use crate::fs::hash::Hash;
2+
use crate::user::PublicUser;
33

4-
use std::path::Path;
5-
use uuid::Uuid;
6-
use anyhow::Result;
74
use crate::fs::file::SimpleFile;
5+
use anyhow::Result;
86
use async_trait::async_trait;
9-
use std::collections::{HashSet, BTreeSet};
10-
use std::time::SystemTime;
11-
use std::fs::DirEntry;
7+
use serde::{Deserialize, Serialize};
8+
use std::collections::{BTreeSet, HashSet};
129
use std::ffi::OsString;
13-
use serde::{Serialize,Deserialize};
10+
use std::fs::DirEntry;
11+
use std::path::Path;
12+
use std::time::SystemTime;
13+
use uuid::Uuid;
1414

15-
#[derive(Serialize,Deserialize,Debug,Clone, Eq, PartialEq, Hash)]
15+
#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq, Hash)]
1616
pub struct LocalFile {
1717
name: OsString,
1818
modtime: SystemTime,
@@ -37,7 +37,7 @@ impl LocalFile {
3737
pub trait Api {
3838
/// Equivalent of `git init`. Creates a new group with only you in it.
3939
async fn init_group(&self, path: &Path) -> Result<Uuid>;
40-
40+
4141
/// Equivalent of `git add`. Adds a file to the group and makes it visible for others in the group.
4242
async fn add_file(&self, guuid: Uuid, path: &Path) -> Result<SimpleFile>;
4343

@@ -54,13 +54,20 @@ pub trait Api {
5454
async fn get_available_files(&self, guuid: Uuid, path: &Path) -> Result<HashSet<LocalFile>>;
5555

5656
/// gets a certain level of filetree as seen by another user
57-
async fn get_files(&self, guuid: Uuid, user: &PublicUser, path: &Path) -> Result<HashSet<SimpleFile>>;
57+
async fn get_files(
58+
&self,
59+
guuid: Uuid,
60+
user: &PublicUser,
61+
path: &Path,
62+
) -> Result<HashSet<SimpleFile>>;
5863

5964
/// requests a file from other users in the group
6065
async fn request_file(&self, hash: Hash, to: &Path) -> Result<()>;
6166

6267
/// Lists current download/uploads.
63-
async fn status(&self) {todo!()}
68+
async fn status(&self) {
69+
todo!()
70+
}
6471

6572
/// Refreshes internal state.
6673
/// may do any of the following things:
@@ -69,12 +76,9 @@ pub trait Api {
6976
/// * Check for new files from other users.
7077
async fn refresh(&self);
7178

72-
7379
// TODO:
74-
async fn add_folder(&self, guuid: Uuid, path: &Path) -> Result<()> {
80+
async fn add_folder(&self, _guuid: Uuid, path: &Path) -> Result<()> {
7581
assert!(path.is_dir());
7682
todo!()
7783
}
7884
}
79-
80-

src/dspfs/mod.rs

+64-37
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
use crate::dspfs::builder::DspfsBuilder;
22
use crate::dspfs::client::Client;
33
use crate::dspfs::server::{Server, ServerHandle};
4+
use crate::fs::file::File;
5+
use crate::fs::file::SimpleFile;
46
use crate::fs::group::StoredGroup;
7+
use crate::fs::hash::Hash;
58
use crate::global_store::{SharedStore, Store};
69
use crate::user::{PrivateUser, PublicUser};
7-
use anyhow::{Result, Context};
10+
use anyhow::{Context, Result};
11+
use api::Api;
12+
use api::LocalFile;
13+
use async_trait::async_trait;
814
use log::*;
9-
use std::collections::{HashMap, HashSet, BTreeSet};
15+
use std::collections::{BTreeSet, HashMap, HashSet};
1016
use std::fs;
1117
use std::mem;
1218
use std::path::Path;
1319
use uuid::Uuid;
14-
use crate::fs::file::SimpleFile;
15-
use crate::fs::file::File;
16-
use crate::fs::hash::Hash;
17-
use async_trait::async_trait;
18-
use api::LocalFile;
19-
use api::Api;
2020

21+
pub mod api;
2122
pub mod builder;
2223
pub mod client;
2324
pub mod server;
24-
pub mod api;
2525

2626
pub struct Dspfs<S: Store + 'static> {
2727
pub(self) store: SharedStore<S>,
@@ -63,7 +63,6 @@ impl<S: Store> Dspfs<S> {
6363
}
6464
}
6565

66-
6766
#[async_trait]
6867
impl<S: Store> Api for Dspfs<S> {
6968
async fn init_group(&self, path: &Path) -> Result<Uuid> {
@@ -72,7 +71,7 @@ impl<S: Store> Api for Dspfs<S> {
7271
if !group.dspfs_folder().exists() {
7372
// New folder
7473
fs::create_dir_all(group.dspfs_folder())?;
75-
let uuid = group.uuid.clone();
74+
let uuid = group.uuid;
7675
self.store.write().await.add_group(group)?;
7776

7877
Ok(uuid)
@@ -82,81 +81,109 @@ impl<S: Store> Api for Dspfs<S> {
8281
}
8382

8483
async fn add_file(&self, guuid: Uuid, path: &Path) -> Result<SimpleFile> {
85-
let mut group = self.store.read().await.get_group(guuid)?
86-
.context("There is no group with this uuid.")?.reload(self.store.clone())?;
84+
let mut group = self
85+
.store
86+
.read()
87+
.await
88+
.get_group(guuid)?
89+
.context("There is no group with this uuid.")?
90+
.reload(self.store.clone())?;
8791

8892
let us = self.store.read().await.get_self_user()?.context("")?;
8993

9094
let mut location = group.dspfs_root().to_path_buf();
9195
location.push(path);
9296

9397
if !location.is_file() {
94-
return Err(anyhow::anyhow!("Path does not point to a file"))
98+
return Err(anyhow::anyhow!("Path does not point to a file"));
9599
}
96100

97-
let file = File::new(location).await
98-
.context("Indexing file failed")?;
101+
let file = File::new(location).await.context("Indexing file failed")?;
99102

100103
let simple_file = file.simplify();
101104

102-
group.add_file(&us, file).await.context("Adding file to group failed")?;
105+
group
106+
.add_file(&us, file)
107+
.await
108+
.context("Adding file to group failed")?;
103109

104110
Ok(simple_file)
105111
}
106112

107-
async fn join_group(&self, guuid: Uuid, bootstrap_user: &PublicUser) -> Result<()> {
113+
async fn join_group(&self, _guuid: Uuid, _bootstrap_user: &PublicUser) -> Result<()> {
108114
unimplemented!("procrastination is life")
109115
}
110116

111117
async fn list_files(&self, guuid: Uuid) -> Result<HashSet<SimpleFile>> {
112-
let group = self.store.read().await.get_group(guuid)?
113-
.context("There is no group with this uuid.")?.reload(self.store.clone())?;
118+
let group = self
119+
.store
120+
.read()
121+
.await
122+
.get_group(guuid)?
123+
.context("There is no group with this uuid.")?
124+
.reload(self.store.clone())?;
114125

115-
let f = group.list_files().await?
116-
.into_iter()
117-
.map(|f| f.simplify());
126+
let f = group.list_files().await?.into_iter().map(|f| f.simplify());
118127

119128
Ok(f.collect())
120129
}
121130

122131
async fn get_users(&self, guuid: Uuid) -> Result<BTreeSet<PublicUser>> {
123-
let group = self.store.read().await.get_group(guuid)?
132+
let group = self
133+
.store
134+
.read()
135+
.await
136+
.get_group(guuid)?
124137
.context("There is no group with this uuid.")?;
125138

126139
Ok(group.users)
127140
}
128141

129142
async fn get_available_files(&self, guuid: Uuid, path: &Path) -> Result<HashSet<LocalFile>> {
130-
let group = self.store.read().await.get_group(guuid)?
143+
let group = self
144+
.store
145+
.read()
146+
.await
147+
.get_group(guuid)?
131148
.context("There is no group with this uuid.")?;
132149

133150
let mut location = group.dspfs_root().to_path_buf();
134151
location.push(path);
135152

136-
let set = location.read_dir().context("Reading directory failed")?
137-
.map(|i| i.map(|direntry| {
138-
LocalFile::from_direntry(direntry)
139-
})).flatten().collect::<Result<_>>()?;
153+
let set = location
154+
.read_dir()
155+
.context("Reading directory failed")?
156+
.map(|i| i.map(LocalFile::from_direntry))
157+
.flatten()
158+
.collect::<Result<_>>()?;
140159

141160
Ok(set)
142161
}
143162

144-
async fn get_files(&self, guuid: Uuid, user: &PublicUser, path: &Path) -> Result<HashSet<SimpleFile>> {
145-
let group = self.store.read().await.get_group(guuid)?
146-
.context("There is no group with this uuid.")?.reload(self.store.clone())?;
147-
148-
let files = group.get_files_from_user(user, path).await;
163+
async fn get_files(
164+
&self,
165+
guuid: Uuid,
166+
user: &PublicUser,
167+
path: &Path,
168+
) -> Result<HashSet<SimpleFile>> {
169+
let group = self
170+
.store
171+
.read()
172+
.await
173+
.get_group(guuid)?
174+
.context("There is no group with this uuid.")?
175+
.reload(self.store.clone())?;
176+
177+
let _files = group.get_files_from_user(user, path).await;
149178

150179
todo!()
151180
}
152181

153-
async fn request_file(&self, hash: Hash, to: &Path) -> Result<()> {
182+
async fn request_file(&self, _hash: Hash, _to: &Path) -> Result<()> {
154183
unimplemented!()
155184
}
156185

157186
async fn refresh(&self) {
158187
unimplemented!()
159188
}
160189
}
161-
162-

src/fs/file.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,13 @@ impl File {
6565
}
6666
}
6767

68-
6968
/// TODO: maybe avoid cloning everything here
7069
pub fn simplify(&self) -> SimpleFile {
7170
SimpleFile {
7271
path: self.path.clone(),
7372
hash: self.hash.clone(),
7473
users: self.users.clone(),
75-
file_size: self.file_size.clone(),
74+
file_size: self.file_size,
7675
}
7776
}
7877

src/fs/group/heed.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ impl GroupStore for HeedGroupStore {
7373
fn list_files(&self) -> Result<Vec<File>> {
7474
let rtxn = self.env.read_txn()?;
7575

76-
let files = self.files.iter(&rtxn)?
76+
let files = self
77+
.files
78+
.iter(&rtxn)?
7779
.map(|i| i.map(|(_hash, file)| file))
7880
.flatten()
7981
.collect();
@@ -88,8 +90,7 @@ impl GroupStore for HeedGroupStore {
8890
.filetrees
8991
.get(&rtxn, user)
9092
.context("error getting filetree from db")?
91-
.context("no filetree found for this user")?
92-
)
93+
.context("no filetree found for this user")?)
9394
}
9495

9596
fn delete_file(&mut self, user: &PublicUser, file: &File) -> Result<()> {

src/fs/group/mod.rs

+16-18
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ mod heed;
22
mod store;
33

44
use crate::fs::file::{File, SimpleFile};
5+
use crate::fs::filetree::FileTree;
56
use crate::fs::group::heed::HeedGroupStore;
67
use crate::fs::group::store::{GroupStore, SharedGroupStore};
78
use crate::fs::hash::Hash;
89
use crate::global_store::{SharedStore, Store};
910
use crate::user::PublicUser;
1011
use anyhow::{Context, Result};
1112
use serde::{Deserialize, Serialize};
13+
use std::collections::{BTreeSet, HashSet};
1214
use std::io::SeekFrom;
1315
use std::ops::{Deref, DerefMut};
1416
use std::path::{Path, PathBuf};
@@ -17,8 +19,6 @@ use tokio::fs;
1719
use tokio::io::AsyncReadExt;
1820
use tokio::sync::RwLock;
1921
use uuid::Uuid;
20-
use std::collections::{BTreeSet, HashSet};
21-
use crate::fs::filetree::FileTree;
2222

2323
/// A *StoredGroup* is a reduced version of a [Group], which can safely be stored in a database.
2424
/// For documentation on what a DSPFS *Group* is, refer to the documentation of [Group].
@@ -221,30 +221,28 @@ impl<S: Store> Group<S> {
221221

222222
/// Returns all files in the directory pointed to by path, from a certain user.
223223
/// This function only returns the files directly in that folder, and not recursively.
224-
pub async fn get_files_from_user(&self, user: &PublicUser, path: &Path) -> Result<HashSet<SimpleFile>> {
224+
pub async fn get_files_from_user(
225+
&self,
226+
user: &PublicUser,
227+
path: &Path,
228+
) -> Result<HashSet<SimpleFile>> {
225229
let filetree = self.group_store.read().await.get_filetree(user)?;
226230

227-
let dir = filetree.find(path)
228-
.context("no file exists at this path")?;
231+
let dir = filetree.find(path).context("no file exists at this path")?;
229232

230233
Ok(match dir {
231234
FileTree::Leaf { file, .. } => {
232235
let mut hs = HashSet::new();
233236
hs.insert(file.simplify());
234237
hs
235-
},
236-
FileTree::Node { children, .. } => {
237-
children.iter()
238-
.map(|node| match node {
239-
FileTree::Leaf { file, .. } => {
240-
file.simplify()
241-
},
242-
FileTree::Node { .. } => {
243-
todo!()
244-
},
245-
})
246-
.collect()
247-
},
238+
}
239+
FileTree::Node { children, .. } => children
240+
.iter()
241+
.map(|node| match node {
242+
FileTree::Leaf { file, .. } => file.simplify(),
243+
FileTree::Node { .. } => todo!(),
244+
})
245+
.collect(),
248246
})
249247
}
250248
}

src/fs/group/store.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use crate::fs::file::File;
2+
use crate::fs::filetree::FileTree;
23
use crate::fs::hash::Hash;
34
use crate::user::PublicUser;
45
use anyhow::Result;
56
use std::sync::Arc;
67
use tokio::sync::RwLock;
7-
use crate::fs::filetree::FileTree;
88

99
/// Thread safe group store
1010
pub type SharedGroupStore = Arc<RwLock<Box<dyn GroupStore>>>;

src/main.rs

-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,5 @@ async fn main() -> Result<(), Box<dyn Error>> {
4141

4242
// Run program
4343

44-
45-
4644
Ok(())
4745
}

0 commit comments

Comments
 (0)