Project rename
This commit is contained in:
parent
42895931c8
commit
c36c82e087
6 changed files with 71 additions and 71 deletions
24
src/api.rs
24
src/api.rs
|
@ -16,18 +16,18 @@ use url::percent_encoding::percent_decode;
|
|||
use collection::*;
|
||||
use error::*;
|
||||
|
||||
impl From<SwineError> for IronError {
|
||||
fn from(err: SwineError) -> IronError {
|
||||
impl From<PError> for IronError {
|
||||
fn from(err: PError) -> IronError {
|
||||
match err {
|
||||
SwineError::Io(e) => IronError::new(e, status::NotFound),
|
||||
SwineError::PathDecoding => IronError::new(err, status::InternalServerError),
|
||||
SwineError::ConflictingMount => IronError::new(err, status::BadRequest),
|
||||
SwineError::PathNotInVfs => IronError::new(err, status::NotFound),
|
||||
SwineError::CannotServeDirectory => IronError::new(err, status::BadRequest),
|
||||
SwineError::ConfigFileOpenError => IronError::new(err, status::InternalServerError),
|
||||
SwineError::ConfigFileReadError => IronError::new(err, status::InternalServerError),
|
||||
SwineError::ConfigFileParseError => IronError::new(err, status::InternalServerError),
|
||||
SwineError::ConfigMountDirsParseError => IronError::new(err, status::InternalServerError),
|
||||
PError::Io(e) => IronError::new(e, status::NotFound),
|
||||
PError::PathDecoding => IronError::new(err, status::InternalServerError),
|
||||
PError::ConflictingMount => IronError::new(err, status::BadRequest),
|
||||
PError::PathNotInVfs => IronError::new(err, status::NotFound),
|
||||
PError::CannotServeDirectory => IronError::new(err, status::BadRequest),
|
||||
PError::ConfigFileOpenError => IronError::new(err, status::InternalServerError),
|
||||
PError::ConfigFileReadError => IronError::new(err, status::InternalServerError),
|
||||
PError::ConfigFileParseError => 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() {
|
||||
return Err(IronError::new(SwineError::CannotServeDirectory, status::BadRequest));
|
||||
return Err(IronError::new(PError::CannotServeDirectory, status::BadRequest));
|
||||
}
|
||||
|
||||
Ok(Response::with((status::Ok, real_path)))
|
||||
|
|
|
@ -15,14 +15,14 @@ pub struct 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());
|
||||
assert!(file_meta.is_file());
|
||||
|
||||
let file_path = file.path();
|
||||
let file_path = file_path.as_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 = display_name.to_str().unwrap();
|
||||
|
@ -44,14 +44,14 @@ pub struct Directory {
|
|||
impl Directory {
|
||||
pub fn read(collection: &Collection,
|
||||
file: &fs::DirEntry)
|
||||
-> Result<Directory, SwineError> {
|
||||
-> Result<Directory, PError> {
|
||||
let file_meta = try!(file.metadata());
|
||||
assert!(file_meta.is_dir());
|
||||
|
||||
let file_path = file.path();
|
||||
let file_path = file_path.as_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 = display_name.to_str().unwrap();
|
||||
|
@ -83,26 +83,26 @@ impl Collection {
|
|||
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
|
||||
let mut config_file = match File::open(config_path) {
|
||||
Ok(c) => c,
|
||||
Err(_) => return Err(SwineError::ConfigFileOpenError),
|
||||
Err(_) => return Err(PError::ConfigFileOpenError),
|
||||
};
|
||||
|
||||
// Read
|
||||
let mut config_file_content = String::new();
|
||||
match config_file.read_to_string(&mut config_file_content) {
|
||||
Ok(_) => (),
|
||||
Err(_) => return Err(SwineError::ConfigFileReadError),
|
||||
Err(_) => return Err(PError::ConfigFileReadError),
|
||||
};
|
||||
|
||||
// Parse
|
||||
let parsed_config = toml::Parser::new(config_file_content.as_str()).parse();
|
||||
let parsed_config = match parsed_config {
|
||||
Some(c) => c,
|
||||
None => return Err(SwineError::ConfigFileParseError),
|
||||
None => return Err(PError::ConfigFileParseError),
|
||||
};
|
||||
|
||||
// Apply
|
||||
|
@ -111,7 +111,7 @@ impl Collection {
|
|||
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) {
|
||||
Some(s) => s,
|
||||
None => return Ok(()),
|
||||
|
@ -119,25 +119,25 @@ impl Collection {
|
|||
|
||||
let mount_dirs = match mount_dirs {
|
||||
&toml::Value::Array(ref a) => a,
|
||||
_ => return Err(SwineError::ConfigMountDirsParseError),
|
||||
_ => return Err(PError::ConfigMountDirsParseError),
|
||||
};
|
||||
|
||||
for dir in mount_dirs {
|
||||
let name = match dir.lookup(CONFIG_MOUNT_DIR_NAME) {
|
||||
None => return Err(SwineError::ConfigMountDirsParseError),
|
||||
None => return Err(PError::ConfigMountDirsParseError),
|
||||
Some(n) => n,
|
||||
};
|
||||
let name = match name.as_str() {
|
||||
None => return Err(SwineError::ConfigMountDirsParseError),
|
||||
None => return Err(PError::ConfigMountDirsParseError),
|
||||
Some(n) => n,
|
||||
};
|
||||
|
||||
let source = match dir.lookup(CONFIG_MOUNT_DIR_SOURCE) {
|
||||
None => return Err(SwineError::ConfigMountDirsParseError),
|
||||
None => return Err(PError::ConfigMountDirsParseError),
|
||||
Some(n) => n,
|
||||
};
|
||||
let source = match source.as_str() {
|
||||
None => return Err(SwineError::ConfigMountDirsParseError),
|
||||
None => return Err(PError::ConfigMountDirsParseError),
|
||||
Some(n) => n,
|
||||
};
|
||||
let source = PathBuf::from(source);
|
||||
|
@ -148,11 +148,11 @@ impl Collection {
|
|||
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)
|
||||
}
|
||||
|
||||
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));
|
||||
|
||||
|
@ -172,7 +172,7 @@ impl Collection {
|
|||
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));
|
||||
files.fold(Ok(vec![]), |acc, file| {
|
||||
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));
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
|
66
src/error.rs
66
src/error.rs
|
@ -3,7 +3,7 @@ use std::fmt;
|
|||
use std::io;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum SwineError {
|
||||
pub enum PError {
|
||||
PathDecoding,
|
||||
Io(io::Error),
|
||||
ConflictingMount,
|
||||
|
@ -15,68 +15,68 @@ pub enum SwineError {
|
|||
ConfigMountDirsParseError,
|
||||
}
|
||||
|
||||
impl From<io::Error> for SwineError {
|
||||
fn from(err: io::Error) -> SwineError {
|
||||
SwineError::Io(err)
|
||||
impl From<io::Error> for PError {
|
||||
fn from(err: io::Error) -> PError {
|
||||
PError::Io(err)
|
||||
}
|
||||
}
|
||||
|
||||
impl error::Error for SwineError {
|
||||
impl error::Error for PError {
|
||||
fn description(&self) -> &str {
|
||||
match *self {
|
||||
SwineError::Io(ref err) => err.description(),
|
||||
SwineError::PathDecoding => "Error while decoding a Path as a UTF-8 string",
|
||||
SwineError::ConflictingMount => {
|
||||
PError::Io(ref err) => err.description(),
|
||||
PError::PathDecoding => "Error while decoding a Path as a UTF-8 string",
|
||||
PError::ConflictingMount => {
|
||||
"Attempting to mount multiple directories under the same name"
|
||||
}
|
||||
SwineError::PathNotInVfs => "Requested path does not index a mount point",
|
||||
SwineError::CannotServeDirectory => "Only individual files can be served",
|
||||
SwineError::ConfigFileOpenError => "Could not open config file",
|
||||
SwineError::ConfigFileReadError => "Could not read config file",
|
||||
SwineError::ConfigFileParseError => "Could not parse config file",
|
||||
SwineError::ConfigMountDirsParseError => "Could not parse mount directories in config file",
|
||||
PError::PathNotInVfs => "Requested path does not index a mount point",
|
||||
PError::CannotServeDirectory => "Only individual files can be served",
|
||||
PError::ConfigFileOpenError => "Could not open config file",
|
||||
PError::ConfigFileReadError => "Could not read config file",
|
||||
PError::ConfigFileParseError => "Could not parse config file",
|
||||
PError::ConfigMountDirsParseError => "Could not parse mount directories in config file",
|
||||
}
|
||||
}
|
||||
|
||||
fn cause(&self) -> Option<&error::Error> {
|
||||
match *self {
|
||||
SwineError::Io(ref err) => Some(err),
|
||||
SwineError::PathDecoding => None,
|
||||
SwineError::ConflictingMount => None,
|
||||
SwineError::PathNotInVfs => None,
|
||||
SwineError::CannotServeDirectory => None,
|
||||
SwineError::ConfigFileOpenError => None,
|
||||
SwineError::ConfigFileReadError => None,
|
||||
SwineError::ConfigFileParseError => None,
|
||||
SwineError::ConfigMountDirsParseError => None,
|
||||
PError::Io(ref err) => Some(err),
|
||||
PError::PathDecoding => None,
|
||||
PError::ConflictingMount => None,
|
||||
PError::PathNotInVfs => None,
|
||||
PError::CannotServeDirectory => None,
|
||||
PError::ConfigFileOpenError => None,
|
||||
PError::ConfigFileReadError => None,
|
||||
PError::ConfigFileParseError => None,
|
||||
PError::ConfigMountDirsParseError => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for SwineError {
|
||||
impl fmt::Display for PError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match *self {
|
||||
SwineError::Io(ref err) => write!(f, "IO error: {}", err),
|
||||
SwineError::PathDecoding => write!(f, "Path decoding error"),
|
||||
SwineError::ConflictingMount => {
|
||||
PError::Io(ref err) => write!(f, "IO error: {}", err),
|
||||
PError::PathDecoding => write!(f, "Path decoding error"),
|
||||
PError::ConflictingMount => {
|
||||
write!(f, "Mount point already has a target directory")
|
||||
}
|
||||
SwineError::PathNotInVfs => {
|
||||
PError::PathNotInVfs => {
|
||||
write!(f, "Requested path does not index a mount point")
|
||||
}
|
||||
SwineError::CannotServeDirectory => {
|
||||
PError::CannotServeDirectory => {
|
||||
write!(f, "Only individual files can be served")
|
||||
}
|
||||
SwineError::ConfigFileOpenError => {
|
||||
PError::ConfigFileOpenError => {
|
||||
write!(f, "Could not open config file")
|
||||
}
|
||||
SwineError::ConfigFileReadError => {
|
||||
PError::ConfigFileReadError => {
|
||||
write!(f, "Could not read config file")
|
||||
}
|
||||
SwineError::ConfigFileParseError => {
|
||||
PError::ConfigFileParseError => {
|
||||
write!(f, "Could not parse config file")
|
||||
}
|
||||
SwineError::ConfigMountDirsParseError => {
|
||||
PError::ConfigMountDirsParseError => {
|
||||
write!(f, "Could not parse mount directories in config file")
|
||||
}
|
||||
}
|
||||
|
|
|
@ -23,7 +23,7 @@ use collection::*;
|
|||
fn main() {
|
||||
|
||||
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));
|
||||
|
||||
|
|
12
src/vfs.rs
12
src/vfs.rs
|
@ -14,16 +14,16 @@ impl Vfs {
|
|||
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();
|
||||
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());
|
||||
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 {
|
||||
match real_path.strip_prefix(target) {
|
||||
Ok(p) => {
|
||||
|
@ -33,10 +33,10 @@ impl Vfs {
|
|||
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 {
|
||||
let mount_path = Path::new(&name);
|
||||
match virtual_path.strip_prefix(mount_path) {
|
||||
|
@ -44,7 +44,7 @@ impl Vfs {
|
|||
Err(_) => (),
|
||||
}
|
||||
}
|
||||
Err(SwineError::PathNotInVfs)
|
||||
Err(PError::PathNotInVfs)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue