Extracted CollectionError to submodule

This commit is contained in:
Antoine Gersant 2016-08-19 00:13:59 -07:00
parent 474710a6a9
commit f067a1c3d1
3 changed files with 63 additions and 20 deletions

View file

@ -9,11 +9,13 @@ use rustc_serialize::json;
use url::percent_encoding::percent_decode; use url::percent_encoding::percent_decode;
use collection; use collection;
use collection::CollectionError;
impl From<collection::CollectionError> for IronError { impl From<CollectionError> for IronError {
fn from(err: collection::CollectionError) -> IronError { fn from(err: CollectionError) -> IronError {
match err { 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
View 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"),
}
}
}

View file

@ -1,14 +1,19 @@
use std::fs; use std::fs;
use std::io;
use std::path::Path; use std::path::Path;
use std::path::PathBuf;
pub use self::error::CollectionError;
mod error;
#[derive(Debug, RustcEncodable)] #[derive(Debug, RustcEncodable)]
pub struct Song(PathBuf); pub struct Song {
path: String,
}
#[derive(Debug, RustcEncodable)] #[derive(Debug, RustcEncodable)]
pub struct Directory(PathBuf); pub struct Directory {
path: String,
}
#[derive(Debug, RustcEncodable)] #[derive(Debug, RustcEncodable)]
pub enum CollectionFile { pub enum CollectionFile {
@ -16,17 +21,6 @@ pub enum CollectionFile {
Song(Song), 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> { pub fn browse(path: &Path) -> Result<Vec<CollectionFile>, CollectionError> {
let full_path = "samplemusic/".to_string() + path.to_str().unwrap(); // TMP use mount directories 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_meta = try!(file.metadata());
let file_path = file.path().to_owned(); let file_path = file.path().to_owned();
if file_meta.is_file() { 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); out.push(collection_file);
} else if file_meta.is_dir() { } 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); out.push(collection_file);
} }
} }