From bdc33a29aa4c9ad32cf7fa08192b2748b6f9da18 Mon Sep 17 00:00:00 2001 From: Antoine Gersant Date: Sat, 1 Jul 2017 13:09:51 -0700 Subject: [PATCH] Cosmetic changes --- src/api.rs | 3 ++- src/db/mod.rs | 7 +++---- src/index.rs | 46 +++++++++++++++++++++++++++++++--------------- src/user.rs | 4 +++- src/vfs.rs | 16 ++++++++-------- 5 files changed, 47 insertions(+), 29 deletions(-) diff --git a/src/api.rs b/src/api.rs index 877986e..80ddf2a 100644 --- a/src/api.rs +++ b/src/api.rs @@ -152,7 +152,8 @@ impl Handler for AuthHandler { // Auth via Authorization header if let Some(auth) = req.headers.get::>() { if let Some(ref password) = auth.password { - auth_success = user::auth(self.db.deref(), auth.username.as_str(), password.as_str())?; + auth_success = + user::auth(self.db.deref(), auth.username.as_str(), password.as_str())?; req.extensions .insert::(Session { username: auth.username.clone() }); } diff --git a/src/db/mod.rs b/src/db/mod.rs index ab9fcb4..61c9341 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -11,7 +11,7 @@ use ddns::{DDNSConfigSource, DDNSConfig}; use errors::*; use index; use user::*; -use vfs::{MountPoint, Vfs, VFSSource}; +use vfs::{MountPoint, VFS, VFSSource}; mod schema; @@ -138,9 +138,9 @@ impl DDNSConfigSource for DB { } impl VFSSource for DB { - fn get_vfs(&self) -> Result { + fn get_vfs(&self) -> Result { use self::mount_points::dsl::*; - let mut vfs = Vfs::new(); + let mut vfs = VFS::new(); let connection = self.connection.lock().unwrap(); let connection = connection.deref(); let points: Vec = mount_points @@ -180,4 +180,3 @@ fn test_migrations_down() { db.migrate_down().unwrap(); db.migrate_up().unwrap(); } - diff --git a/src/index.rs b/src/index.rs index 33c46a5..49ed04c 100644 --- a/src/index.rs +++ b/src/index.rs @@ -15,7 +15,7 @@ use config::{MiscSettings, UserConfig}; use db::ConnectionSource; use db::DB; use db::{directories, misc_settings, songs}; -use vfs::{Vfs, VFSSource}; +use vfs::{VFS, VFSSource}; use errors::*; use metadata; @@ -281,7 +281,9 @@ impl<'conn> IndexBuilder<'conn> { } } -fn clean(db: &T) -> Result<()> where T: ConnectionSource + VFSSource { +fn clean(db: &T) -> Result<()> + where T: ConnectionSource + VFSSource +{ let vfs = db.get_vfs()?; { @@ -346,7 +348,9 @@ fn clean(db: &T) -> Result<()> where T: ConnectionSource + VFSSource { Ok(()) } -fn populate(db: &T) -> Result<()> where T: ConnectionSource + VFSSource { +fn populate(db: &T) -> Result<()> + where T: ConnectionSource + VFSSource +{ let vfs = db.get_vfs()?; let mount_points = vfs.get_mount_points(); let connection = db.get_connection(); @@ -368,7 +372,9 @@ fn populate(db: &T) -> Result<()> where T: ConnectionSource + VFSSource { Ok(()) } -pub fn update(db: &T) -> Result<()> where T: ConnectionSource + VFSSource { +pub fn update(db: &T) -> Result<()> + where T: ConnectionSource + VFSSource +{ let start = time::Instant::now(); println!("Beginning library index update"); clean(db)?; @@ -378,7 +384,9 @@ pub fn update(db: &T) -> Result<()> where T: ConnectionSource + VFSSource { Ok(()) } -pub fn update_loop(db: &T) where T: ConnectionSource + VFSSource { +pub fn update_loop(db: &T) + where T: ConnectionSource + VFSSource +{ loop { if let Err(e) = update(db) { println!("Error while updating index: {}", e); @@ -404,7 +412,7 @@ pub fn update_loop(db: &T) where T: ConnectionSource + VFSSource { } } -fn virtualize_song(vfs: &Vfs, mut song: Song) -> Option { +fn virtualize_song(vfs: &VFS, mut song: Song) -> Option { song.path = match vfs.real_to_virtual(Path::new(&song.path)) { Ok(p) => p.to_string_lossy().into_owned(), _ => return None, @@ -418,7 +426,7 @@ fn virtualize_song(vfs: &Vfs, mut song: Song) -> Option { Some(song) } -fn virtualize_directory(vfs: &Vfs, mut directory: Directory) -> Option { +fn virtualize_directory(vfs: &VFS, mut directory: Directory) -> Option { directory.path = match vfs.real_to_virtual(Path::new(&directory.path)) { Ok(p) => p.to_string_lossy().into_owned(), _ => return None, @@ -432,7 +440,9 @@ fn virtualize_directory(vfs: &Vfs, mut directory: Directory) -> Option(db: &T, virtual_path: &Path) -> Result> where T: ConnectionSource + VFSSource { +pub fn browse(db: &T, virtual_path: &Path) -> Result> + where T: ConnectionSource + VFSSource +{ let mut output = Vec::new(); let vfs = db.get_vfs()?; let connection = db.get_connection(); @@ -448,8 +458,8 @@ pub fn browse(db: &T, virtual_path: &Path) -> Result> whe .into_iter() .filter_map(|s| virtualize_directory(&vfs, s)); output.extend(virtual_directories - .into_iter() - .map(|d| CollectionFile::Directory(d))); + .into_iter() + .map(|d| CollectionFile::Directory(d))); } else { // Browse sub-directory @@ -478,7 +488,9 @@ pub fn browse(db: &T, virtual_path: &Path) -> Result> whe Ok(output) } -pub fn flatten(db: &T, virtual_path: &Path) -> Result> where T: ConnectionSource + VFSSource { +pub fn flatten(db: &T, virtual_path: &Path) -> Result> + where T: ConnectionSource + VFSSource +{ use self::songs::dsl::*; let vfs = db.get_vfs()?; let connection = db.get_connection(); @@ -493,7 +505,9 @@ pub fn flatten(db: &T, virtual_path: &Path) -> Result> where T: Con Ok(virtual_songs.collect::>()) } -pub fn get_random_albums(db: &T, count: i64) -> Result> where T: ConnectionSource + VFSSource { +pub fn get_random_albums(db: &T, count: i64) -> Result> + where T: ConnectionSource + VFSSource +{ use self::directories::dsl::*; let vfs = db.get_vfs()?; let connection = db.get_connection(); @@ -510,7 +524,9 @@ pub fn get_random_albums(db: &T, count: i64) -> Result> where Ok(virtual_directories.collect::>()) } -pub fn get_recent_albums(db: &T, count: i64) -> Result> where T: ConnectionSource + VFSSource { +pub fn get_recent_albums(db: &T, count: i64) -> Result> + where T: ConnectionSource + VFSSource +{ use self::directories::dsl::*; let vfs = db.get_vfs()?; let connection = db.get_connection(); @@ -527,7 +543,7 @@ pub fn get_recent_albums(db: &T, count: i64) -> Result> where Ok(virtual_directories.collect::>()) } -fn _get_test_db(name: &str) -> DB { +fn _get_test_db(name: &str) -> DB { let config_path = Path::new("test/config.toml"); let config = UserConfig::parse(&config_path).unwrap(); @@ -661,4 +677,4 @@ fn test_recent() { let results = get_recent_albums(&db, 2).unwrap(); assert_eq!(results.len(), 2); assert!(results[0].date_added >= results[1].date_added); -} \ No newline at end of file +} diff --git a/src/user.rs b/src/user.rs index 66c42a3..547d819 100644 --- a/src/user.rs +++ b/src/user.rs @@ -61,7 +61,9 @@ impl NewUser { } } -pub fn auth(db: &T, username: &str, password: &str) -> Result where T: ConnectionSource { +pub fn auth(db: &T, username: &str, password: &str) -> Result + where T: ConnectionSource +{ use db::users::dsl::*; let connection = db.get_connection(); let connection = connection.lock().unwrap(); diff --git a/src/vfs.rs b/src/vfs.rs index 5457c5d..54b43bb 100644 --- a/src/vfs.rs +++ b/src/vfs.rs @@ -6,7 +6,7 @@ use db::mount_points; use errors::*; pub trait VFSSource { - fn get_vfs(&self) -> Result; + fn get_vfs(&self) -> Result; } #[derive(Debug, Deserialize, Insertable, Queryable)] @@ -16,13 +16,13 @@ pub struct MountPoint { pub name: String, } -pub struct Vfs { +pub struct VFS { mount_points: HashMap, } -impl Vfs { - pub fn new() -> Vfs { - Vfs { mount_points: HashMap::new() } +impl VFS { + pub fn new() -> VFS { + VFS { mount_points: HashMap::new() } } pub fn mount(&mut self, real_path: &Path, name: &str) -> Result<()> { @@ -72,7 +72,7 @@ impl Vfs { #[test] fn test_virtual_to_real() { - let mut vfs = Vfs::new(); + let mut vfs = VFS::new(); vfs.mount(Path::new("test_dir"), "root").unwrap(); let mut correct_path = PathBuf::new(); @@ -91,7 +91,7 @@ fn test_virtual_to_real() { #[test] fn test_virtual_to_real_no_trail() { - let mut vfs = Vfs::new(); + let mut vfs = VFS::new(); vfs.mount(Path::new("test_dir"), "root").unwrap(); let correct_path = Path::new("test_dir"); let found_path = vfs.virtual_to_real(Path::new("root")).unwrap(); @@ -100,7 +100,7 @@ fn test_virtual_to_real_no_trail() { #[test] fn test_real_to_virtual() { - let mut vfs = Vfs::new(); + let mut vfs = VFS::new(); vfs.mount(Path::new("test_dir"), "root").unwrap(); let mut correct_path = PathBuf::new();