Project rename

This commit is contained in:
Antoine Gersant 2016-08-28 16:39:31 -07:00
parent 42895931c8
commit c36c82e087
6 changed files with 71 additions and 71 deletions

View file

@ -16,18 +16,18 @@ use url::percent_encoding::percent_decode;
use collection::*; use collection::*;
use error::*; use error::*;
impl From<SwineError> for IronError { impl From<PError> for IronError {
fn from(err: SwineError) -> IronError { fn from(err: PError) -> IronError {
match err { match err {
SwineError::Io(e) => IronError::new(e, status::NotFound), PError::Io(e) => IronError::new(e, status::NotFound),
SwineError::PathDecoding => IronError::new(err, status::InternalServerError), PError::PathDecoding => IronError::new(err, status::InternalServerError),
SwineError::ConflictingMount => IronError::new(err, status::BadRequest), PError::ConflictingMount => IronError::new(err, status::BadRequest),
SwineError::PathNotInVfs => IronError::new(err, status::NotFound), PError::PathNotInVfs => IronError::new(err, status::NotFound),
SwineError::CannotServeDirectory => IronError::new(err, status::BadRequest), PError::CannotServeDirectory => IronError::new(err, status::BadRequest),
SwineError::ConfigFileOpenError => IronError::new(err, status::InternalServerError), PError::ConfigFileOpenError => IronError::new(err, status::InternalServerError),
SwineError::ConfigFileReadError => IronError::new(err, status::InternalServerError), PError::ConfigFileReadError => IronError::new(err, status::InternalServerError),
SwineError::ConfigFileParseError => IronError::new(err, status::InternalServerError), PError::ConfigFileParseError => IronError::new(err, status::InternalServerError),
SwineError::ConfigMountDirsParseError => IronError::new(err, status::InternalServerError), PError::ConfigMountDirsParseError => IronError::new(err, status::InternalServerError),
} }
} }
} }
@ -124,7 +124,7 @@ fn serve(request: &mut Request, collection: &mut Collection) -> IronResult<Respo
}; };
if !metadata.is_file() { if !metadata.is_file() {
return Err(IronError::new(SwineError::CannotServeDirectory, status::BadRequest)); return Err(IronError::new(PError::CannotServeDirectory, status::BadRequest));
} }
Ok(Response::with((status::Ok, real_path))) Ok(Response::with((status::Ok, real_path)))

View file

@ -15,14 +15,14 @@ pub struct Song {
} }
impl Song { impl Song {
pub fn read(collection: &Collection, file: &fs::DirEntry) -> Result<Song, SwineError> { pub fn read(collection: &Collection, file: &fs::DirEntry) -> Result<Song, PError> {
let file_meta = try!(file.metadata()); let file_meta = try!(file.metadata());
assert!(file_meta.is_file()); assert!(file_meta.is_file());
let file_path = file.path(); let file_path = file.path();
let file_path = file_path.as_path(); let file_path = file_path.as_path();
let virtual_path = try!(collection.vfs.real_to_virtual(file_path)); let virtual_path = try!(collection.vfs.real_to_virtual(file_path));
let path_string = try!(virtual_path.to_str().ok_or(SwineError::PathDecoding)); let path_string = try!(virtual_path.to_str().ok_or(PError::PathDecoding));
let display_name = virtual_path.file_stem().unwrap(); let display_name = virtual_path.file_stem().unwrap();
let display_name = display_name.to_str().unwrap(); let display_name = display_name.to_str().unwrap();
@ -44,14 +44,14 @@ pub struct Directory {
impl Directory { impl Directory {
pub fn read(collection: &Collection, pub fn read(collection: &Collection,
file: &fs::DirEntry) file: &fs::DirEntry)
-> Result<Directory, SwineError> { -> Result<Directory, PError> {
let file_meta = try!(file.metadata()); let file_meta = try!(file.metadata());
assert!(file_meta.is_dir()); assert!(file_meta.is_dir());
let file_path = file.path(); let file_path = file.path();
let file_path = file_path.as_path(); let file_path = file_path.as_path();
let virtual_path = try!(collection.vfs.real_to_virtual(file_path)); let virtual_path = try!(collection.vfs.real_to_virtual(file_path));
let path_string = try!(virtual_path.to_str().ok_or(SwineError::PathDecoding)); let path_string = try!(virtual_path.to_str().ok_or(PError::PathDecoding));
let display_name = virtual_path.iter().last().unwrap(); let display_name = virtual_path.iter().last().unwrap();
let display_name = display_name.to_str().unwrap(); let display_name = display_name.to_str().unwrap();
@ -83,26 +83,26 @@ impl Collection {
Collection { vfs: Vfs::new() } Collection { vfs: Vfs::new() }
} }
pub fn load_config(&mut self, config_path: &Path) -> Result<(), SwineError> pub fn load_config(&mut self, config_path: &Path) -> Result<(), PError>
{ {
// Open // Open
let mut config_file = match File::open(config_path) { let mut config_file = match File::open(config_path) {
Ok(c) => c, Ok(c) => c,
Err(_) => return Err(SwineError::ConfigFileOpenError), Err(_) => return Err(PError::ConfigFileOpenError),
}; };
// Read // Read
let mut config_file_content = String::new(); let mut config_file_content = String::new();
match config_file.read_to_string(&mut config_file_content) { match config_file.read_to_string(&mut config_file_content) {
Ok(_) => (), Ok(_) => (),
Err(_) => return Err(SwineError::ConfigFileReadError), Err(_) => return Err(PError::ConfigFileReadError),
}; };
// Parse // Parse
let parsed_config = toml::Parser::new(config_file_content.as_str()).parse(); let parsed_config = toml::Parser::new(config_file_content.as_str()).parse();
let parsed_config = match parsed_config { let parsed_config = match parsed_config {
Some(c) => c, Some(c) => c,
None => return Err(SwineError::ConfigFileParseError), None => return Err(PError::ConfigFileParseError),
}; };
// Apply // Apply
@ -111,7 +111,7 @@ impl Collection {
Ok(()) Ok(())
} }
fn load_config_mount_points(&mut self, config: &toml::Table) -> Result<(), SwineError> { fn load_config_mount_points(&mut self, config: &toml::Table) -> Result<(), PError> {
let mount_dirs = match config.get(CONFIG_MOUNT_DIRS) { let mount_dirs = match config.get(CONFIG_MOUNT_DIRS) {
Some(s) => s, Some(s) => s,
None => return Ok(()), None => return Ok(()),
@ -119,25 +119,25 @@ impl Collection {
let mount_dirs = match mount_dirs { let mount_dirs = match mount_dirs {
&toml::Value::Array(ref a) => a, &toml::Value::Array(ref a) => a,
_ => return Err(SwineError::ConfigMountDirsParseError), _ => return Err(PError::ConfigMountDirsParseError),
}; };
for dir in mount_dirs { for dir in mount_dirs {
let name = match dir.lookup(CONFIG_MOUNT_DIR_NAME) { let name = match dir.lookup(CONFIG_MOUNT_DIR_NAME) {
None => return Err(SwineError::ConfigMountDirsParseError), None => return Err(PError::ConfigMountDirsParseError),
Some(n) => n, Some(n) => n,
}; };
let name = match name.as_str() { let name = match name.as_str() {
None => return Err(SwineError::ConfigMountDirsParseError), None => return Err(PError::ConfigMountDirsParseError),
Some(n) => n, Some(n) => n,
}; };
let source = match dir.lookup(CONFIG_MOUNT_DIR_SOURCE) { let source = match dir.lookup(CONFIG_MOUNT_DIR_SOURCE) {
None => return Err(SwineError::ConfigMountDirsParseError), None => return Err(PError::ConfigMountDirsParseError),
Some(n) => n, Some(n) => n,
}; };
let source = match source.as_str() { let source = match source.as_str() {
None => return Err(SwineError::ConfigMountDirsParseError), None => return Err(PError::ConfigMountDirsParseError),
Some(n) => n, Some(n) => n,
}; };
let source = PathBuf::from(source); let source = PathBuf::from(source);
@ -148,11 +148,11 @@ impl Collection {
Ok(()) Ok(())
} }
pub fn mount(&mut self, name: &str, real_path: &Path) -> Result<(), SwineError> { pub fn mount(&mut self, name: &str, real_path: &Path) -> Result<(), PError> {
self.vfs.mount(name, real_path) self.vfs.mount(name, real_path)
} }
pub fn browse(&self, path: &Path) -> Result<Vec<CollectionFile>, SwineError> { pub fn browse(&self, path: &Path) -> Result<Vec<CollectionFile>, PError> {
let full_path = try!(self.vfs.virtual_to_real(path)); let full_path = try!(self.vfs.virtual_to_real(path));
@ -172,7 +172,7 @@ impl Collection {
Ok(out) Ok(out)
} }
fn flatten_internal(&self, path: &Path) -> Result<Vec<Song>, SwineError> { fn flatten_internal(&self, path: &Path) -> Result<Vec<Song>, PError> {
let files = try!(fs::read_dir(path)); let files = try!(fs::read_dir(path));
files.fold(Ok(vec![]), |acc, file| { files.fold(Ok(vec![]), |acc, file| {
let mut acc = try!(acc); let mut acc = try!(acc);
@ -191,12 +191,12 @@ impl Collection {
}) })
} }
pub fn flatten(&self, path: &Path) -> Result<Vec<Song>, SwineError> { pub fn flatten(&self, path: &Path) -> Result<Vec<Song>, PError> {
let real_path = try!(self.vfs.virtual_to_real(path)); let real_path = try!(self.vfs.virtual_to_real(path));
self.flatten_internal(real_path.as_path()) self.flatten_internal(real_path.as_path())
} }
pub fn locate(&self, virtual_path: &Path) -> Result<PathBuf, SwineError> { pub fn locate(&self, virtual_path: &Path) -> Result<PathBuf, PError> {
self.vfs.virtual_to_real(virtual_path) self.vfs.virtual_to_real(virtual_path)
} }
} }

View file

@ -3,7 +3,7 @@ use std::fmt;
use std::io; use std::io;
#[derive(Debug)] #[derive(Debug)]
pub enum SwineError { pub enum PError {
PathDecoding, PathDecoding,
Io(io::Error), Io(io::Error),
ConflictingMount, ConflictingMount,
@ -15,68 +15,68 @@ pub enum SwineError {
ConfigMountDirsParseError, ConfigMountDirsParseError,
} }
impl From<io::Error> for SwineError { impl From<io::Error> for PError {
fn from(err: io::Error) -> SwineError { fn from(err: io::Error) -> PError {
SwineError::Io(err) PError::Io(err)
} }
} }
impl error::Error for SwineError { impl error::Error for PError {
fn description(&self) -> &str { fn description(&self) -> &str {
match *self { match *self {
SwineError::Io(ref err) => err.description(), PError::Io(ref err) => err.description(),
SwineError::PathDecoding => "Error while decoding a Path as a UTF-8 string", PError::PathDecoding => "Error while decoding a Path as a UTF-8 string",
SwineError::ConflictingMount => { PError::ConflictingMount => {
"Attempting to mount multiple directories under the same name" "Attempting to mount multiple directories under the same name"
} }
SwineError::PathNotInVfs => "Requested path does not index a mount point", PError::PathNotInVfs => "Requested path does not index a mount point",
SwineError::CannotServeDirectory => "Only individual files can be served", PError::CannotServeDirectory => "Only individual files can be served",
SwineError::ConfigFileOpenError => "Could not open config file", PError::ConfigFileOpenError => "Could not open config file",
SwineError::ConfigFileReadError => "Could not read config file", PError::ConfigFileReadError => "Could not read config file",
SwineError::ConfigFileParseError => "Could not parse config file", PError::ConfigFileParseError => "Could not parse config file",
SwineError::ConfigMountDirsParseError => "Could not parse mount directories in config file", PError::ConfigMountDirsParseError => "Could not parse mount directories in config file",
} }
} }
fn cause(&self) -> Option<&error::Error> { fn cause(&self) -> Option<&error::Error> {
match *self { match *self {
SwineError::Io(ref err) => Some(err), PError::Io(ref err) => Some(err),
SwineError::PathDecoding => None, PError::PathDecoding => None,
SwineError::ConflictingMount => None, PError::ConflictingMount => None,
SwineError::PathNotInVfs => None, PError::PathNotInVfs => None,
SwineError::CannotServeDirectory => None, PError::CannotServeDirectory => None,
SwineError::ConfigFileOpenError => None, PError::ConfigFileOpenError => None,
SwineError::ConfigFileReadError => None, PError::ConfigFileReadError => None,
SwineError::ConfigFileParseError => None, PError::ConfigFileParseError => None,
SwineError::ConfigMountDirsParseError => None, PError::ConfigMountDirsParseError => None,
} }
} }
} }
impl fmt::Display for SwineError { impl fmt::Display for PError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self { match *self {
SwineError::Io(ref err) => write!(f, "IO error: {}", err), PError::Io(ref err) => write!(f, "IO error: {}", err),
SwineError::PathDecoding => write!(f, "Path decoding error"), PError::PathDecoding => write!(f, "Path decoding error"),
SwineError::ConflictingMount => { PError::ConflictingMount => {
write!(f, "Mount point already has a target directory") write!(f, "Mount point already has a target directory")
} }
SwineError::PathNotInVfs => { PError::PathNotInVfs => {
write!(f, "Requested path does not index a mount point") write!(f, "Requested path does not index a mount point")
} }
SwineError::CannotServeDirectory => { PError::CannotServeDirectory => {
write!(f, "Only individual files can be served") write!(f, "Only individual files can be served")
} }
SwineError::ConfigFileOpenError => { PError::ConfigFileOpenError => {
write!(f, "Could not open config file") write!(f, "Could not open config file")
} }
SwineError::ConfigFileReadError => { PError::ConfigFileReadError => {
write!(f, "Could not read config file") write!(f, "Could not read config file")
} }
SwineError::ConfigFileParseError => { PError::ConfigFileParseError => {
write!(f, "Could not parse config file") write!(f, "Could not parse config file")
} }
SwineError::ConfigMountDirsParseError => { PError::ConfigMountDirsParseError => {
write!(f, "Could not parse mount directories in config file") write!(f, "Could not parse mount directories in config file")
} }
} }

View file

@ -23,7 +23,7 @@ use collection::*;
fn main() { fn main() {
let mut collection = Collection::new(); let mut collection = Collection::new();
collection.load_config(Path::new("Swine.toml")).unwrap(); collection.load_config(Path::new("Polaris.toml")).unwrap();
let collection = Arc::new(Mutex::new(collection)); let collection = Arc::new(Mutex::new(collection));

View file

@ -14,16 +14,16 @@ impl Vfs {
instance instance
} }
pub fn mount(&mut self, name: &str, real_path: &Path) -> Result<(), SwineError> { pub fn mount(&mut self, name: &str, real_path: &Path) -> Result<(), PError> {
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(SwineError::ConflictingMount); return Err(PError::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, SwineError> { pub fn real_to_virtual(&self, real_path: &Path) -> Result<PathBuf, PError> {
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) => {
@ -33,10 +33,10 @@ impl Vfs {
Err(_) => (), Err(_) => (),
} }
} }
Err(SwineError::PathNotInVfs) Err(PError::PathNotInVfs)
} }
pub fn virtual_to_real(&self, virtual_path: &Path) -> Result<PathBuf, SwineError> { pub fn virtual_to_real(&self, virtual_path: &Path) -> Result<PathBuf, PError> {
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) {
@ -44,7 +44,7 @@ impl Vfs {
Err(_) => (), Err(_) => (),
} }
} }
Err(SwineError::PathNotInVfs) Err(PError::PathNotInVfs)
} }
} }