Formatting

This commit is contained in:
Antoine Gersant 2016-08-21 22:35:54 -07:00
parent 842514da85
commit 6339f94836
5 changed files with 69 additions and 71 deletions

View file

@ -32,14 +32,14 @@ pub fn get_api_handler(collection: Arc<Mutex<Collection>>) -> Mount {
mount.mount("/browse/", move |request: &mut Request| {
let mut acquired_collection = collection.deref().lock().unwrap();
self::browse(request, acquired_collection.deref_mut())
} );
});
}
{
let collection = collection.clone();
mount.mount("/flatten/", move |request: &mut Request| {
let mut acquired_collection = collection.deref().lock().unwrap();
self::flatten(request, acquired_collection.deref_mut())
} );
});
}
mount
}

View file

@ -26,9 +26,7 @@ pub struct Collection {
impl Collection {
pub fn new() -> Collection {
Collection{
vfs: Vfs::new(),
}
Collection { vfs: Vfs::new() }
}
pub fn mount(&mut self, name: &str, real_path: &Path) -> Result<(), CollectionError> {
@ -49,17 +47,14 @@ impl Collection {
let file_path = file_path.as_path();
if file_meta.is_file() {
let virtual_path = try!(self.vfs.real_to_virtual(file_path));
let path_string = try!(virtual_path.to_str().ok_or(CollectionError::PathDecoding));
let collection_file = CollectionFile::Song(Song {
path: path_string.to_string(),
});
let path_string = try!(virtual_path.to_str().ok_or(CollectionError::PathDecoding));
let collection_file = CollectionFile::Song(Song { path: path_string.to_string() });
out.push(collection_file);
} else if file_meta.is_dir() {
let virtual_path = try!(self.vfs.real_to_virtual(file_path));
let path_string = try!(virtual_path.to_str().ok_or(CollectionError::PathDecoding));
let collection_file = CollectionFile::Directory(Directory {
path: path_string.to_string(),
});
let path_string = try!(virtual_path.to_str().ok_or(CollectionError::PathDecoding));
let collection_file =
CollectionFile::Directory(Directory { path: path_string.to_string() });
out.push(collection_file);
}
}

View file

@ -3,8 +3,7 @@ use std::fmt;
use std::io;
#[derive(Debug)]
pub enum CollectionError
{
pub enum CollectionError {
PathDecoding,
Io(io::Error),
ConflictingMount,
@ -22,7 +21,9 @@ impl error::Error for CollectionError {
match *self {
CollectionError::Io(ref err) => err.description(),
CollectionError::PathDecoding => "Error while decoding a Path as a UTF-8 string",
CollectionError::ConflictingMount => "Attempting to mount multiple directories under the same name",
CollectionError::ConflictingMount => {
"Attempting to mount multiple directories under the same name"
}
CollectionError::PathNotInVfs => "Requested path does not index a mount point",
}
}
@ -42,8 +43,12 @@ impl fmt::Display for CollectionError {
match *self {
CollectionError::Io(ref err) => write!(f, "IO error: {}", err),
CollectionError::PathDecoding => write!(f, "Path decoding error"),
CollectionError::ConflictingMount => write!(f, "Mount point already has a target directory"),
CollectionError::PathNotInVfs => write!(f, "Requested path does not index a mount point"),
CollectionError::ConflictingMount => {
write!(f, "Mount point already has a target directory")
}
CollectionError::PathNotInVfs => {
write!(f, "Requested path does not index a mount point")
}
}
}
}
}

View file

@ -28,7 +28,7 @@ fn main() {
let collection = Arc::new(Mutex::new(collection));
let mut mount = Mount::new();
let api_handler = get_api_handler( collection );
let api_handler = get_api_handler(collection);
mount.mount("/static/", Static::new("samplemusic/"))
.mount("/api/", api_handler);

View file

@ -5,72 +5,70 @@ use std::path::Path;
use error::*;
pub struct Vfs {
mount_points: HashMap<String, PathBuf>,
mount_points: HashMap<String, PathBuf>,
}
impl Vfs {
pub fn new() -> Vfs {
let instance = Vfs {
mount_points: HashMap::new(),
};
instance
}
pub fn new() -> Vfs {
let instance = Vfs { mount_points: HashMap::new() };
instance
}
pub fn mount(&mut self, name: &str, real_path: &Path) -> Result<(), CollectionError> {
let name = name.to_string();
if self.mount_points.contains_key(&name) {
return Err(CollectionError::ConflictingMount);
}
self.mount_points.insert(name, real_path.to_path_buf());
Ok(())
}
pub fn mount(&mut self, name: &str, real_path: &Path) -> Result<(), CollectionError> {
let name = name.to_string();
if self.mount_points.contains_key(&name) {
return Err(CollectionError::ConflictingMount);
}
self.mount_points.insert(name, real_path.to_path_buf());
Ok(())
}
pub fn real_to_virtual(&self, real_path: &Path) -> Result<PathBuf, CollectionError> {
for (name, target) in &self.mount_points {
match real_path.strip_prefix(target) {
Ok(p) => {
let mount_path = Path::new(&name);
return Ok(mount_path.join(p));
},
Err(_) => (),
}
}
Err(CollectionError::PathNotInVfs)
}
pub fn real_to_virtual(&self, real_path: &Path) -> Result<PathBuf, CollectionError> {
for (name, target) in &self.mount_points {
match real_path.strip_prefix(target) {
Ok(p) => {
let mount_path = Path::new(&name);
return Ok(mount_path.join(p));
}
Err(_) => (),
}
}
Err(CollectionError::PathNotInVfs)
}
pub fn virtual_to_real(&self, virtual_path: &Path) -> Result<PathBuf, CollectionError> {
for (name, target) in &self.mount_points {
let mount_path = Path::new(&name);
match virtual_path.strip_prefix(mount_path) {
Ok(p) => return Ok(target.join(p)),
Err(_) => (),
}
}
Err(CollectionError::PathNotInVfs)
}
pub fn virtual_to_real(&self, virtual_path: &Path) -> Result<PathBuf, CollectionError> {
for (name, target) in &self.mount_points {
let mount_path = Path::new(&name);
match virtual_path.strip_prefix(mount_path) {
Ok(p) => return Ok(target.join(p)),
Err(_) => (),
}
}
Err(CollectionError::PathNotInVfs)
}
}
#[test]
fn test_mount() {
let mut vfs = Vfs::new();
assert!(vfs.mount("root", Path::new("test_dir")).is_ok());
assert!(vfs.mount("root", Path::new("another_dir")).is_err());
let mut vfs = Vfs::new();
assert!(vfs.mount("root", Path::new("test_dir")).is_ok());
assert!(vfs.mount("root", Path::new("another_dir")).is_err());
}
#[test]
fn test_virtual_to_real() {
let mut vfs = Vfs::new();
assert!(vfs.mount("root", Path::new("test_dir")).is_ok());
let correct_path = Path::new("test_dir/somewhere/something.png");
let found_path = vfs.virtual_to_real(Path::new("root/somewhere/something.png")).unwrap();
assert!(found_path == correct_path);
let mut vfs = Vfs::new();
assert!(vfs.mount("root", Path::new("test_dir")).is_ok());
let correct_path = Path::new("test_dir/somewhere/something.png");
let found_path = vfs.virtual_to_real(Path::new("root/somewhere/something.png")).unwrap();
assert!(found_path == correct_path);
}
#[test]
fn test_real_to_virtual() {
let mut vfs = Vfs::new();
assert!(vfs.mount("root", Path::new("test_dir")).is_ok());
let correct_path = Path::new("root/somewhere/something.png");
let found_path = vfs.real_to_virtual(Path::new("test_dir/somewhere/something.png")).unwrap();
assert!(found_path == correct_path);
}
let mut vfs = Vfs::new();
assert!(vfs.mount("root", Path::new("test_dir")).is_ok());
let correct_path = Path::new("root/somewhere/something.png");
let found_path = vfs.real_to_virtual(Path::new("test_dir/somewhere/something.png")).unwrap();
assert!(found_path == correct_path);
}