From 3e91918e742ae597226c30f3ccc4046e2cd5c6be Mon Sep 17 00:00:00 2001 From: Wojciech Kozlowski Date: Fri, 14 Apr 2023 12:17:54 +0200 Subject: [PATCH] Improve API by making signatures more generic --- src/database/json.rs | 10 ++++------ src/library/beets.rs | 29 +++++++++++++++-------------- src/tui/app.rs | 6 +++--- 3 files changed, 22 insertions(+), 23 deletions(-) diff --git a/src/database/json.rs b/src/database/json.rs index b039dad..08b8913 100644 --- a/src/database/json.rs +++ b/src/database/json.rs @@ -1,7 +1,7 @@ //! Module for storing MusicHoard data in a JSON file database. use std::fs; -use std::path::{Path, PathBuf}; +use std::path::PathBuf; use serde::de::DeserializeOwned; use serde::Serialize; @@ -60,10 +60,8 @@ pub struct JsonDatabaseFileBackend { impl JsonDatabaseFileBackend { /// Create a [`JsonDatabaseFileBackend`] that will read/write to the provided path. - pub fn new(path: &Path) -> Self { - JsonDatabaseFileBackend { - path: path.to_path_buf(), - } + pub fn new>(path: P) -> Self { + JsonDatabaseFileBackend { path: path.into() } } } @@ -144,7 +142,7 @@ mod tests { ) } - fn artists_to_json(artists: &Vec) -> String { + fn artists_to_json(artists: &[Artist]) -> String { let mut artists_strings: Vec = vec![]; for artist in artists.iter() { artists_strings.push(artist_to_json(artist)); diff --git a/src/library/beets.rs b/src/library/beets.rs index e7ad1db..072a9c7 100644 --- a/src/library/beets.rs +++ b/src/library/beets.rs @@ -3,7 +3,8 @@ use std::{ collections::{HashMap, HashSet}, - path::{Path, PathBuf}, + ffi::OsString, + path::PathBuf, process::Command, str, }; @@ -80,7 +81,7 @@ impl ToBeetsArgs for Query { #[cfg_attr(test, automock)] pub trait BeetsLibraryExecutor { /// Invoke beets with the provided arguments. - fn exec(&mut self, arguments: &[String]) -> Result, Error>; + fn exec + 'static>(&mut self, arguments: &[S]) -> Result, Error>; } /// Beets library. @@ -90,7 +91,7 @@ pub struct BeetsLibrary { trait LibraryPrivate { fn list_cmd_and_args(query: &Query) -> Vec; - fn list_to_artists(list_output: Vec) -> Result, Error>; + fn list_to_artists>(list_output: &[S]) -> Result, Error>; } impl BeetsLibrary { @@ -104,7 +105,7 @@ impl Library for BeetsLibrary { fn list(&mut self, query: &Query) -> Result, Error> { let cmd = Self::list_cmd_and_args(query); let output = self.executor.exec(&cmd)?; - Self::list_to_artists(output) + Self::list_to_artists(&output) } } @@ -116,11 +117,11 @@ impl LibraryPrivate for BeetsLibrary { cmd } - fn list_to_artists(list_output: Vec) -> Result, Error> { + fn list_to_artists>(list_output: &[S]) -> Result, Error> { let mut artists: Vec = vec![]; let mut album_ids = HashMap::>::new(); - for line in list_output.iter() { + for line in list_output.iter().map(|s| s.as_ref()) { if line.is_empty() { continue; } @@ -199,22 +200,22 @@ impl LibraryPrivate for BeetsLibrary { /// Beets library executor that executes beets commands in their own process. pub struct BeetsLibraryCommandExecutor { - bin: String, + bin: OsString, config: Option, } impl BeetsLibraryCommandExecutor { /// Create a new [`BeetsLibraryCommandExecutor`] that uses the provided beets executable. - pub fn new(bin: &str) -> Self { + pub fn new>(bin: S) -> Self { BeetsLibraryCommandExecutor { - bin: bin.to_string(), + bin: bin.into(), config: None, } } /// Update the configuration file passed to the beets executable. - pub fn config(mut self, path: Option<&Path>) -> Self { - self.config = path.map(|p| p.to_path_buf()); + pub fn config>(mut self, path: Option

) -> Self { + self.config = path.map(|p| p.into()); self } } @@ -227,13 +228,13 @@ impl Default for BeetsLibraryCommandExecutor { } impl BeetsLibraryExecutor for BeetsLibraryCommandExecutor { - fn exec(&mut self, arguments: &[String]) -> Result, Error> { + fn exec + 'static>(&mut self, arguments: &[S]) -> Result, Error> { let mut cmd = Command::new(&self.bin); if let Some(ref path) = self.config { cmd.arg("--config"); cmd.arg(path); } - let output = cmd.args(arguments).output()?; + let output = cmd.args(arguments.iter().map(|s| s.as_ref())).output()?; if !output.status.success() { return Err(Error::CmdExec( String::from_utf8_lossy(&output.stderr).to_string(), @@ -281,7 +282,7 @@ mod tests { strings } - fn artists_to_beets_string(artists: &Vec) -> Vec { + fn artists_to_beets_string(artists: &[Artist]) -> Vec { let mut strings = vec![]; for artist in artists.iter() { strings.append(&mut artist_to_beets_string(artist)); diff --git a/src/tui/app.rs b/src/tui/app.rs index 2609c9e..e6bc869 100644 --- a/src/tui/app.rs +++ b/src/tui/app.rs @@ -14,7 +14,7 @@ struct TrackSelection { } impl TrackSelection { - fn initialise(tracks: &Vec) -> Option { + fn initialise(tracks: &[Track]) -> Option { if !tracks.is_empty() { Some(TrackSelection { index: 0 }) } else { @@ -43,7 +43,7 @@ struct AlbumSelection { } impl AlbumSelection { - fn initialise(albums: &Vec) -> Option { + fn initialise(albums: &[Album]) -> Option { if !albums.is_empty() { Some(AlbumSelection { index: 0, @@ -77,7 +77,7 @@ struct ArtistSelection { } impl ArtistSelection { - fn initialise(artists: &Vec) -> Option { + fn initialise(artists: &[Artist]) -> Option { if !artists.is_empty() { Some(ArtistSelection { index: 0, -- 2.45.2