From 8222f1b2e46c8e8251cbfb14627bb67ffffaa70d Mon Sep 17 00:00:00 2001 From: Henrik Friedrichsen Date: Mon, 6 Feb 2023 20:06:25 +0100 Subject: [PATCH] Use variables directly inside format strings Most systems should have an up to date Rust compiler by now, so we should be fine. --- src/command.rs | 16 ++++++++-------- src/config.rs | 2 +- src/main.rs | 7 +++---- src/model/album.rs | 4 ++-- src/model/artist.rs | 6 +++--- src/model/episode.rs | 2 +- src/model/playable.rs | 2 +- src/model/playlist.rs | 2 +- src/model/show.rs | 2 +- src/model/track.rs | 6 +++--- src/mpris.rs | 2 +- src/serialization.rs | 4 ++-- src/sharing.rs | 2 +- src/spotify_worker.rs | 6 ++---- src/ui/layout.rs | 7 ++----- src/ui/library.rs | 2 +- src/utils.rs | 2 +- 17 files changed, 34 insertions(+), 40 deletions(-) diff --git a/src/command.rs b/src/command.rs index d94ef80..1f6d846 100644 --- a/src/command.rs +++ b/src/command.rs @@ -90,12 +90,12 @@ pub enum SeekDirection { impl fmt::Display for SeekDirection { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let repr = match self { - SeekDirection::Absolute(pos) => format!("{}", pos), + SeekDirection::Absolute(pos) => format!("{pos}"), SeekDirection::Relative(delta) => { format!("{}{}", if delta > &0 { "+" } else { "" }, delta) } }; - write!(f, "{}", repr) + write!(f, "{repr}") } } @@ -113,7 +113,7 @@ impl fmt::Display for InsertSource { InsertSource::Clipboard => "".into(), InsertSource::Input(url) => url.to_string(), }; - write!(f, "{}", repr) + write!(f, "{repr}") } } @@ -315,12 +315,12 @@ impl fmt::Display for CommandParseError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { use CommandParseError::*; let formatted = match self { - NoSuchCommand { cmd } => format!("No such command \"{}\"", cmd), + NoSuchCommand { cmd } => format!("No such command \"{cmd}\""), InsufficientArgs { cmd, hint } => { if let Some(hint_str) = hint { - format!("\"{}\" requires additional arguments: {}", cmd, hint_str) + format!("\"{cmd}\" requires additional arguments: {hint_str}") } else { - format!("\"{}\" requires additional arguments", cmd) + format!("\"{cmd}\" requires additional arguments") } } BadEnumArg { arg, accept } => { @@ -330,9 +330,9 @@ impl fmt::Display for CommandParseError { accept.join("|") ) } - ArgParseError { arg, err } => format!("Error with argument \"{}\": {}", arg, err), + ArgParseError { arg, err } => format!("Error with argument \"{arg}\": {err}"), }; - write!(f, "{}", formatted) + write!(f, "{formatted}") } } diff --git a/src/config.rs b/src/config.rs index 91e70a8..4dec449 100644 --- a/src/config.rs +++ b/src/config.rs @@ -170,7 +170,7 @@ pub struct Config { impl Config { pub fn new(filename: &str) -> Self { let values = load(filename).unwrap_or_else(|e| { - eprintln!("could not load config: {}", e); + eprintln!("could not load config: {e}"); process::exit(1); }); diff --git a/src/main.rs b/src/main.rs index bcb1bef..f76d907 100644 --- a/src/main.rs +++ b/src/main.rs @@ -85,8 +85,7 @@ fn credentials_prompt(error_message: Option) -> Result Result<(), String> { }; while let Err(error) = spotify::Spotify::test_credentials(credentials.clone()) { - let error_msg = format!("{}", error); + let error_msg = format!("{error}"); credentials = credentials_prompt(Some(error_msg))?; } diff --git a/src/model/album.rs b/src/model/album.rs index dd6b924..e248792 100644 --- a/src/model/album.rs +++ b/src/model/album.rs @@ -170,7 +170,7 @@ impl ListItem for Album { } fn display_left(&self, _library: Arc) -> String { - format!("{}", self) + format!("{self}") } fn display_right(&self, library: Arc) -> String { @@ -289,7 +289,7 @@ impl ListItem for Album { fn share_url(&self) -> Option { self.id .clone() - .map(|id| format!("https://open.spotify.com/album/{}", id)) + .map(|id| format!("https://open.spotify.com/album/{id}")) } fn artists(&self) -> Option> { diff --git a/src/model/artist.rs b/src/model/artist.rs index cf57e55..e2f40f8 100644 --- a/src/model/artist.rs +++ b/src/model/artist.rs @@ -95,7 +95,7 @@ impl ListItem for Artist { } fn display_left(&self, _library: Arc) -> String { - format!("{}", self) + format!("{self}") } fn display_right(&self, library: Arc) -> String { @@ -115,7 +115,7 @@ impl ListItem for Artist { "".into() }; - format!("{}{}", followed, tracks) + format!("{followed}{tracks}") } fn play(&mut self, queue: Arc) { @@ -199,7 +199,7 @@ impl ListItem for Artist { fn share_url(&self) -> Option { self.id .clone() - .map(|id| format!("https://open.spotify.com/artist/{}", id)) + .map(|id| format!("https://open.spotify.com/artist/{id}")) } #[inline] diff --git a/src/model/episode.rs b/src/model/episode.rs index ec61a7e..7f589b4 100644 --- a/src/model/episode.rs +++ b/src/model/episode.rs @@ -25,7 +25,7 @@ impl Episode { pub fn duration_str(&self) -> String { let minutes = self.duration / 60_000; let seconds = (self.duration / 1000) % 60; - format!("{:02}:{:02}", minutes, seconds) + format!("{minutes:02}:{seconds:02}") } } diff --git a/src/model/playable.rs b/src/model/playable.rs index 9fd077e..2611b88 100644 --- a/src/model/playable.rs +++ b/src/model/playable.rs @@ -121,7 +121,7 @@ impl Playable { let duration = self.duration(); let minutes = duration / 60_000; let seconds = (duration / 1000) % 60; - format!("{:02}:{:02}", minutes, seconds) + format!("{minutes:02}:{seconds:02}") } pub fn as_listitem(&self) -> Box { diff --git a/src/model/playlist.rs b/src/model/playlist.rs index d04e84d..dba4318 100644 --- a/src/model/playlist.rs +++ b/src/model/playlist.rs @@ -216,7 +216,7 @@ impl ListItem for Playlist { .map(|t| t.len()) .unwrap_or(self.num_tracks); - format!("{}{:>4} tracks", saved, num_tracks) + format!("{saved}{num_tracks:>4} tracks") } fn play(&mut self, queue: Arc) { diff --git a/src/model/show.rs b/src/model/show.rs index 398af14..0b4c3a4 100644 --- a/src/model/show.rs +++ b/src/model/show.rs @@ -77,7 +77,7 @@ impl ListItem for Show { } fn display_left(&self, _library: Arc) -> String { - format!("{}", self) + format!("{self}") } fn display_right(&self, library: Arc) -> String { diff --git a/src/model/track.rs b/src/model/track.rs index c2ca9e8..bc497b6 100644 --- a/src/model/track.rs +++ b/src/model/track.rs @@ -74,7 +74,7 @@ impl Track { pub fn duration_str(&self) -> String { let minutes = self.duration / 60_000; let seconds = (self.duration / 1000) % 60; - format!("{:02}:{:02}", minutes, seconds) + format!("{minutes:02}:{seconds:02}") } } @@ -194,7 +194,7 @@ impl ListItem for Track { if left != default { Playable::format(&Playable::Track(self.clone()), &left, library) } else { - format!("{}", self) + format!("{self}") } } @@ -307,7 +307,7 @@ impl ListItem for Track { fn share_url(&self) -> Option { self.id .clone() - .map(|id| format!("https://open.spotify.com/track/{}", id)) + .map(|id| format!("https://open.spotify.com/track/{id}")) } fn album(&self, queue: Arc) -> Option { diff --git a/src/mpris.rs b/src/mpris.rs index 02a493f..1f23ea6 100644 --- a/src/mpris.rs +++ b/src/mpris.rs @@ -566,7 +566,7 @@ fn run_dbus_server( let captures = regex.captures(s).unwrap(); let uri_type = &captures[2]; let id = &captures[3]; - format!("spotify:{}:{}", uri_type, id) + format!("spotify:{uri_type}:{id}") }else { s.to_string() }; diff --git a/src/serialization.rs b/src/serialization.rs index 538db4c..4fd83f2 100644 --- a/src/serialization.rs +++ b/src/serialization.rs @@ -54,8 +54,8 @@ impl Serializer for TomlSerializer { } fn write, T: serde::Serialize>(&self, path: P, value: T) -> Result { - let content = toml::to_string_pretty(&value) - .map_err(|e| format!("Failed serializing value: {}", e))?; + let content = + toml::to_string_pretty(&value).map_err(|e| format!("Failed serializing value: {e}"))?; fs::write(path.as_ref(), content) .map(|_| value) .map_err(|e| { diff --git a/src/sharing.rs b/src/sharing.rs index 18644e4..fe66f48 100644 --- a/src/sharing.rs +++ b/src/sharing.rs @@ -67,7 +67,7 @@ pub fn read_share() -> Option { string = None } Err(err) => { - eprintln!("{}", err); + eprintln!("{err}"); string = None } } diff --git a/src/spotify_worker.rs b/src/spotify_worker.rs index b1222fb..e24677b 100644 --- a/src/spotify_worker.rs +++ b/src/spotify_worker.rs @@ -78,10 +78,8 @@ impl Worker { ) -> Pin + Send>> { let client_id = config::CLIENT_ID; let scopes = "user-read-private,playlist-read-private,playlist-read-collaborative,playlist-modify-public,playlist-modify-private,user-follow-modify,user-follow-read,user-library-read,user-library-modify,user-top-read,user-read-recently-played"; - let url = format!( - "hm://keymaster/token/authenticated?client_id={}&scope={}", - client_id, scopes - ); + let url = + format!("hm://keymaster/token/authenticated?client_id={client_id}&scope={scopes}"); Box::pin( self.session .mercury() diff --git a/src/ui/layout.rs b/src/ui/layout.rs index 48ef248..8e85300 100644 --- a/src/ui/layout.rs +++ b/src/ui/layout.rs @@ -218,7 +218,7 @@ impl View for Layout { // back button + title if !self.is_current_stack_empty() { printer.with_color(ColorStyle::title_secondary(), |printer| { - printer.print((1, 0), &format!("< {}", screen_title)); + printer.print((1, 0), &format!("< {screen_title}")); }); } @@ -255,10 +255,7 @@ impl View for Layout { printer.with_color(style, |printer| { printer.print_hline((0, printer.size.y - cmdline_height), printer.size.x, " "); - printer.print( - (0, printer.size.y - cmdline_height), - &format!("ERROR: {}", e), - ); + printer.print((0, printer.size.y - cmdline_height), &format!("ERROR: {e}")); }); } diff --git a/src/ui/library.rs b/src/ui/library.rs index 5cdf2a1..cb3b49e 100644 --- a/src/ui/library.rs +++ b/src/ui/library.rs @@ -83,7 +83,7 @@ impl ViewWrapper for LibraryView { impl ViewExt for LibraryView { fn title(&self) -> String { if let Some(name) = &self.display_name { - format!("Library of {}", name) + format!("Library of {name}") } else { "Library".to_string() } diff --git a/src/utils.rs b/src/utils.rs index e3fd1ba..a8140e3 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -9,7 +9,7 @@ pub fn format_duration(d: &std::time::Duration) -> String { let mut s = String::new(); let mut append_unit = |value, unit| { if value > 0 { - let _ = write!(s, "{}{}", value, unit); + let _ = write!(s, "{value}{unit}"); } };