Skip to content

Commit

Permalink
refactor: fix various clippy warnigns
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeStanger committed Mar 29, 2023
1 parent a0421a8 commit 4c22c0e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 22 deletions.
12 changes: 6 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ async fn idle(hosts: &[String]) -> MPDClient {

#[tokio::main]
async fn main() {
let re = Regex::new(r"\$(\w+)").unwrap();
let re = Regex::new(r"\$(\w+)").expect("Failed to parse regex");

let config = Config::load();
let format = &config.format;
Expand Down Expand Up @@ -91,23 +91,23 @@ async fn main() {
assets = assets.small_image(&format.small_image);
}
if !large_text.is_empty() {
assets = assets.large_text(large_text)
assets = assets.large_text(large_text);
}
if !small_text.is_empty() {
assets = assets.small_text(small_text)
assets = assets.small_text(small_text);
}
assets
})
.timestamps(|_| timestamps)
});

if let Err(why) = res {
eprintln!("Failed to set activity: {:?}", why);
eprintln!("Failed to set activity: {why:?}");
};
}
} else {
if let Err(why) = drpc.clear_activity() {
eprintln!("Failed to clear activity: {}", why);
eprintln!("Failed to clear activity: {why:?}");
};

mpd = idle(&config.hosts).await;
Expand Down Expand Up @@ -136,7 +136,7 @@ async fn replace_tokens(
let mut compiled_string = format_string.to_string();
for token in tokens {
let value = mpd_conn::get_token_value(mpd, song, token).await;
compiled_string = compiled_string.replace(format!("${}", token).as_str(), value.as_str());
compiled_string = compiled_string.replace(format!("${token}").as_str(), value.as_str());
}
compiled_string
}
28 changes: 12 additions & 16 deletions src/mpd_conn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use std::os::unix::fs::FileTypeExt;
/// Cycles through each MPD host and
/// returns the first one which is playing,
/// or none if one is not found.
pub(crate) async fn try_get_mpd_conn(hosts: &[String]) -> Option<MPDClient> {
pub async fn try_get_mpd_conn(hosts: &[String]) -> Option<MPDClient> {
for host in hosts {
let connection = if is_unix_socket(host) {
connect_unix(host).await
Expand All @@ -30,7 +30,7 @@ pub(crate) async fn try_get_mpd_conn(hosts: &[String]) -> Option<MPDClient> {
return Some(client);
}
}
Err(why) => eprintln!("Error connecting to {}: {}", host, why),
Err(why) => eprintln!("Error connecting to {host}: {why:?}"),
}
}

Expand All @@ -40,23 +40,22 @@ pub(crate) async fn try_get_mpd_conn(hosts: &[String]) -> Option<MPDClient> {
fn is_unix_socket(host: &String) -> bool {
let path = PathBuf::from(host);
path.exists()
&& match path.metadata() {
Ok(metadata) => metadata.file_type().is_socket(),
Err(_) => false,
}
&& path
.metadata()
.map_or(false, |metadata| metadata.file_type().is_socket())
}

async fn connect_unix(host: &String) -> Result<Connection, MpdProtocolError> {
let connection = UnixStream::connect(host)
.await
.unwrap_or_else(|_| panic!("Error connecting to unix socket: {}", host));
.unwrap_or_else(|_| panic!("Error connecting to unix socket: {host}"));
MPDClient::connect(connection).await
}

async fn connect_tcp(host: &String) -> Result<Connection, MpdProtocolError> {
let connection = TcpStream::connect(host)
.await
.unwrap_or_else(|_| panic!("Error connecting to unix socket: {}", host));
.unwrap_or_else(|_| panic!("Error connecting to unix socket: {host}"));
MPDClient::connect(connection).await
}

Expand All @@ -66,12 +65,12 @@ fn format_time(time: u64) -> String {
let minutes = (time / 60) % 60;
let seconds = time % 60;

format!("{:0>2}:{:0>2}", minutes, seconds)
format!("{minutes:0>2}:{seconds:0>2}")
}

/// Converts a string format token value
/// into its respective MPD value.
pub(crate) async fn get_token_value(client: &mut MPDClient, song: &Song, token: &str) -> String {
pub async fn get_token_value(client: &mut MPDClient, song: &Song, token: &str) -> String {
match token {
"title" => song.title(),
"album" => try_get_first_tag(song.tags.get(&Tag::Album)),
Expand Down Expand Up @@ -115,7 +114,7 @@ pub async fn get_timestamp(client: &mut MPDClient, mode: TimestampMode) -> Activ

/// Gets MPD server status.
/// Panics on error.
pub(crate) async fn get_status(client: &MPDClient) -> Status {
pub async fn get_status(client: &MPDClient) -> Status {
client
.command(commands::Status)
.await
Expand All @@ -124,11 +123,8 @@ pub(crate) async fn get_status(client: &MPDClient) -> Status {

/// Attempts to read the first value for a tag
/// (since the MPD client returns a vector of tags, or None)
pub(crate) fn try_get_first_tag(vec: Option<&Vec<String>>) -> Option<&str> {
match vec {
Some(vec) => vec.first().map(|val| val.as_str()),
None => None,
}
pub fn try_get_first_tag(vec: Option<&Vec<String>>) -> Option<&str> {
vec.and_then(|vec| vec.first().map(String::as_str))
}

/// Gets the duration of the current song
Expand Down

0 comments on commit 4c22c0e

Please sign in to comment.