diff --git a/src/library/album.rs b/src/library/album.rs index a14b35c..2e55233 100644 --- a/src/library/album.rs +++ b/src/library/album.rs @@ -65,7 +65,7 @@ impl ToAPI for Album { json!({ "id": &self._id, "title": &self.title, - "artist": self.artist_id.as_ref().map(|x| x.id()), + "artist": self.artist_id.as_ref().map(mongod::Reference::id), "cover_url": if self.get_cover().await.is_some() { Some(format!("/album/{}/cover", self._id)) } else { diff --git a/src/library/metadata.rs b/src/library/metadata.rs index 3061f9f..e5c4106 100644 --- a/src/library/metadata.rs +++ b/src/library/metadata.rs @@ -5,15 +5,15 @@ use serde::{Deserialize, Serialize}; #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] pub struct AudioMetadata(pub serde_json::Value); -impl Into for AudioMetadata { - fn into(self) -> mongodb::bson::Bson { - mongodb::bson::to_bson(&self.0).unwrap() +impl From for mongodb::bson::Bson { + fn from(val: AudioMetadata) -> Self { + mongodb::bson::to_bson(&val.0).unwrap() } } impl AudioMetadata { fn get_key(&self, key: &str) -> Option<&str> { - Some(self.0.as_object()?.get(key)?.as_str()?) + self.0.as_object()?.get(key)?.as_str() } pub fn title(&self) -> Option<&str> { diff --git a/src/library/mod.rs b/src/library/mod.rs index af14917..39e3e48 100644 --- a/src/library/mod.rs +++ b/src/library/mod.rs @@ -31,7 +31,7 @@ pub struct Libary { } impl Libary { - pub async fn new(root_dir: PathBuf) -> Self { + pub const fn new(root_dir: PathBuf) -> Self { Self { root_dir } } @@ -53,9 +53,13 @@ impl Libary { pub async fn add_path_to_library(&self, path: &str) { // search for path already present - if let Some(_) = Track::find_one_partial(doc! { "path": path }, json!({})).await { + if Track::find_one_partial(doc! { "path": path }, json!({})) + .await + .is_some() + { return; } + log::info!("Adding {path} to library"); // add track to library @@ -124,7 +128,7 @@ impl Libary { ); } - Track::create(&entry.as_object().unwrap()).await; + Track::create(entry.as_object().unwrap()).await; } pub async fn get_artists(&self) -> Vec { @@ -164,8 +168,8 @@ impl Libary { log::info!("Rescanning library"); for entry in WalkDir::new(self.root_dir.clone()) .follow_links(true) - .into_iter() // todo : remove - .filter_map(|e| e.ok()) + .into_iter() + .filter_map(std::result::Result::ok) { let path = entry.path(); if path.is_file() && is_music_file(path) { diff --git a/src/library/playlist.rs b/src/library/playlist.rs index 1485def..0ff515b 100644 --- a/src/library/playlist.rs +++ b/src/library/playlist.rs @@ -42,7 +42,7 @@ impl Playlist { } } -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] pub enum Visibility { Private, Public, @@ -67,7 +67,7 @@ impl ToAPI for Playlist { "owner": self.owner.id(), "visibility": serde_json::to_value(&self.visibility).unwrap(), "title": self.title, - "tracks": self.tracks.iter().map(|x| x.id()).collect::>() + "tracks": self.tracks.iter().map(mongod::Reference::id).collect::>() }) } } diff --git a/src/library/track.rs b/src/library/track.rs index 156bc0e..136264d 100644 --- a/src/library/track.rs +++ b/src/library/track.rs @@ -37,7 +37,7 @@ impl Track { meta: data.get("meta").map(|x| AudioMetadata(x.clone())), }; t.insert().await.unwrap(); - t.update(&serde_json::to_value(&data).unwrap()) + t.update(&serde_json::to_value(data).unwrap()) .await .unwrap(); } @@ -88,12 +88,12 @@ impl Track { impl ToAPI for Track { async fn api(&self) -> serde_json::Value { - let (cover, album_title) = if let Some(album_ref) = &self.album_id { + let (cover, album_title, album_id) = if let Some(album_ref) = &self.album_id { let album = album_ref.get::().await; - (album.get_cover().await.is_some(), album.title) + (album.get_cover().await.is_some(), album.title, album._id) } else { - (false, String::new()) + (false, String::new(), String::new()) }; let artist_title = if let Some(artist_ref) = &self.artist_id { @@ -108,12 +108,12 @@ impl ToAPI for Track { json!({ "id": self._id, "title": self.title, - "track_number": self.meta.as_ref().map(|x| x.track_number()), + "track_number": self.meta.as_ref().map(super::metadata::AudioMetadata::track_number), "meta": serde_json::to_value(&self.meta).unwrap(), "album_id": self.album_id.as_ref().map(|x| x.id().to_string()), "album": album_title, "cover": if cover { - Some(format!("/album/{}/cover", self._id)) + Some(format!("/album/{album_id}/cover")) } else { None }, diff --git a/src/library/user.rs b/src/library/user.rs index 22d0a9c..1797dd7 100644 --- a/src/library/user.rs +++ b/src/library/user.rs @@ -41,14 +41,14 @@ impl Validate for User { impl User { pub async fn create(username: &str, password: &str, role: UserRole) -> Option { - if User::find_one_partial(doc! { "username": username }, json!({})) + if Self::find_one_partial(doc! { "username": username }, json!({})) .await .is_some() { return None; } - let u = User { + let u = Self { _id: uuid::Uuid::new_v4().to_string(), username: username.to_string(), password: bcrypt::hash(password, bcrypt::DEFAULT_COST).unwrap(), @@ -61,7 +61,7 @@ impl User { } pub async fn login(username: &str, password: &str) -> Option<(Session, UserRole)> { - let u = User::find_one(doc! { "username": username }).await?; + let u = Self::find_one(doc! { "username": username }).await?; if !u.verify_pw(password) { return None; @@ -99,7 +99,7 @@ impl User { s } - pub fn is_admin(&self) -> bool { + pub const fn is_admin(&self) -> bool { matches!(self.role, UserRole::Admin) } diff --git a/src/main.rs b/src/main.rs index 3eeca8d..3e10471 100644 --- a/src/main.rs +++ b/src/main.rs @@ -27,7 +27,7 @@ async fn rocket() -> _ { .to_cors() .expect("error creating CORS options"); - let lib = Libary::new("./media".into()).await; + let lib = Libary::new("./media".into()); lib.rescan().await; diff --git a/src/route/album.rs b/src/route/album.rs index 8741d45..18f84bd 100644 --- a/src/route/album.rs +++ b/src/route/album.rs @@ -3,10 +3,9 @@ use std::cmp::Ordering; use super::api_error; use super::FallibleApiResponse; use super::ToAPI; -use mongod::Referencable; use mongodb::bson::doc; use rocket::fs::NamedFile; -use rocket::*; +use rocket::{get, State}; use serde_json::json; use crate::cache::RouteCache; @@ -69,17 +68,9 @@ pub async fn album_route( .await .ok_or_else(|| api_error("No album with that ID found"))?; - let mut tracks = Album::get_tracks_of_album(&format!("album::{}", album._id)) - .await - .into_iter() - .map(|x| { - json!({ - "id": x.id(), - "title": x.title, - "tracknumber": x.meta.map(|x| x.track_number()) - }) - }) - .collect::>(); + let tracks = Album::get_tracks_of_album(&format!("album::{}", album._id)).await; + + let mut tracks = to_api(&tracks).await; tracks.sort_by(sort_by_tracknumber); diff --git a/src/route/artist.rs b/src/route/artist.rs index c64443c..30fff23 100644 --- a/src/route/artist.rs +++ b/src/route/artist.rs @@ -5,7 +5,7 @@ use super::ToAPI; use fs::NamedFile; use mongod::Model; use mongodb::bson::doc; -use rocket::*; +use rocket::{fs, get, State}; use crate::cache::RouteCache; use crate::library::artist::Artist; diff --git a/src/route/mod.rs b/src/route/mod.rs index 4289343..2602d6c 100644 --- a/src/route/mod.rs +++ b/src/route/mod.rs @@ -38,11 +38,11 @@ pub async fn to_api(albums: &[impl ToAPI]) -> Vec { } #[get("/")] -pub async fn index_redir() -> Redirect { +pub fn index_redir() -> Redirect { Redirect::to(uri!("/web")) } #[get("/manifest.json")] -pub async fn manifest_redir() -> Redirect { +pub fn manifest_redir() -> Redirect { Redirect::to(uri!("/web/manifest.json")) } diff --git a/src/route/track.rs b/src/route/track.rs index 0c36507..ede01af 100644 --- a/src/route/track.rs +++ b/src/route/track.rs @@ -3,7 +3,7 @@ use super::FallibleApiResponse; use super::ToAPI; use fs::NamedFile; use mongodb::bson::doc; -use rocket::*; +use rocket::{fs, get, State}; use crate::library::Libary; diff --git a/src/route/user.rs b/src/route/user.rs index 3dccb40..cddbe07 100644 --- a/src/route/user.rs +++ b/src/route/user.rs @@ -71,7 +71,7 @@ pub struct PasswdData { pub async fn passwd_route(passwd: Json, mut u: User) -> FallibleApiResponse { u.passwd(&passwd.old, &passwd.new) .await - .map_err(|_| api_error("Password change failed"))?; + .map_err(|()| api_error("Password change failed"))?; Ok(json!({ "ok": 1