Formatting
This commit is contained in:
parent
842514da85
commit
6339f94836
5 changed files with 69 additions and 71 deletions
|
@ -32,14 +32,14 @@ pub fn get_api_handler(collection: Arc<Mutex<Collection>>) -> Mount {
|
||||||
mount.mount("/browse/", move |request: &mut Request| {
|
mount.mount("/browse/", move |request: &mut Request| {
|
||||||
let mut acquired_collection = collection.deref().lock().unwrap();
|
let mut acquired_collection = collection.deref().lock().unwrap();
|
||||||
self::browse(request, acquired_collection.deref_mut())
|
self::browse(request, acquired_collection.deref_mut())
|
||||||
} );
|
});
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
let collection = collection.clone();
|
let collection = collection.clone();
|
||||||
mount.mount("/flatten/", move |request: &mut Request| {
|
mount.mount("/flatten/", move |request: &mut Request| {
|
||||||
let mut acquired_collection = collection.deref().lock().unwrap();
|
let mut acquired_collection = collection.deref().lock().unwrap();
|
||||||
self::flatten(request, acquired_collection.deref_mut())
|
self::flatten(request, acquired_collection.deref_mut())
|
||||||
} );
|
});
|
||||||
}
|
}
|
||||||
mount
|
mount
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,9 +26,7 @@ pub struct Collection {
|
||||||
|
|
||||||
impl Collection {
|
impl Collection {
|
||||||
pub fn new() -> Collection {
|
pub fn new() -> Collection {
|
||||||
Collection{
|
Collection { vfs: Vfs::new() }
|
||||||
vfs: Vfs::new(),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn mount(&mut self, name: &str, real_path: &Path) -> Result<(), CollectionError> {
|
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();
|
let file_path = file_path.as_path();
|
||||||
if file_meta.is_file() {
|
if file_meta.is_file() {
|
||||||
let virtual_path = try!(self.vfs.real_to_virtual(file_path));
|
let virtual_path = try!(self.vfs.real_to_virtual(file_path));
|
||||||
let path_string = try!(virtual_path.to_str().ok_or(CollectionError::PathDecoding));
|
let path_string = try!(virtual_path.to_str().ok_or(CollectionError::PathDecoding));
|
||||||
let collection_file = CollectionFile::Song(Song {
|
let collection_file = CollectionFile::Song(Song { path: path_string.to_string() });
|
||||||
path: path_string.to_string(),
|
|
||||||
});
|
|
||||||
out.push(collection_file);
|
out.push(collection_file);
|
||||||
} else if file_meta.is_dir() {
|
} else if file_meta.is_dir() {
|
||||||
let virtual_path = try!(self.vfs.real_to_virtual(file_path));
|
let virtual_path = try!(self.vfs.real_to_virtual(file_path));
|
||||||
let path_string = try!(virtual_path.to_str().ok_or(CollectionError::PathDecoding));
|
let path_string = try!(virtual_path.to_str().ok_or(CollectionError::PathDecoding));
|
||||||
let collection_file = CollectionFile::Directory(Directory {
|
let collection_file =
|
||||||
path: path_string.to_string(),
|
CollectionFile::Directory(Directory { path: path_string.to_string() });
|
||||||
});
|
|
||||||
out.push(collection_file);
|
out.push(collection_file);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
17
src/error.rs
17
src/error.rs
|
@ -3,8 +3,7 @@ use std::fmt;
|
||||||
use std::io;
|
use std::io;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum CollectionError
|
pub enum CollectionError {
|
||||||
{
|
|
||||||
PathDecoding,
|
PathDecoding,
|
||||||
Io(io::Error),
|
Io(io::Error),
|
||||||
ConflictingMount,
|
ConflictingMount,
|
||||||
|
@ -22,7 +21,9 @@ impl error::Error for CollectionError {
|
||||||
match *self {
|
match *self {
|
||||||
CollectionError::Io(ref err) => err.description(),
|
CollectionError::Io(ref err) => err.description(),
|
||||||
CollectionError::PathDecoding => "Error while decoding a Path as a UTF-8 string",
|
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",
|
CollectionError::PathNotInVfs => "Requested path does not index a mount point",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -42,8 +43,12 @@ impl fmt::Display for CollectionError {
|
||||||
match *self {
|
match *self {
|
||||||
CollectionError::Io(ref err) => write!(f, "IO error: {}", err),
|
CollectionError::Io(ref err) => write!(f, "IO error: {}", err),
|
||||||
CollectionError::PathDecoding => write!(f, "Path decoding error"),
|
CollectionError::PathDecoding => write!(f, "Path decoding error"),
|
||||||
CollectionError::ConflictingMount => write!(f, "Mount point already has a target directory"),
|
CollectionError::ConflictingMount => {
|
||||||
CollectionError::PathNotInVfs => write!(f, "Requested path does not index a mount point"),
|
write!(f, "Mount point already has a target directory")
|
||||||
|
}
|
||||||
|
CollectionError::PathNotInVfs => {
|
||||||
|
write!(f, "Requested path does not index a mount point")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,7 +28,7 @@ fn main() {
|
||||||
let collection = Arc::new(Mutex::new(collection));
|
let collection = Arc::new(Mutex::new(collection));
|
||||||
|
|
||||||
let mut mount = Mount::new();
|
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.mount("/static/", Static::new("samplemusic/"))
|
||||||
.mount("/api/", api_handler);
|
.mount("/api/", api_handler);
|
||||||
|
|
||||||
|
|
100
src/vfs.rs
100
src/vfs.rs
|
@ -5,72 +5,70 @@ use std::path::Path;
|
||||||
use error::*;
|
use error::*;
|
||||||
|
|
||||||
pub struct Vfs {
|
pub struct Vfs {
|
||||||
mount_points: HashMap<String, PathBuf>,
|
mount_points: HashMap<String, PathBuf>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Vfs {
|
impl Vfs {
|
||||||
pub fn new() -> Vfs {
|
pub fn new() -> Vfs {
|
||||||
let instance = Vfs {
|
let instance = Vfs { mount_points: HashMap::new() };
|
||||||
mount_points: HashMap::new(),
|
instance
|
||||||
};
|
}
|
||||||
instance
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn mount(&mut self, name: &str, real_path: &Path) -> Result<(), CollectionError> {
|
pub fn mount(&mut self, name: &str, real_path: &Path) -> Result<(), CollectionError> {
|
||||||
let name = name.to_string();
|
let name = name.to_string();
|
||||||
if self.mount_points.contains_key(&name) {
|
if self.mount_points.contains_key(&name) {
|
||||||
return Err(CollectionError::ConflictingMount);
|
return Err(CollectionError::ConflictingMount);
|
||||||
}
|
}
|
||||||
self.mount_points.insert(name, real_path.to_path_buf());
|
self.mount_points.insert(name, real_path.to_path_buf());
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn real_to_virtual(&self, real_path: &Path) -> Result<PathBuf, CollectionError> {
|
pub fn real_to_virtual(&self, real_path: &Path) -> Result<PathBuf, CollectionError> {
|
||||||
for (name, target) in &self.mount_points {
|
for (name, target) in &self.mount_points {
|
||||||
match real_path.strip_prefix(target) {
|
match real_path.strip_prefix(target) {
|
||||||
Ok(p) => {
|
Ok(p) => {
|
||||||
let mount_path = Path::new(&name);
|
let mount_path = Path::new(&name);
|
||||||
return Ok(mount_path.join(p));
|
return Ok(mount_path.join(p));
|
||||||
},
|
}
|
||||||
Err(_) => (),
|
Err(_) => (),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(CollectionError::PathNotInVfs)
|
Err(CollectionError::PathNotInVfs)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn virtual_to_real(&self, virtual_path: &Path) -> Result<PathBuf, CollectionError> {
|
pub fn virtual_to_real(&self, virtual_path: &Path) -> Result<PathBuf, CollectionError> {
|
||||||
for (name, target) in &self.mount_points {
|
for (name, target) in &self.mount_points {
|
||||||
let mount_path = Path::new(&name);
|
let mount_path = Path::new(&name);
|
||||||
match virtual_path.strip_prefix(mount_path) {
|
match virtual_path.strip_prefix(mount_path) {
|
||||||
Ok(p) => return Ok(target.join(p)),
|
Ok(p) => return Ok(target.join(p)),
|
||||||
Err(_) => (),
|
Err(_) => (),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(CollectionError::PathNotInVfs)
|
Err(CollectionError::PathNotInVfs)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_mount() {
|
fn test_mount() {
|
||||||
let mut vfs = Vfs::new();
|
let mut vfs = Vfs::new();
|
||||||
assert!(vfs.mount("root", Path::new("test_dir")).is_ok());
|
assert!(vfs.mount("root", Path::new("test_dir")).is_ok());
|
||||||
assert!(vfs.mount("root", Path::new("another_dir")).is_err());
|
assert!(vfs.mount("root", Path::new("another_dir")).is_err());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_virtual_to_real() {
|
fn test_virtual_to_real() {
|
||||||
let mut vfs = Vfs::new();
|
let mut vfs = Vfs::new();
|
||||||
assert!(vfs.mount("root", Path::new("test_dir")).is_ok());
|
assert!(vfs.mount("root", Path::new("test_dir")).is_ok());
|
||||||
let correct_path = Path::new("test_dir/somewhere/something.png");
|
let correct_path = Path::new("test_dir/somewhere/something.png");
|
||||||
let found_path = vfs.virtual_to_real(Path::new("root/somewhere/something.png")).unwrap();
|
let found_path = vfs.virtual_to_real(Path::new("root/somewhere/something.png")).unwrap();
|
||||||
assert!(found_path == correct_path);
|
assert!(found_path == correct_path);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_real_to_virtual() {
|
fn test_real_to_virtual() {
|
||||||
let mut vfs = Vfs::new();
|
let mut vfs = Vfs::new();
|
||||||
assert!(vfs.mount("root", Path::new("test_dir")).is_ok());
|
assert!(vfs.mount("root", Path::new("test_dir")).is_ok());
|
||||||
let correct_path = Path::new("root/somewhere/something.png");
|
let correct_path = Path::new("root/somewhere/something.png");
|
||||||
let found_path = vfs.real_to_virtual(Path::new("test_dir/somewhere/something.png")).unwrap();
|
let found_path = vfs.real_to_virtual(Path::new("test_dir/somewhere/something.png")).unwrap();
|
||||||
assert!(found_path == correct_path);
|
assert!(found_path == correct_path);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue