mirror of
https://github.com/agersant/polaris
synced 2024-09-20 05:41:52 +00:00
Extracted CollectionError to submodule
This commit is contained in:
parent
474710a6a9
commit
f067a1c3d1
3 changed files with 63 additions and 20 deletions
|
@ -9,11 +9,13 @@ use rustc_serialize::json;
|
|||
use url::percent_encoding::percent_decode;
|
||||
|
||||
use collection;
|
||||
use collection::CollectionError;
|
||||
|
||||
impl From<collection::CollectionError> for IronError {
|
||||
fn from(err: collection::CollectionError) -> IronError {
|
||||
impl From<CollectionError> for IronError {
|
||||
fn from(err: CollectionError) -> IronError {
|
||||
match err {
|
||||
collection::CollectionError::Io(e) => IronError::new(e, status::NotFound)
|
||||
CollectionError::Io(e) => IronError::new(e, status::NotFound),
|
||||
CollectionError::PathDecoding => IronError::new(err, status::InternalServerError)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
41
src/collection/error.rs
Normal file
41
src/collection/error.rs
Normal file
|
@ -0,0 +1,41 @@
|
|||
use std::error;
|
||||
use std::fmt;
|
||||
use std::io;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum CollectionError
|
||||
{
|
||||
PathDecoding,
|
||||
Io(io::Error),
|
||||
}
|
||||
|
||||
impl From<io::Error> for CollectionError {
|
||||
fn from(err: io::Error) -> CollectionError {
|
||||
CollectionError::Io(err)
|
||||
}
|
||||
}
|
||||
|
||||
impl error::Error for CollectionError {
|
||||
fn description(&self) -> &str {
|
||||
match *self {
|
||||
CollectionError::Io(ref err) => err.description(),
|
||||
CollectionError::PathDecoding => "Error while decoding a Path as a UTF-8 string",
|
||||
}
|
||||
}
|
||||
|
||||
fn cause(&self) -> Option<&error::Error> {
|
||||
match *self {
|
||||
CollectionError::Io(ref err) => Some(err),
|
||||
CollectionError::PathDecoding => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for CollectionError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match *self {
|
||||
CollectionError::Io(ref err) => write!(f, "IO error: {}", err),
|
||||
CollectionError::PathDecoding => write!(f, "Path decoding error"),
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,14 +1,19 @@
|
|||
use std::fs;
|
||||
use std::io;
|
||||
use std::path::Path;
|
||||
use std::path::PathBuf;
|
||||
|
||||
pub use self::error::CollectionError;
|
||||
|
||||
mod error;
|
||||
|
||||
#[derive(Debug, RustcEncodable)]
|
||||
pub struct Song(PathBuf);
|
||||
pub struct Song {
|
||||
path: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, RustcEncodable)]
|
||||
pub struct Directory(PathBuf);
|
||||
pub struct Directory {
|
||||
path: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, RustcEncodable)]
|
||||
pub enum CollectionFile {
|
||||
|
@ -16,17 +21,6 @@ pub enum CollectionFile {
|
|||
Song(Song),
|
||||
}
|
||||
|
||||
pub enum CollectionError
|
||||
{
|
||||
Io(io::Error),
|
||||
}
|
||||
|
||||
impl From<io::Error> for CollectionError {
|
||||
fn from(err: io::Error) -> CollectionError {
|
||||
CollectionError::Io(err)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn browse(path: &Path) -> Result<Vec<CollectionFile>, CollectionError> {
|
||||
|
||||
let full_path = "samplemusic/".to_string() + path.to_str().unwrap(); // TMP use mount directories
|
||||
|
@ -38,10 +32,16 @@ pub fn browse(path: &Path) -> Result<Vec<CollectionFile>, CollectionError> {
|
|||
let file_meta = try!(file.metadata());
|
||||
let file_path = file.path().to_owned();
|
||||
if file_meta.is_file() {
|
||||
let collection_file = CollectionFile::Song(Song(file_path));
|
||||
let path_string = try!(file_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 collection_file = CollectionFile::Directory(Directory(file_path));
|
||||
let path_string = try!(file_path.to_str().ok_or(CollectionError::PathDecoding));
|
||||
let collection_file = CollectionFile::Directory(Directory {
|
||||
path: path_string.to_string(),
|
||||
});
|
||||
out.push(collection_file);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue