From d073e0254976a99892e96e32f0430719de98cd94 Mon Sep 17 00:00:00 2001 From: Antoine Gersant Date: Sun, 7 May 2017 21:21:30 -0700 Subject: [PATCH] Applied rustfmt --- src/api.rs | 16 ++++-- src/collection.rs | 4 +- src/config.rs | 19 ++++--- src/ddns.rs | 11 ++-- src/index.rs | 141 +++++++++++++++++++++++++++------------------- src/main.rs | 4 +- src/metadata.rs | 52 +++++++++-------- src/vfs.rs | 20 ++++--- 8 files changed, 155 insertions(+), 112 deletions(-) diff --git a/src/api.rs b/src/api.rs index e4d1ce1..fc46e2e 100644 --- a/src/api.rs +++ b/src/api.rs @@ -74,7 +74,7 @@ pub fn get_api_handler(collection: Arc) -> Mount { } let mut auth_api_chain = Chain::new(auth_api_mount); - let auth = AuthRequirement { collection:collection.clone() }; + let auth = AuthRequirement { collection: collection.clone() }; auth_api_chain.link_around(auth); api_handler.mount("/", auth_api_chain); @@ -83,7 +83,10 @@ pub fn get_api_handler(collection: Arc) -> Mount { } fn path_from_request(request: &Request) -> Result { - let path_string = request.url.path().join(&::std::path::MAIN_SEPARATOR.to_string()); + let path_string = request + .url + .path() + .join(&::std::path::MAIN_SEPARATOR.to_string()); let decoded_path = percent_decode(path_string.as_bytes()).decode_utf8()?; Ok(PathBuf::from(decoded_path.deref())) } @@ -95,9 +98,9 @@ struct AuthRequirement { impl AroundMiddleware for AuthRequirement { fn around(self, handler: Box) -> Box { Box::new(AuthHandler { - collection: self.collection, - handler: handler - }) as Box + collection: self.collection, + handler: handler, + }) as Box } } @@ -121,7 +124,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 = self.collection.auth(auth.username.as_str(), password.as_str()); + auth_success = self.collection + .auth(auth.username.as_str(), password.as_str()); username = Some(auth.username.clone()); } } diff --git a/src/collection.rs b/src/collection.rs index 0b255be..09c2848 100644 --- a/src/collection.rs +++ b/src/collection.rs @@ -45,7 +45,9 @@ impl Collection { } pub fn auth(&self, username: &str, password: &str) -> bool { - self.users.iter().any(|u| u.name == username && u.password == password) + self.users + .iter() + .any(|u| u.name == username && u.password == password) } pub fn browse(&self, virtual_path: &Path) -> Result> { diff --git a/src/config.rs b/src/config.rs index ca57650..4e1915d 100644 --- a/src/config.rs +++ b/src/config.rs @@ -52,7 +52,8 @@ impl Config { let mut config_file_content = String::new(); config_file.read_to_string(&mut config_file_content)?; let parsed_config = toml::Parser::new(config_file_content.as_str()).parse(); - let parsed_config = parsed_config.ok_or("Could not parse config as valid TOML")?; + let parsed_config = parsed_config + .ok_or("Could not parse config as valid TOML")?; let mut config = Config { secret: String::new(), @@ -77,7 +78,8 @@ impl Config { } fn parse_secret(&mut self, source: &toml::Table) -> Result<()> { - self.secret = source.get(CONFIG_SECRET) + self.secret = source + .get(CONFIG_SECRET) .and_then(|s| s.as_str()) .map(|s| s.to_owned()) .ok_or("Could not parse config secret")?; @@ -173,8 +175,9 @@ impl Config { _ => bail!("Could not parse DDNS settings table"), }; - let host = - ddns.get(CONFIG_DDNS_HOST).and_then(|n| n.as_str()).ok_or("Could not parse DDNS host")?; + let host = ddns.get(CONFIG_DDNS_HOST) + .and_then(|n| n.as_str()) + .ok_or("Could not parse DDNS host")?; let username = ddns.get(CONFIG_DDNS_USERNAME) .and_then(|n| n.as_str()) .ok_or("Could not parse DDNS username")?; @@ -183,10 +186,10 @@ impl Config { .ok_or("Could not parse DDNS password")?; self.ddns = Some(DDNSConfig { - host: host.to_owned(), - username: username.to_owned(), - password: password.to_owned(), - }); + host: host.to_owned(), + username: username.to_owned(), + password: password.to_owned(), + }); Ok(()) } } diff --git a/src/ddns.rs b/src/ddns.rs index 3b6af9a..b324c20 100644 --- a/src/ddns.rs +++ b/src/ddns.rs @@ -49,11 +49,14 @@ fn update_my_ip(ip: &String, config: &DDNSConfig) -> Result<(), DDNSError> { let host = &config.host; let full_url = format!("{}?host={}&ip={}", url, host, ip); let auth_header = Authorization(Basic { - username: config.username.clone(), - password: Some(config.password.to_owned()), - }); + username: config.username.clone(), + password: Some(config.password.to_owned()), + }); - let res = client.get(full_url.as_str()).header(auth_header).send()?; + let res = client + .get(full_url.as_str()) + .header(auth_header) + .send()?; match res.status { hyper::status::StatusCode::Ok => Ok(()), s => Err(DDNSError::UpdateError(s)), diff --git a/src/index.rs b/src/index.rs index 7204dda..ce5c56f 100644 --- a/src/index.rs +++ b/src/index.rs @@ -99,16 +99,14 @@ impl<'db> IndexBuilder<'db> { let mut queue = Vec::new(); queue.reserve_exact(INDEX_BUILDING_INSERT_BUFFER_SIZE); Ok(IndexBuilder { - queue: queue, - db: db, - insert_directory: - db.prepare("INSERT OR REPLACE INTO directories (path, parent, artwork, year, \ + queue: queue, + db: db, + insert_directory: db.prepare("INSERT OR REPLACE INTO directories (path, parent, artwork, year, \ artist, album) VALUES (?, ?, ?, ?, ?, ?)")?, - insert_song: - db.prepare("INSERT OR REPLACE INTO songs (path, parent, disc_number, track_number, \ + insert_song: db.prepare("INSERT OR REPLACE INTO songs (path, parent, disc_number, track_number, \ title, year, album_artist, artist, album, artwork) VALUES (?, ?, ?, ?, \ ?, ?, ?, ?, ?, ?)")?, - }) + }) } fn get_parent(path: &str) -> Option { @@ -130,11 +128,14 @@ impl<'db> IndexBuilder<'db> { CollectionFile::Directory(directory) => { let parent = IndexBuilder::get_parent(directory.path.as_str()); self.insert_directory.reset()?; - self.insert_directory.bind(1, &Value::String(directory.path))?; - self.insert_directory.bind(2, &string_option_to_value(parent))?; + self.insert_directory + .bind(1, &Value::String(directory.path))?; + self.insert_directory + .bind(2, &string_option_to_value(parent))?; self.insert_directory .bind(3, &string_option_to_value(directory.artwork))?; - self.insert_directory.bind(4, &i32_option_to_value(directory.year))?; + self.insert_directory + .bind(4, &i32_option_to_value(directory.year))?; self.insert_directory .bind(5, &string_option_to_value(directory.artist))?; self.insert_directory @@ -147,15 +148,24 @@ impl<'db> IndexBuilder<'db> { let parent = IndexBuilder::get_parent(song.path.as_str()); self.insert_song.reset()?; self.insert_song.bind(1, &Value::String(song.path))?; - self.insert_song.bind(2, &string_option_to_value(parent))?; - self.insert_song.bind(3, &u32_option_to_value(song.disc_number))?; - self.insert_song.bind(4, &u32_option_to_value(song.track_number))?; - self.insert_song.bind(5, &string_option_to_value(song.title))?; - self.insert_song.bind(6, &i32_option_to_value(song.year))?; - self.insert_song.bind(7, &string_option_to_value(song.album_artist))?; - self.insert_song.bind(8, &string_option_to_value(song.artist))?; - self.insert_song.bind(9, &string_option_to_value(song.album))?; - self.insert_song.bind(10, &string_option_to_value(song.artwork))?; + self.insert_song + .bind(2, &string_option_to_value(parent))?; + self.insert_song + .bind(3, &u32_option_to_value(song.disc_number))?; + self.insert_song + .bind(4, &u32_option_to_value(song.track_number))?; + self.insert_song + .bind(5, &string_option_to_value(song.title))?; + self.insert_song + .bind(6, &i32_option_to_value(song.year))?; + self.insert_song + .bind(7, &string_option_to_value(song.album_artist))?; + self.insert_song + .bind(8, &string_option_to_value(song.artist))?; + self.insert_song + .bind(9, &string_option_to_value(song.album))?; + self.insert_song + .bind(10, &string_option_to_value(song.artwork))?; self.insert_song.next()?; } @@ -458,7 +468,11 @@ impl Index { let mut artwork = None; if let Some(artwork_path) = artwork_path { artwork = match self.vfs.real_to_virtual(artwork_path) { - Ok(p) => Some(p.to_str().ok_or("Invalid song artwork path")?.to_owned()), + Ok(p) => { + Some(p.to_str() + .ok_or("Invalid song artwork path")? + .to_owned()) + } _ => None, }; } @@ -500,7 +514,11 @@ impl Index { let mut artwork = None; if let Some(artwork_path) = artwork_path { artwork = match self.vfs.real_to_virtual(artwork_path) { - Ok(p) => Some(p.to_str().ok_or("Invalid directory artwork path")?.to_owned()), + Ok(p) => { + Some(p.to_str() + .ok_or("Invalid directory artwork path")? + .to_owned()) + } _ => None, }; } @@ -523,13 +541,16 @@ impl Index { let db = self.connect()?; let path_string = real_path.to_string_lossy(); - let mut select = - db.prepare("SELECT path, artwork, year, artist, album FROM directories WHERE \ + let mut select = db.prepare("SELECT path, artwork, year, artist, album FROM directories WHERE \ parent = ? ORDER BY path COLLATE NOCASE ASC")?; - select.bind(1, &Value::String(path_string.deref().to_owned()))?; + select + .bind(1, &Value::String(path_string.deref().to_owned()))?; let output = self.select_directories(&mut select)?; - let output = output.into_iter().map(|d| CollectionFile::Directory(d)).collect(); + let output = output + .into_iter() + .map(|d| CollectionFile::Directory(d)) + .collect(); Ok(output) } @@ -537,12 +558,15 @@ impl Index { fn browse_songs(&self, real_path: &Path) -> Result> { let db = self.connect()?; let path_string = real_path.to_string_lossy(); - let mut select = - db.prepare("SELECT path, disc_number, track_number, title, year, album_artist, \ + let mut select = db.prepare("SELECT path, disc_number, track_number, title, year, album_artist, \ artist, album, artwork FROM songs WHERE parent = ? ORDER BY track_number, path \ COLLATE NOCASE ASC")?; - select.bind(1, &Value::String(path_string.deref().to_owned()))?; - Ok(self.select_songs(&mut select)?.into_iter().map(|s| CollectionFile::Song(s)).collect()) + select + .bind(1, &Value::String(path_string.deref().to_owned()))?; + Ok(self.select_songs(&mut select)? + .into_iter() + .map(|s| CollectionFile::Song(s)) + .collect()) } pub fn browse(&self, virtual_path: &Path) -> Result> { @@ -578,18 +602,17 @@ impl Index { let db = self.connect()?; let real_path = self.vfs.virtual_to_real(virtual_path)?; let path_string = real_path.to_string_lossy().into_owned() + "%"; - let mut select = - db.prepare("SELECT path, disc_number, track_number, title, year, album_artist, \ + let mut select = db.prepare("SELECT path, disc_number, track_number, title, year, album_artist, \ artist, album, artwork FROM songs WHERE path LIKE ? ORDER BY path \ COLLATE NOCASE ASC")?; - select.bind(1, &Value::String(path_string.deref().to_owned()))?; + select + .bind(1, &Value::String(path_string.deref().to_owned()))?; self.select_songs(&mut select) } pub fn get_random_albums(&self, count: u32) -> Result> { let db = self.connect()?; - let mut select = - db.prepare("SELECT path, artwork, year, artist, album FROM directories WHERE album \ + let mut select = db.prepare("SELECT path, artwork, year, artist, album FROM directories WHERE album \ IS NOT NULL ORDER BY RANDOM() LIMIT ?")?; select.bind(1, &Value::Integer(count as i64))?; self.select_directories(&mut select) @@ -647,16 +670,16 @@ fn test_metadata() { assert_eq!(results.len(), 7); assert_eq!(results[4], CollectionFile::Song(Song { - path: song_path.to_str().unwrap().to_string(), - track_number: Some(5), - disc_number: None, - title: Some("シャーベット (Sherbet)".to_owned()), - artist: Some("Tobokegao".to_owned()), - album_artist: None, - album: Some("Picnic".to_owned()), - year: Some(2016), - artwork: Some(artwork_path.to_str().unwrap().to_string()), - })); + path: song_path.to_str().unwrap().to_string(), + track_number: Some(5), + disc_number: None, + title: Some("シャーベット (Sherbet)".to_owned()), + artist: Some("Tobokegao".to_owned()), + album_artist: None, + album: Some("Picnic".to_owned()), + year: Some(2016), + artwork: Some(artwork_path.to_str().unwrap().to_string()), + })); } #[test] @@ -665,25 +688,27 @@ fn test_browse() { khemmis_path.push("root"); khemmis_path.push("Khemmis"); - let khemmis = CollectionFile::Directory(Directory { - path: khemmis_path.to_string_lossy().deref().to_string(), - artist: None, - album: None, - year: None, - artwork: None, - }); + let khemmis = + CollectionFile::Directory(Directory { + path: khemmis_path.to_string_lossy().deref().to_string(), + artist: None, + album: None, + year: None, + artwork: None, + }); let mut tobokegao_path = PathBuf::new(); tobokegao_path.push("root"); tobokegao_path.push("Tobokegao"); - let tobokegao = CollectionFile::Directory(Directory { - path: tobokegao_path.to_string_lossy().deref().to_string(), - artist: None, - album: None, - year: None, - artwork: None, - }); + let tobokegao = + CollectionFile::Directory(Directory { + path: tobokegao_path.to_string_lossy().deref().to_string(), + artist: None, + album: None, + year: None, + artwork: None, + }); let index = _get_test_index("browse.sqlite"); index.update_index().unwrap(); diff --git a/src/main.rs b/src/main.rs index 829f876..a8c4b2c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -120,9 +120,7 @@ fn run() -> Result<()> { match config.ddns { Some(ref ddns_config) => { let ddns_config = ddns_config.clone(); - std::thread::spawn(|| { - ddns::run(ddns_config); - }); + std::thread::spawn(|| { ddns::run(ddns_config); }); } None => (), }; diff --git a/src/metadata.rs b/src/metadata.rs index 1a6d018..22c6212 100644 --- a/src/metadata.rs +++ b/src/metadata.rs @@ -47,14 +47,14 @@ fn read_id3(path: &Path) -> Result { .or(tag.date_recorded().and_then(|d| d.year)); Ok(SongTags { - artist: artist, - album_artist: album_artist, - album: album, - title: title, - disc_number: disc_number, - track_number: track_number, - year: year, - }) + artist: artist, + album_artist: album_artist, + album: album, + title: title, + disc_number: disc_number, + track_number: track_number, + year: year, + }) } fn read_ape_string(item: &ape::Item) -> Option { @@ -95,14 +95,14 @@ fn read_ape(path: &Path) -> Result { let disc_number = tag.item("Disc").and_then(read_ape_x_of_y); let track_number = tag.item("Track").and_then(read_ape_x_of_y); Ok(SongTags { - artist: artist, - album_artist: album_artist, - album: album, - title: title, - disc_number: disc_number, - track_number: track_number, - year: year, - }) + artist: artist, + album_artist: album_artist, + album: album, + title: title, + disc_number: disc_number, + track_number: track_number, + year: year, + }) } fn read_vorbis(path: &Path) -> Result { @@ -139,17 +139,19 @@ fn read_vorbis(path: &Path) -> Result { fn read_flac(path: &Path) -> Result { let tag = metaflac::Tag::read_from_path(path)?; let vorbis = tag.vorbis_comments().ok_or("Missing Vorbis comments")?; - let disc_number = vorbis.get("DISCNUMBER").and_then(|d| d[0].parse::().ok()); + let disc_number = vorbis + .get("DISCNUMBER") + .and_then(|d| d[0].parse::().ok()); let year = vorbis.get("DATE").and_then(|d| d[0].parse::().ok()); Ok(SongTags { - artist: vorbis.artist().map(|v| v[0].clone()), - album_artist: vorbis.album_artist().map(|v| v[0].clone()), - album: vorbis.album().map(|v| v[0].clone()), - title: vorbis.title().map(|v| v[0].clone()), - disc_number: disc_number, - track_number: vorbis.track(), - year: year, - }) + artist: vorbis.artist().map(|v| v[0].clone()), + album_artist: vorbis.album_artist().map(|v| v[0].clone()), + album: vorbis.album().map(|v| v[0].clone()), + title: vorbis.title().map(|v| v[0].clone()), + disc_number: disc_number, + track_number: vorbis.track(), + year: year, + }) } #[test] diff --git a/src/vfs.rs b/src/vfs.rs index 8dbbe3b..69e5259 100644 --- a/src/vfs.rs +++ b/src/vfs.rs @@ -43,10 +43,10 @@ impl Vfs { match virtual_path.strip_prefix(mount_path) { Ok(p) => { return if p.components().count() == 0 { - Ok(target.clone()) - } else { - Ok(target.join(p)) - } + Ok(target.clone()) + } else { + Ok(target.join(p)) + } } Err(_) => (), } @@ -62,7 +62,9 @@ impl Vfs { #[test] fn test_virtual_to_real() { let mut config = VfsConfig::new(); - config.mount_points.insert("root".to_owned(), Path::new("test_dir").to_path_buf()); + config + .mount_points + .insert("root".to_owned(), Path::new("test_dir").to_path_buf()); let vfs = Vfs::new(config); let mut correct_path = PathBuf::new(); @@ -82,7 +84,9 @@ fn test_virtual_to_real() { #[test] fn test_virtual_to_real_no_trail() { let mut config = VfsConfig::new(); - config.mount_points.insert("root".to_owned(), Path::new("test_dir").to_path_buf()); + config + .mount_points + .insert("root".to_owned(), Path::new("test_dir").to_path_buf()); let vfs = Vfs::new(config); let correct_path = Path::new("test_dir"); let found_path = vfs.virtual_to_real(Path::new("root")).unwrap(); @@ -92,7 +96,9 @@ fn test_virtual_to_real_no_trail() { #[test] fn test_real_to_virtual() { let mut config = VfsConfig::new(); - config.mount_points.insert("root".to_owned(), Path::new("test_dir").to_path_buf()); + config + .mount_points + .insert("root".to_owned(), Path::new("test_dir").to_path_buf()); let vfs = Vfs::new(config); let mut correct_path = PathBuf::new();