From 8579d5190d227d56b7493a8a11b8c29a276472fd Mon Sep 17 00:00:00 2001 From: psykose Date: Sat, 23 Nov 2024 22:39:12 +0100 Subject: [PATCH] make code less readable --- Cargo.toml | 2 +- src/application.rs | 2 +- src/model/song.rs | 2 +- src/mpris.rs | 18 ++++++------- src/mpris/player.rs | 11 ++++---- src/mpv/error.rs | 4 +-- src/mpv/format.rs | 12 ++++----- src/subsonic.rs | 10 +++----- src/ui/play_queue.rs | 2 +- src/ui/play_queue/song.rs | 2 +- src/ui/playbar.rs | 8 +++--- src/ui/window.rs | 54 ++++++++++++++++++++------------------- 12 files changed, 63 insertions(+), 64 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index bbe4019..4491829 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -40,7 +40,7 @@ reqwest-middleware = { version = "0.4.0", features = [ ] } serde = { version = "1.0.214", features = ["derive"] } serde_json = "1.0.133" -tokio = { version = "1", features = ["parking_lot", "rt-multi-thread"] } +tokio = { version = "1", features = ["parking_lot", "rt-multi-thread", "sync"] } tracing = { version = "0.1.40", default-features = false, features = [ "attributes", "std", diff --git a/src/application.rs b/src/application.rs index 40a0951..0195919 100644 --- a/src/application.rs +++ b/src/application.rs @@ -56,7 +56,7 @@ glib::wrapper! { impl Default for Application { fn default() -> Self { - glib::Object::builder::() + glib::Object::builder::() .property("application-id", crate::APP_ID) .property("flags", gio::ApplicationFlags::default()) .build() diff --git a/src/model/song.rs b/src/model/song.rs index eb88071..027805a 100644 --- a/src/model/song.rs +++ b/src/model/song.rs @@ -76,7 +76,7 @@ glib::wrapper! { impl Song { pub fn from_child(window: &crate::ui::Window, song: &subsonic::schema::Child) -> Self { let api = window.api(); - let song: Song = Object::builder() + let song: Self = Object::builder() .property("id", &song.id) .property("title", &song.title) .property("artist", &song.artist) diff --git a/src/mpris.rs b/src/mpris.rs index d65db7d..619024f 100644 --- a/src/mpris.rs +++ b/src/mpris.rs @@ -20,8 +20,6 @@ impl Mpris { object_server.at("/org/mpris/MediaPlayer2", mpris).await?; - //let _mpris = object_server.interface::<_, Self>("/org/mpris/MediaPlayer2").await?; - Ok(()) } } @@ -43,34 +41,34 @@ impl Mpris { } #[zbus(property)] - fn can_quit(&self) -> bool { + const fn can_quit(&self) -> bool { true } #[zbus(property)] - fn fullscreen(&self) -> bool { + const fn fullscreen(&self) -> bool { false } #[zbus(property)] // TODO: report that if the argument is just _ the attribute panics // TODO: why can't this return zbus::fdo::Result?? - fn set_fullscreen(&self, _fullscreen: bool) -> zbus::Result<()> { + const fn set_fullscreen(&self, _fullscreen: bool) -> zbus::Result<()> { Err(zbus::Error::Unsupported) } #[zbus(property)] - fn can_set_fullscreen(&self) -> bool { + const fn can_set_fullscreen(&self) -> bool { false } #[zbus(property)] - fn can_raise(&self) -> bool { + const fn can_raise(&self) -> bool { true } #[zbus(property)] - fn has_track_list(&self) -> bool { + const fn has_track_list(&self) -> bool { false // TODO? } @@ -85,12 +83,12 @@ impl Mpris { } #[zbus(property)] - fn supported_uri_schemes(&self) -> Vec { + const fn supported_uri_schemes(&self) -> Vec { vec![] } #[zbus(property)] - fn supported_mime_types(&self) -> Vec { + const fn supported_mime_types(&self) -> Vec { vec![] } } diff --git a/src/mpris/player.rs b/src/mpris/player.rs index 5a03cbd..2f59010 100644 --- a/src/mpris/player.rs +++ b/src/mpris/player.rs @@ -16,7 +16,7 @@ impl Player { pub async fn new( object_server: &zbus::ObjectServer, playbin: &Window, - ) -> Result, zbus::Error> { + ) -> Result, zbus::Error> { let player = Self { window: playbin.downgrade().into(), }; @@ -131,7 +131,7 @@ impl Player { } #[zbus(property)] - fn rate(&self) -> zbus::fdo::Result { + const fn rate(&self) -> zbus::fdo::Result { Ok(1.0) } @@ -173,6 +173,7 @@ impl Player { (url::Url::from_file_path(path).unwrap().to_string()).into(), ); } + drop(mpris_art_path); map.insert("xesam:album", song.album().into()); // TODO: use the right opensubsonic data map.insert("xesam:artist", vec![song.artist()].into()); @@ -201,12 +202,12 @@ impl Player { } #[zbus(property)] - fn minimum_rate(&self) -> f64 { + const fn minimum_rate(&self) -> f64 { 1.0 } #[zbus(property)] - fn maximum_rate(&self) -> f64 { + const fn maximum_rate(&self) -> f64 { 1.0 } @@ -236,7 +237,7 @@ impl Player { } #[zbus(property(emits_changed_signal = "const"))] - fn can_control(&self) -> bool { + const fn can_control(&self) -> bool { true } } diff --git a/src/mpv/error.rs b/src/mpv/error.rs index bb6bc0e..868d8fc 100644 --- a/src/mpv/error.rs +++ b/src/mpv/error.rs @@ -6,7 +6,7 @@ use std::fmt; pub struct Error(pub(super) c_int); impl Error { - pub(super) fn from_return_code(rc: c_int) -> Result<(), Self> { + pub(super) const fn from_return_code(rc: c_int) -> Result<(), Self> { if rc == 0 { Ok(()) } else { @@ -20,7 +20,7 @@ impl Error { .unwrap() } - pub fn is_property_unavailable(self) -> bool { + pub const fn is_property_unavailable(self) -> bool { self.0 == ffi::mpv_error_MPV_ERROR_PROPERTY_UNAVAILABLE } } diff --git a/src/mpv/format.rs b/src/mpv/format.rs index b8043f7..b37813c 100644 --- a/src/mpv/format.rs +++ b/src/mpv/format.rs @@ -66,7 +66,7 @@ impl SetProperty for i64 { ctx, name, ffi::mpv_format_MPV_FORMAT_INT64, - std::ptr::from_ref::(&self) as *mut c_void, + std::ptr::from_ref::(&self) as *mut c_void, )) } } @@ -81,7 +81,7 @@ impl SetProperty for f64 { ctx, name, ffi::mpv_format_MPV_FORMAT_DOUBLE, - std::ptr::from_ref::(&self) as *mut c_void, + std::ptr::from_ref::(&self) as *mut c_void, )) } } @@ -111,12 +111,12 @@ impl GetProperty for bool { impl GetProperty for i64 { unsafe fn get_property(ctx: *mut ffi::mpv_handle, name: *const c_char) -> Result { - let mut value: i64 = -1; + let mut value: Self = -1; Error::from_return_code(ffi::mpv_get_property( ctx, name, ffi::mpv_format_MPV_FORMAT_INT64, - std::ptr::from_mut::(&mut value) as *mut c_void, + std::ptr::from_mut::(&mut value) as *mut c_void, ))?; Ok(value) } @@ -124,12 +124,12 @@ impl GetProperty for i64 { impl GetProperty for f64 { unsafe fn get_property(ctx: *mut ffi::mpv_handle, name: *const c_char) -> Result { - let mut value: f64 = 0.0; + let mut value: Self = 0.0; Error::from_return_code(ffi::mpv_get_property( ctx, name, ffi::mpv_format_MPV_FORMAT_DOUBLE, - std::ptr::from_mut::(&mut value) as *mut c_void, + std::ptr::from_mut::(&mut value) as *mut c_void, ))?; Ok(value) } diff --git a/src/subsonic.rs b/src/subsonic.rs index c8ec249..437fdf5 100644 --- a/src/subsonic.rs +++ b/src/subsonic.rs @@ -146,7 +146,7 @@ impl Client { .with(http_cache) .build(); - Ok(Client { client, base_url }) + Ok(Self { client, base_url }) } async fn send( @@ -255,12 +255,10 @@ impl Client { } pub fn cover_art_url(&self, id: &str, size: Option) -> url::Url { + let endpoint = &["rest", "getCoverArt"]; match size { - None => self.url(&["rest", "getCoverArt"], &[("id", id)]), - Some(size) => self.url( - &["rest", "getCoverArt"], - &[("id", id), ("size", &size.to_string())], - ), + None => self.url(endpoint, &[("id", id)]), + Some(size) => self.url(endpoint, &[("id", id), ("size", &size.to_string())]), } } diff --git a/src/ui/play_queue.rs b/src/ui/play_queue.rs index df9f5f1..e305799 100644 --- a/src/ui/play_queue.rs +++ b/src/ui/play_queue.rs @@ -50,7 +50,7 @@ mod imp { #[gtk::template_callbacks] impl PlayQueue { #[template_callback] - fn visible_child_name(&self, n_items: u32) -> &'static str { + const fn visible_child_name(&self, n_items: u32) -> &'static str { if n_items > 0 { "not-empty" } else { diff --git a/src/ui/play_queue/song.rs b/src/ui/play_queue/song.rs index 40154a3..ee00b82 100644 --- a/src/ui/play_queue/song.rs +++ b/src/ui/play_queue/song.rs @@ -203,5 +203,5 @@ impl Song { song.need_thumbnail(window); } - pub fn unbind(&self) {} + pub const fn unbind(&self) {} } diff --git a/src/ui/playbar.rs b/src/ui/playbar.rs index af5277f..e57db49 100644 --- a/src/ui/playbar.rs +++ b/src/ui/playbar.rs @@ -76,7 +76,7 @@ mod imp { } #[template_callback] - fn mute_button_icon_name(&self, mute: bool) -> &'static str { + const fn mute_button_icon_name(&self, mute: bool) -> &'static str { if mute { "audio-volume-muted" } else { @@ -150,17 +150,17 @@ mod imp { // updated #[template_callback] fn song_title(&self, song: Option<&Song>) -> String { - song.map(Song::title).unwrap_or("".to_string()) + song.map(Song::title).unwrap_or_default() } #[template_callback] fn song_artist(&self, song: Option<&Song>) -> String { - song.map(Song::artist).unwrap_or("".to_string()) + song.map(Song::artist).unwrap_or_default() } #[template_callback] fn song_album(&self, song: Option<&Song>) -> String { - song.map(Song::album).unwrap_or("".to_string()) + song.map(Song::album).unwrap_or_default() } } diff --git a/src/ui/window.rs b/src/ui/window.rs index fef2112..8b4121d 100644 --- a/src/ui/window.rs +++ b/src/ui/window.rs @@ -243,16 +243,17 @@ mod imp { self.time_pos_notify_timeout .replace(Some(glib::timeout_add_local( std::time::Duration::from_millis(100), - move || match window.upgrade() { - None => glib::ControlFlow::Break, - Some(window) => { - match window.imp().state.get() { - State::Idle | State::FileLoading | State::Seeking => {} - State::Active => window.notify("time-pos"), - State::FileEnded => {} - } - glib::ControlFlow::Continue - } + move || { + window + .upgrade() + .map_or(glib::ControlFlow::Break, move |window| { + match window.imp().state.get() { + State::Idle | State::FileLoading | State::Seeking => {} + State::Active => window.notify("time-pos"), + State::FileEnded => {} + } + glib::ControlFlow::Continue + }) }, ))); @@ -522,17 +523,19 @@ mod imp { } fn song(&self) -> Option { - match self.obj().playlist_pos().try_into() { - Ok(playlist_pos) => Some( - self.obj() - .playlist_model() - .item(playlist_pos) - .unwrap() - .dynamic_cast() - .unwrap(), - ), - Err(_) => None, - } + self.obj().playlist_pos().try_into().map_or_else( + move |_| None, + move |playlist_pos| { + Some( + self.obj() + .playlist_model() + .item(playlist_pos) + .unwrap() + .dynamic_cast() + .unwrap(), + ) + }, + ) } fn buffering_start(&self) { @@ -541,17 +544,16 @@ mod imp { if let Some(source) = self.buffering_timeout.replace(Some(glib::timeout_add_local( std::time::Duration::from_millis(100), move || { - match window.upgrade() { - None => glib::ControlFlow::Break, - Some(window) => { + window + .upgrade() + .map_or(glib::ControlFlow::Break, move |window| { // 3 second period from gnome hig if started_buffering.elapsed() > std::time::Duration::from_secs(3) { window.imp().playbar.set_show_pulse_bar(true); window.imp().playbar.pulse_bar().pulse(); } glib::ControlFlow::Continue - } - } + }) }, ))) { source.remove()