From 186e3173cd3cb9ef4f84768d4f712c11119cc15a Mon Sep 17 00:00:00 2001 From: Antoine Gersant Date: Fri, 31 Jan 2020 19:16:55 -0800 Subject: [PATCH] Formatting --- src/db/mod.rs | 13 ++-- src/db/schema.rs | 142 ++++++++++++++++++------------------- src/index/mod.rs | 6 +- src/index/test.rs | 2 - src/index/types.rs | 2 +- src/index/update.rs | 91 ++++++++++++++---------- src/main.rs | 4 +- src/service/rocket/api.rs | 5 +- src/service/rocket/test.rs | 4 +- 9 files changed, 140 insertions(+), 129 deletions(-) diff --git a/src/db/mod.rs b/src/db/mod.rs index e59695d..3c92e8a 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -21,15 +21,20 @@ pub struct DB { #[derive(Debug)] struct ConnectionCustomizer {} -impl diesel::r2d2::CustomizeConnection for ConnectionCustomizer { +impl diesel::r2d2::CustomizeConnection + for ConnectionCustomizer +{ fn on_acquire(&self, connection: &mut SqliteConnection) -> Result<(), diesel::r2d2::Error> { - let query = diesel::sql_query(r#" + let query = diesel::sql_query( + r#" PRAGMA busy_timeout = 60000; PRAGMA journal_mode = WAL; PRAGMA synchronous = NORMAL; PRAGMA foreign_keys = ON; - "#); - query.execute(connection) + "#, + ); + query + .execute(connection) .map_err(|e| diesel::r2d2::Error::QueryError(e))?; Ok(()) } diff --git a/src/db/schema.rs b/src/db/schema.rs index ed63c4e..430fd6c 100644 --- a/src/db/schema.rs +++ b/src/db/schema.rs @@ -1,100 +1,100 @@ table! { - ddns_config (id) { - id -> Integer, - host -> Text, - username -> Text, - password -> Text, - } + ddns_config (id) { + id -> Integer, + host -> Text, + username -> Text, + password -> Text, + } } table! { - directories (id) { - id -> Integer, - path -> Text, - parent -> Nullable, - artist -> Nullable, - year -> Nullable, - album -> Nullable, - artwork -> Nullable, - date_added -> Integer, - } + directories (id) { + id -> Integer, + path -> Text, + parent -> Nullable, + artist -> Nullable, + year -> Nullable, + album -> Nullable, + artwork -> Nullable, + date_added -> Integer, + } } table! { - misc_settings (id) { - id -> Integer, - auth_secret -> Binary, - index_sleep_duration_seconds -> Integer, - index_album_art_pattern -> Text, - prefix_url -> Text, - } + misc_settings (id) { + id -> Integer, + auth_secret -> Binary, + index_sleep_duration_seconds -> Integer, + index_album_art_pattern -> Text, + prefix_url -> Text, + } } table! { - mount_points (id) { - id -> Integer, - source -> Text, - name -> Text, - } + mount_points (id) { + id -> Integer, + source -> Text, + name -> Text, + } } table! { - playlist_songs (id) { - id -> Integer, - playlist -> Integer, - path -> Text, - ordering -> Integer, - } + playlist_songs (id) { + id -> Integer, + playlist -> Integer, + path -> Text, + ordering -> Integer, + } } table! { - playlists (id) { - id -> Integer, - owner -> Integer, - name -> Text, - } + playlists (id) { + id -> Integer, + owner -> Integer, + name -> Text, + } } table! { - songs (id) { - id -> Integer, - path -> Text, - parent -> Text, - track_number -> Nullable, - disc_number -> Nullable, - title -> Nullable, - artist -> Nullable, - album_artist -> Nullable, - year -> Nullable, - album -> Nullable, - artwork -> Nullable, - duration -> Nullable, - } + songs (id) { + id -> Integer, + path -> Text, + parent -> Text, + track_number -> Nullable, + disc_number -> Nullable, + title -> Nullable, + artist -> Nullable, + album_artist -> Nullable, + year -> Nullable, + album -> Nullable, + artwork -> Nullable, + duration -> Nullable, + } } table! { - users (id) { - id -> Integer, - name -> Text, - password_hash -> Text, - admin -> Integer, - lastfm_username -> Nullable, - lastfm_session_key -> Nullable, - web_theme_base -> Nullable, - web_theme_accent -> Nullable, - } + users (id) { + id -> Integer, + name -> Text, + password_hash -> Text, + admin -> Integer, + lastfm_username -> Nullable, + lastfm_session_key -> Nullable, + web_theme_base -> Nullable, + web_theme_accent -> Nullable, + } } joinable!(playlist_songs -> playlists (playlist)); joinable!(playlists -> users (owner)); allow_tables_to_appear_in_same_query!( - ddns_config, - directories, - misc_settings, - mount_points, - playlist_songs, - playlists, - songs, - users, + ddns_config, + directories, + misc_settings, + mount_points, + playlist_songs, + playlists, + songs, + users, ); diff --git a/src/index/mod.rs b/src/index/mod.rs index 5c5e99b..a793748 100644 --- a/src/index/mod.rs +++ b/src/index/mod.rs @@ -4,11 +4,11 @@ use diesel::prelude::*; #[cfg(feature = "profile-index")] use flame; use log::error; -use std::sync::{Arc, Mutex, Condvar}; +use std::sync::{Arc, Condvar, Mutex}; use std::time; -use crate::db::{misc_settings, DB}; use crate::config::MiscSettings; +use crate::db::{misc_settings, DB}; use crate::vfs::VFS; mod metadata; @@ -18,9 +18,9 @@ mod test; mod types; mod update; -pub use self::update::*; pub use self::query::*; pub use self::types::*; +pub use self::update::*; pub fn builder(db: DB) -> IndexBuilder { IndexBuilder { diff --git a/src/index/test.rs b/src/index/test.rs index a347af9..3645b4c 100644 --- a/src/index/test.rs +++ b/src/index/test.rs @@ -1,7 +1,5 @@ - use std::path::{Path, PathBuf}; - use crate::db; use crate::db::{directories, songs}; use crate::index::*; diff --git a/src/index/types.rs b/src/index/types.rs index 6852d4e..df2b855 100644 --- a/src/index/types.rs +++ b/src/index/types.rs @@ -39,4 +39,4 @@ pub struct Directory { pub enum CollectionFile { Directory(Directory), Song(Song), -} \ No newline at end of file +} diff --git a/src/index/update.rs b/src/index/update.rs index e97f90c..77571ac 100644 --- a/src/index/update.rs +++ b/src/index/update.rs @@ -8,8 +8,8 @@ use rayon::prelude::*; use regex::Regex; use std::fs; use std::path::Path; -use std::time; use std::sync::mpsc::*; +use std::time; use crate::config::MiscSettings; use crate::db::{directories, misc_settings, songs, DB}; @@ -69,7 +69,11 @@ struct IndexUpdater { impl IndexUpdater { #[cfg_attr(feature = "profile-index", flame)] - fn new(album_art_pattern: Regex, directory_sender: Sender, song_sender: Sender) -> Result { + fn new( + album_art_pattern: Regex, + directory_sender: Sender, + song_sender: Sender, + ) -> Result { Ok(IndexUpdater { directory_sender, song_sender, @@ -100,12 +104,12 @@ impl IndexUpdater { } fn populate_directory(&mut self, parent: Option<&Path>, path: &Path) -> Result<()> { - #[cfg(feature = "profile-index")] - let _guard = flame::start_guard(format!("dir: {}", - path.file_name().map(|s| { - s.to_string_lossy().into_owned() - }).unwrap_or("Unknown".to_owned()) + let _guard = flame::start_guard(format!( + "dir: {}", + path.file_name() + .map(|s| { s.to_string_lossy().into_owned() }) + .unwrap_or("Unknown".to_owned()) )); // Find artwork @@ -129,10 +133,10 @@ impl IndexUpdater { #[cfg(feature = "profile-index")] let _guard = flame::start_guard("created_date"); metadata - .created() - .or_else(|_| metadata.modified())? - .duration_since(time::UNIX_EPOCH)? - .as_secs() as i32 + .created() + .or_else(|_| metadata.modified())? + .duration_since(time::UNIX_EPOCH)? + .as_secs() as i32 }; let mut directory_album = None; @@ -147,7 +151,6 @@ impl IndexUpdater { // Insert content for file in fs::read_dir(path)? { - let file_path = match file { Ok(ref f) => f.path(), _ => { @@ -157,10 +160,13 @@ impl IndexUpdater { }; #[cfg(feature = "profile-index")] - let _guard = flame::start_guard(format!("file: {}", - file_path.as_path().file_name().map(|s| { - s.to_string_lossy().into_owned() - }).unwrap_or("Unknown".to_owned()) + let _guard = flame::start_guard(format!( + "file: {}", + file_path + .as_path() + .file_name() + .map(|s| { s.to_string_lossy().into_owned() }) + .unwrap_or("Unknown".to_owned()) )); if file_path.is_dir() { @@ -170,7 +176,6 @@ impl IndexUpdater { if let Some(file_path_string) = file_path.to_str() { if let Some(tags) = metadata::read(file_path.as_path()) { - if tags.year.is_some() { inconsistent_directory_year |= directory_year.is_some() && directory_year != tags.year; @@ -311,7 +316,7 @@ pub fn populate(db: &DB) -> Result<()> { let vfs = db.get_vfs()?; let mount_points = vfs.get_mount_points(); - let album_art_pattern = { + let album_art_pattern = { let connection = db.connect()?; let settings: MiscSettings = misc_settings::table.get_result(&connection)?; Regex::new(&settings.index_album_art_pattern)? @@ -339,12 +344,18 @@ pub fn populate(db: &DB) -> Result<()> { } match directories_thread.join() { - Err(e) => error!("Error while waiting for directory insertions to complete: {:?}", e), + Err(e) => error!( + "Error while waiting for directory insertions to complete: {:?}", + e + ), _ => (), } match songs_thread.join() { - Err(e) => error!("Error while waiting for song insertions to complete: {:?}", e), + Err(e) => error!( + "Error while waiting for song insertions to complete: {:?}", + e + ), _ => (), } @@ -352,27 +363,31 @@ pub fn populate(db: &DB) -> Result<()> { } fn flush_directories(db: &DB, entries: &Vec) { - if db.connect() - .and_then(|connection|{ - diesel::insert_into(directories::table) - .values(entries) - .execute(&*connection) // TODO https://github.com/diesel-rs/diesel/issues/1822 - .map_err(Error::new) - }) - .is_err() { + if db + .connect() + .and_then(|connection| { + diesel::insert_into(directories::table) + .values(entries) + .execute(&*connection) // TODO https://github.com/diesel-rs/diesel/issues/1822 + .map_err(Error::new) + }) + .is_err() + { error!("Could not insert new directories in database"); } } fn flush_songs(db: &DB, entries: &Vec) { - if db.connect() - .and_then(|connection|{ - diesel::insert_into(songs::table) - .values(entries) - .execute(&*connection) // TODO https://github.com/diesel-rs/diesel/issues/1822 - .map_err(Error::new) - }) - .is_err() { + if db + .connect() + .and_then(|connection| { + diesel::insert_into(songs::table) + .values(entries) + .execute(&*connection) // TODO https://github.com/diesel-rs/diesel/issues/1822 + .map_err(Error::new) + }) + .is_err() + { error!("Could not insert new songs in database"); } } @@ -389,7 +404,7 @@ fn insert_directories(receiver: Receiver, db: DB) { flush_directories(&db, &new_entries); new_entries.clear(); } - }, + } Err(_) => break, } } @@ -411,7 +426,7 @@ fn insert_songs(receiver: Receiver, db: DB) { flush_songs(&db, &new_entries); new_entries.clear(); } - }, + } Err(_) => break, } } diff --git a/src/main.rs b/src/main.rs index 495d401..c68dad5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -173,9 +173,7 @@ fn main() -> Result<()> { // Init index info!("Initializing index"); - let index = index::builder(db.clone()) - .periodic_updates(true) - .build(); + let index = index::builder(db.clone()).periodic_updates(true).build(); // API mount target let prefix_url = config.prefix_url.unwrap_or_else(|| "".to_string()); diff --git a/src/service/rocket/api.rs b/src/service/rocket/api.rs index 9e474e6..e5776bc 100644 --- a/src/service/rocket/api.rs +++ b/src/service/rocket/api.rs @@ -230,10 +230,7 @@ fn put_preferences(db: State<'_, DB>, auth: Auth, preferences: Json } #[post("/trigger_index")] -fn trigger_index( - index: State<'_, Index>, - _admin_rights: AdminRights, -) -> Result<()> { +fn trigger_index(index: State<'_, Index>, _admin_rights: AdminRights) -> Result<()> { index.trigger_reindex(); Ok(()) } diff --git a/src/service/rocket/test.rs b/src/service/rocket/test.rs index 3ae787b..59e2bf3 100644 --- a/src/service/rocket/test.rs +++ b/src/service/rocket/test.rs @@ -82,9 +82,7 @@ impl TestService for RocketTestService { ) .unwrap(); let client = Client::new(server).unwrap(); - RocketTestService { - client, - } + RocketTestService { client } } fn get(&mut self, url: &str) -> Response<()> {