rustfmt the codebase

This commit is contained in:
Lucien Greathouse
2019-08-27 15:10:34 -07:00
parent fea303ac8b
commit 7fb9aa2115
45 changed files with 766 additions and 764 deletions

View File

@@ -1,8 +1,4 @@
use std::{
io,
fmt,
path::PathBuf,
};
use std::{fmt, io, path::PathBuf};
use failure::Fail;
@@ -62,4 +58,4 @@ impl fmt::Display for FsError {
fn fmt(&self, output: &mut fmt::Formatter) -> fmt::Result {
write!(output, "{}: {}", self.path.display(), self.inner)
}
}
}

View File

@@ -29,4 +29,4 @@ pub trait ImfsFetcher {
fn watch(&mut self, path: &Path);
fn unwatch(&mut self, path: &Path);
fn receiver(&self) -> Receiver<ImfsEvent>;
}
}

View File

@@ -8,9 +8,9 @@ use crossbeam_channel::Receiver;
use crate::path_map::PathMap;
use super::{
error::{FsError, FsResult},
fetcher::{FileType, ImfsEvent, ImfsFetcher},
snapshot::ImfsSnapshot,
error::{FsResult, FsError},
fetcher::{ImfsFetcher, FileType, ImfsEvent},
};
/// An in-memory filesystem that can be incrementally populated and updated as
@@ -89,16 +89,22 @@ impl<F: ImfsFetcher> Imfs<F> {
match snapshot {
ImfsSnapshot::File(file) => {
self.inner.insert(path.to_path_buf(), ImfsItem::File(ImfsFile {
path: path.to_path_buf(),
contents: Some(file.contents),
}));
self.inner.insert(
path.to_path_buf(),
ImfsItem::File(ImfsFile {
path: path.to_path_buf(),
contents: Some(file.contents),
}),
);
}
ImfsSnapshot::Directory(directory) => {
self.inner.insert(path.to_path_buf(), ImfsItem::Directory(ImfsDirectory {
path: path.to_path_buf(),
children_enumerated: true,
}));
self.inner.insert(
path.to_path_buf(),
ImfsItem::Directory(ImfsDirectory {
path: path.to_path_buf(),
children_enumerated: true,
}),
);
for (child_name, child) in directory.children.into_iter() {
self.load_from_snapshot(path.join(child_name), child);
@@ -114,7 +120,9 @@ impl<F: ImfsFetcher> Imfs<F> {
return Ok(());
}
let new_type = self.fetcher.file_type(path)
let new_type = self
.fetcher
.file_type(path)
.map_err(|err| FsError::new(err, path.to_path_buf()))?;
match self.inner.get_mut(path) {
@@ -131,18 +139,25 @@ impl<F: ImfsFetcher> Imfs<F> {
}
(ImfsItem::File(_), FileType::Directory) => {
self.inner.remove(path);
self.inner.insert(path.to_path_buf(), ImfsItem::new_from_type(FileType::Directory, path));
self.inner.insert(
path.to_path_buf(),
ImfsItem::new_from_type(FileType::Directory, path),
);
self.fetcher.watch(path);
}
(ImfsItem::Directory(_), FileType::File) => {
self.inner.remove(path);
self.inner.insert(path.to_path_buf(), ImfsItem::new_from_type(FileType::File, path));
self.inner.insert(
path.to_path_buf(),
ImfsItem::new_from_type(FileType::File, path),
);
self.fetcher.unwatch(path);
}
}
}
None => {
self.inner.insert(path.to_path_buf(), ImfsItem::new_from_type(new_type, path));
self.inner
.insert(path.to_path_buf(), ImfsItem::new_from_type(new_type, path));
}
}
@@ -185,13 +200,19 @@ impl<F: ImfsFetcher> Imfs<F> {
match self.inner.get_mut(path).unwrap() {
ImfsItem::File(file) => {
if file.contents.is_none() {
file.contents = Some(self.fetcher.read_contents(path)
.map_err(|err| FsError::new(err, path.to_path_buf()))?);
file.contents = Some(
self.fetcher
.read_contents(path)
.map_err(|err| FsError::new(err, path.to_path_buf()))?,
);
}
Ok(file.contents.as_ref().unwrap())
}
ImfsItem::Directory(_) => Err(FsError::new(io::Error::new(io::ErrorKind::Other, "Can't read a directory"), path.to_path_buf()))
ImfsItem::Directory(_) => Err(FsError::new(
io::Error::new(io::ErrorKind::Other, "Can't read a directory"),
path.to_path_buf(),
)),
}
}
@@ -205,7 +226,9 @@ impl<F: ImfsFetcher> Imfs<F> {
self.fetcher.watch(path);
if dir.children_enumerated {
return self.inner.children(path)
return self
.inner
.children(path)
.unwrap() // TODO: Handle None here, which means the PathMap entry did not exist.
.into_iter()
.map(PathBuf::from) // Convert paths from &Path to PathBuf
@@ -215,13 +238,17 @@ impl<F: ImfsFetcher> Imfs<F> {
.collect::<FsResult<Vec<ImfsEntry>>>();
}
self.fetcher.read_children(path)
self.fetcher
.read_children(path)
.map_err(|err| FsError::new(err, path.to_path_buf()))?
.into_iter()
.map(|path| self.get(path))
.collect::<FsResult<Vec<ImfsEntry>>>()
}
ImfsItem::File(_) => Err(FsError::new(io::Error::new(io::ErrorKind::Other, "Can't read a directory"), path.to_path_buf()))
ImfsItem::File(_) => Err(FsError::new(
io::Error::new(io::ErrorKind::Other, "Can't read a directory"),
path.to_path_buf(),
)),
}
}
@@ -256,14 +283,17 @@ impl<F: ImfsFetcher> Imfs<F> {
/// is using, this call may read exactly only the given path and no more.
fn read_if_not_exists(&mut self, path: &Path) -> FsResult<()> {
if !self.inner.contains_key(path) {
let kind = self.fetcher.file_type(path)
let kind = self
.fetcher
.file_type(path)
.map_err(|err| FsError::new(err, path.to_path_buf()))?;
if kind == FileType::Directory {
self.fetcher.watch(path);
}
self.inner.insert(path.to_path_buf(), ImfsItem::new_from_type(kind, path));
self.inner
.insert(path.to_path_buf(), ImfsItem::new_from_type(kind, path));
}
Ok(())
@@ -293,10 +323,7 @@ impl ImfsEntry {
imfs.get_contents(&self.path)
}
pub fn children(
&self,
imfs: &mut Imfs<impl ImfsFetcher>,
) -> FsResult<Vec<ImfsEntry>> {
pub fn children(&self, imfs: &mut Imfs<impl ImfsFetcher>) -> FsResult<Vec<ImfsEntry>> {
imfs.get_children(&self.path)
}
@@ -352,19 +379,12 @@ pub struct ImfsDirectory {
mod test {
use super::*;
use std::{
rc::Rc,
cell::RefCell,
};
use std::{cell::RefCell, rc::Rc};
use crossbeam_channel::Receiver;
use maplit::hashmap;
use super::super::{
noop_fetcher::NoopFetcher,
error::FsErrorKind,
fetcher::ImfsEvent,
};
use super::super::{error::FsErrorKind, fetcher::ImfsEvent, noop_fetcher::NoopFetcher};
#[test]
fn from_snapshot_file() {
@@ -458,11 +478,9 @@ mod test {
unimplemented!();
}
fn watch(&mut self, _path: &Path) {
}
fn watch(&mut self, _path: &Path) {}
fn unwatch(&mut self, _path: &Path) {
}
fn unwatch(&mut self, _path: &Path) {}
fn receiver(&self) -> Receiver<ImfsEvent> {
crossbeam_channel::never()
@@ -477,11 +495,9 @@ mod test {
inner: mock_state.clone(),
});
let a = imfs.get("/dir/a.txt")
.expect("mock file did not exist");
let a = imfs.get("/dir/a.txt").expect("mock file did not exist");
let contents = a.contents(&mut imfs)
.expect("mock file contents error");
let contents = a.contents(&mut imfs).expect("mock file contents error");
assert_eq!(contents, b"Initial contents");
@@ -493,8 +509,7 @@ mod test {
imfs.raise_file_changed("/dir/a.txt")
.expect("error processing file change");
let contents = a.contents(&mut imfs)
.expect("mock file contents error");
let contents = a.contents(&mut imfs).expect("mock file contents error");
assert_eq!(contents, b"Changed contents");
}
@@ -506,10 +521,10 @@ mod test {
let file = ImfsSnapshot::file("hello, world!");
imfs.load_from_snapshot("/hello.txt", file);
let hello = imfs.get("/hello.txt")
.expect("couldn't get hello.txt");
let hello = imfs.get("/hello.txt").expect("couldn't get hello.txt");
let contents = hello.contents(&mut imfs)
let contents = hello
.contents(&mut imfs)
.expect("couldn't get hello.txt contents");
assert_eq!(contents, b"hello, world!");
@@ -527,4 +542,4 @@ mod test {
}
}
}
}
}

View File

@@ -9,9 +9,9 @@ pub use error::*;
pub mod new {
pub use super::error::*;
pub use super::imfs::*;
pub use super::fetcher::*;
pub use super::real_fetcher::*;
pub use super::imfs::*;
pub use super::noop_fetcher::*;
pub use super::real_fetcher::*;
pub use super::snapshot::*;
}
}

View File

@@ -8,21 +8,30 @@ use std::{
use crossbeam_channel::Receiver;
use super::fetcher::{ImfsFetcher, FileType, ImfsEvent};
use super::fetcher::{FileType, ImfsEvent, ImfsFetcher};
pub struct NoopFetcher;
impl ImfsFetcher for NoopFetcher {
fn file_type(&mut self, _path: &Path) -> io::Result<FileType> {
Err(io::Error::new(io::ErrorKind::NotFound, "NoopFetcher always returns NotFound"))
Err(io::Error::new(
io::ErrorKind::NotFound,
"NoopFetcher always returns NotFound",
))
}
fn read_children(&mut self, _path: &Path) -> io::Result<Vec<PathBuf>> {
Err(io::Error::new(io::ErrorKind::NotFound, "NoopFetcher always returns NotFound"))
Err(io::Error::new(
io::ErrorKind::NotFound,
"NoopFetcher always returns NotFound",
))
}
fn read_contents(&mut self, _path: &Path) -> io::Result<Vec<u8>> {
Err(io::Error::new(io::ErrorKind::NotFound, "NoopFetcher always returns NotFound"))
Err(io::Error::new(
io::ErrorKind::NotFound,
"NoopFetcher always returns NotFound",
))
}
fn create_directory(&mut self, _path: &Path) -> io::Result<()> {
@@ -37,13 +46,11 @@ impl ImfsFetcher for NoopFetcher {
Ok(())
}
fn watch(&mut self, _path: &Path) {
}
fn watch(&mut self, _path: &Path) {}
fn unwatch(&mut self, _path: &Path) {
}
fn unwatch(&mut self, _path: &Path) {}
fn receiver(&self) -> Receiver<ImfsEvent> {
crossbeam_channel::never()
}
}
}

View File

@@ -2,18 +2,17 @@
//! std::fs interface and notify as the file watcher.
use std::{
fs,
io,
fs, io,
path::{Path, PathBuf},
sync::mpsc,
time::Duration,
};
use crossbeam_channel::{unbounded, Receiver};
use jod_thread::JoinHandle;
use crossbeam_channel::{Receiver, unbounded};
use notify::{RecursiveMode, RecommendedWatcher, Watcher};
use notify::{RecommendedWatcher, RecursiveMode, Watcher};
use super::fetcher::{ImfsFetcher, FileType, ImfsEvent};
use super::fetcher::{FileType, ImfsEvent, ImfsFetcher};
/// Workaround to disable the file watcher for processes that don't need it,
/// since notify appears hang on to mpsc Sender objects too long, causing Rojo
@@ -51,7 +50,7 @@ impl RealFetcher {
.spawn(move || {
notify_receiver
.into_iter()
.for_each(|event| { sender.send(event).unwrap() });
.for_each(|event| sender.send(event).unwrap());
})
.expect("Could not start message converter thread");
@@ -59,10 +58,10 @@ impl RealFetcher {
// causing our program to deadlock. Once this is fixed, watcher no
// longer needs to be optional, but is still maybe useful?
let watcher = match watch_mode {
WatchMode::Enabled => {
Some(notify::watcher(notify_sender, Duration::from_millis(300))
.expect("Couldn't start 'notify' file watcher"))
}
WatchMode::Enabled => Some(
notify::watcher(notify_sender, Duration::from_millis(300))
.expect("Couldn't start 'notify' file watcher"),
),
WatchMode::Disabled => None,
};
@@ -152,4 +151,4 @@ impl ImfsFetcher for RealFetcher {
fn receiver(&self) -> Receiver<ImfsEvent> {
self.receiver.clone()
}
}
}

View File

@@ -16,14 +16,9 @@ impl ImfsSnapshot {
/// Create a new directory ImfsSnapshot with the given children.
pub fn dir<S: Into<String>>(children: HashMap<S, ImfsSnapshot>) -> ImfsSnapshot {
let children = children
.into_iter()
.map(|(k, v)| (k.into(), v))
.collect();
let children = children.into_iter().map(|(k, v)| (k.into(), v)).collect();
ImfsSnapshot::Directory(DirectorySnapshot {
children,
})
ImfsSnapshot::Directory(DirectorySnapshot { children })
}
}
@@ -35,4 +30,4 @@ pub struct FileSnapshot {
#[derive(Debug, Clone)]
pub struct DirectorySnapshot {
pub children: HashMap<String, ImfsSnapshot>,
}
}