From f067a1c3d14f6523ad5a25bf62ee48fda2eb412a Mon Sep 17 00:00:00 2001 From: Antoine Gersant Date: Fri, 19 Aug 2016 00:13:59 -0700 Subject: [PATCH] Extracted CollectionError to submodule --- src/api/mod.rs | 8 +++++--- src/collection/error.rs | 41 +++++++++++++++++++++++++++++++++++++++++ src/collection/mod.rs | 34 +++++++++++++++++----------------- 3 files changed, 63 insertions(+), 20 deletions(-) create mode 100644 src/collection/error.rs diff --git a/src/api/mod.rs b/src/api/mod.rs index 20f2d0b..14b7358 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -9,11 +9,13 @@ use rustc_serialize::json; use url::percent_encoding::percent_decode; use collection; +use collection::CollectionError; -impl From for IronError { - fn from(err: collection::CollectionError) -> IronError { +impl From 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) } } } diff --git a/src/collection/error.rs b/src/collection/error.rs new file mode 100644 index 0000000..a1c54d7 --- /dev/null +++ b/src/collection/error.rs @@ -0,0 +1,41 @@ +use std::error; +use std::fmt; +use std::io; + +#[derive(Debug)] +pub enum CollectionError +{ + PathDecoding, + Io(io::Error), +} + +impl From 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"), + } + } +} \ No newline at end of file diff --git a/src/collection/mod.rs b/src/collection/mod.rs index c21eddd..4ac1d98 100644 --- a/src/collection/mod.rs +++ b/src/collection/mod.rs @@ -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 for CollectionError { - fn from(err: io::Error) -> CollectionError { - CollectionError::Io(err) - } -} - pub fn browse(path: &Path) -> Result, 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, 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); } }