refactor(library): various Rust style optimizations

Lots of small fixes to the APIs and functions in the `library` module,
mostly following best practices outlined in the Rust library guidelines.
Changes outside the `library` module were mostly required changes after
changing function signatures.
This commit is contained in:
Thomas Frans
2024-01-29 14:43:59 +01:00
committed by Henrik Friedrichsen
parent 97f10a9493
commit 7940365344
9 changed files with 202 additions and 125 deletions

View File

@@ -29,7 +29,7 @@ pub struct Playlist {
}
impl Playlist {
pub fn load_tracks(&mut self, spotify: Spotify) {
pub fn load_tracks(&mut self, spotify: &Spotify) {
if self.tracks.is_some() {
return;
}
@@ -37,7 +37,7 @@ impl Playlist {
self.tracks = Some(self.get_all_tracks(spotify));
}
fn get_all_tracks(&self, spotify: Spotify) -> Vec<Playable> {
fn get_all_tracks(&self, spotify: &Spotify) -> Vec<Playable> {
let tracks_result = spotify.api.user_playlist_tracks(&self.id);
while !tracks_result.at_end() {
tracks_result.next();
@@ -221,7 +221,7 @@ impl ListItem for Playlist {
}
fn play(&mut self, queue: &Queue) {
self.load_tracks(queue.get_spotify());
self.load_tracks(&queue.get_spotify());
if let Some(tracks) = &self.tracks {
let index = queue.append_next(tracks);
@@ -230,7 +230,7 @@ impl ListItem for Playlist {
}
fn play_next(&mut self, queue: &Queue) {
self.load_tracks(queue.get_spotify());
self.load_tracks(&queue.get_spotify());
if let Some(tracks) = self.tracks.as_ref() {
for track in tracks.iter().rev() {
@@ -240,7 +240,7 @@ impl ListItem for Playlist {
}
fn queue(&mut self, queue: &Queue) {
self.load_tracks(queue.get_spotify());
self.load_tracks(&queue.get_spotify());
if let Some(tracks) = self.tracks.as_ref() {
for track in tracks.iter() {
@@ -258,12 +258,12 @@ impl ListItem for Playlist {
if library.is_saved_playlist(self) {
library.delete_playlist(&self.id);
} else {
library.follow_playlist(self);
library.follow_playlist(self.clone());
}
}
fn save(&mut self, library: &Library) {
library.follow_playlist(self);
library.follow_playlist(self.clone());
}
fn unsave(&mut self, library: &Library) {
@@ -279,7 +279,7 @@ impl ListItem for Playlist {
queue: Arc<Queue>,
library: Arc<Library>,
) -> Option<Box<dyn ViewExt>> {
self.load_tracks(queue.get_spotify());
self.load_tracks(&queue.get_spotify());
const MAX_SEEDS: usize = 5;
let track_ids: Vec<String> = self
.tracks

View File

@@ -249,18 +249,18 @@ impl ListItem for Track {
fn toggle_saved(&mut self, library: &Library) {
if library.is_saved_track(&Playable::Track(self.clone())) {
library.unsave_tracks(vec![self], true);
library.unsave_tracks(&[self]);
} else {
library.save_tracks(vec![self], true);
library.save_tracks(&[self]);
}
}
fn save(&mut self, library: &Library) {
library.save_tracks(vec![self], true);
library.save_tracks(&[self]);
}
fn unsave(&mut self, library: &Library) {
library.unsave_tracks(vec![self], true);
library.unsave_tracks(&[self]);
}
fn open(&self, _queue: Arc<Queue>, _library: Arc<Library>) -> Option<Box<dyn ViewExt>> {