From 31d53362fad6bb823194f7effd021b95a37594f2 Mon Sep 17 00:00:00 2001 From: Ryan Cao <70191398+ryanccn@users.noreply.github.com> Date: Sun, 12 Nov 2023 15:57:22 +0800 Subject: [PATCH] refactor: make clippy pedantic --- src/cmd/discord/mod.rs | 5 +++-- src/cmd/now.rs | 25 ++++++++++++++----------- src/format.rs | 12 ++++++------ src/main.rs | 7 ++++++- src/rich_presence/ipc_impl.rs | 10 ++++------ src/rich_presence/ipc_trait.rs | 7 ++++--- src/rich_presence/pack_unpack.rs | 7 +++---- 7 files changed, 40 insertions(+), 33 deletions(-) diff --git a/src/cmd/discord/mod.rs b/src/cmd/discord/mod.rs index 62f7492..c5c3880 100644 --- a/src/cmd/discord/mod.rs +++ b/src/cmd/discord/mod.rs @@ -22,6 +22,7 @@ struct ActivityConnection { is_idle: bool, } +#[allow(clippy::cast_precision_loss, clippy::cast_possible_truncation)] async fn update_presence( client: &mut DiscordIpcClient, http_client: &reqwest::Client, @@ -95,7 +96,7 @@ async fn update_presence( &track.artist, match &metadata { Ok(metadata) => format!(" {}", metadata.id.dimmed()), - Err(_) => "".to_owned(), + Err(_) => String::new(), } ); @@ -144,7 +145,7 @@ async fn update_presence( } pub async fn discord() -> Result<()> { - let mut client = DiscordIpcClient::new("861702238472241162")?; + let mut client = DiscordIpcClient::new("861702238472241162"); client.connect().await?; println!("{} to Discord", "Connected".green()); diff --git a/src/cmd/now.rs b/src/cmd/now.rs index ca1f427..dc306ff 100644 --- a/src/cmd/now.rs +++ b/src/cmd/now.rs @@ -95,7 +95,9 @@ async fn update_state( tx.send(PlaybackStateDelta::Track(track)).await?; } - if !playlist_name.is_empty() { + if playlist_name.is_empty() { + tx.send(PlaybackStateDelta::Playlist(None)).await?; + } else { let playlist_duration = music::tell("get {duration} of current playlist") .await? .parse::()?; @@ -105,8 +107,6 @@ async fn update_state( duration: playlist_duration, }))) .await?; - } else { - tx.send(PlaybackStateDelta::Playlist(None)).await?; }; } @@ -116,19 +116,21 @@ async fn update_state( } const BAR_CHAR: &str = "━"; +#[allow(clippy::cast_possible_truncation, clippy::cast_lossless)] fn make_bar(n: f64, width: Option) -> Result { let width = width.unwrap_or(20); let part_one = (n * (width as f64)).floor() as i32; let part_two = width - part_one; - let mut ret = "".to_owned(); + let mut ret = String::new(); ret += &BAR_CHAR.repeat(part_one.try_into()?); ret += &BAR_CHAR.dimmed().to_string().repeat(part_two.try_into()?); Ok(ret) } +#[allow(clippy::unused_async, clippy::cast_possible_truncation)] async fn update_display(data: &PlaybackState, options: &NowOptions) -> Result<()> { if options.watch { execute!( @@ -152,14 +154,14 @@ async fn update_display(data: &PlaybackState, options: &NowOptions) -> Result<() println!("{}", track.name.bold()); println!( "{} {} {} {}", - if !options.no_nerd_fonts { - data.state.to_icon() - } else { + if options.no_nerd_fonts { data.state.to_string() + } else { + data.state.to_icon() }, - format::format_duration(&(position as i32), false), + format::format_duration(position as i32, false), make_bar(position / track.duration, options.bar_width)?, - format::format_duration(&(track.duration as i32), true), + format::format_duration(track.duration as i32, true), ); println!("{} · {}", track.artist.blue(), track.album.magenta()); @@ -169,7 +171,7 @@ async fn update_display(data: &PlaybackState, options: &NowOptions) -> Result<() format!( "Playlist: {} ({})", playlist.name, - format::format_duration_plain(&playlist.duration) + format::format_duration_plain(playlist.duration) ) .dimmed() ); @@ -221,7 +223,7 @@ async fn receive_delta( PlaybackStateDelta::Render => { if let Err(err) = update_display(data, options).await { - eprintln!("{}", err); + eprintln!("{err}"); }; } }; @@ -229,6 +231,7 @@ async fn receive_delta( Ok(()) } +#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)] pub async fn now(options: NowOptions) -> Result<()> { let watch = options.watch; diff --git a/src/format.rs b/src/format.rs index b7862f2..46c2043 100644 --- a/src/format.rs +++ b/src/format.rs @@ -3,9 +3,9 @@ use owo_colors::OwoColorize; const HOUR: i32 = 60 * 60; const MINUTE: i32 = 60; -pub fn format_duration(duration_secs: &i32, yellow: bool) -> String { - let mut duration_secs = *duration_secs; - let mut str = "".to_owned(); +pub fn format_duration(duration_secs: i32, yellow: bool) -> String { + let mut duration_secs = duration_secs; + let mut str = String::new(); let mut has_started = false; if has_started || duration_secs >= HOUR { @@ -48,9 +48,9 @@ pub fn format_duration(duration_secs: &i32, yellow: bool) -> String { str } -pub fn format_duration_plain(duration_secs: &i32) -> String { - let mut duration_secs = *duration_secs; - let mut str = "".to_owned(); +pub fn format_duration_plain(duration_secs: i32) -> String { + let mut duration_secs = duration_secs; + let mut str = String::new(); let mut has_started = false; if has_started || duration_secs > HOUR { diff --git a/src/main.rs b/src/main.rs index a568824..6f6f5f3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,7 @@ +#![warn(clippy::all, clippy::pedantic)] +#![allow(clippy::module_name_repetitions)] +#![deny(unsafe_code)] + use std::io::stdout; use anyhow::{anyhow, Result}; @@ -74,6 +78,7 @@ enum DiscordCommands { #[cfg(not(target_os = "macos"))] compile_error!("am doesn't work on non-macOS platforms!"); +#[allow(clippy::cast_possible_truncation)] async fn concise_now_playing() -> Result<()> { let track_data = music::tell_raw(&[ "set output to \"\"", @@ -111,7 +116,7 @@ async fn concise_now_playing() -> Result<()> { println!( "{} {}\n{} · {}", name.bold(), - format::format_duration_plain(&(duration as i32)).dimmed(), + format::format_duration_plain(duration as i32).dimmed(), artist.blue(), album.magenta(), ); diff --git a/src/rich_presence/ipc_impl.rs b/src/rich_presence/ipc_impl.rs index 7f2f0d1..b21a9da 100644 --- a/src/rich_presence/ipc_impl.rs +++ b/src/rich_presence/ipc_impl.rs @@ -33,14 +33,12 @@ impl DiscordIpcClient { /// ``` /// let ipc_client = DiscordIpcClient::new("")?; /// ``` - pub fn new(client_id: &str) -> Result { - let client = Self { + pub fn new(client_id: &str) -> Self { + Self { client_id: client_id.to_string(), connected: false, socket: None, - }; - - Ok(client) + } } fn get_pipe_pattern() -> PathBuf { @@ -64,7 +62,7 @@ impl DiscordIpcClient { impl DiscordIpc for DiscordIpcClient { async fn connect_ipc(&mut self) -> Result<()> { for i in 0..10 { - let path = DiscordIpcClient::get_pipe_pattern().join(format!("discord-ipc-{}", i)); + let path = DiscordIpcClient::get_pipe_pattern().join(format!("discord-ipc-{i}")); match UnixStream::connect(&path).await { Ok(socket) => { diff --git a/src/rich_presence/ipc_trait.rs b/src/rich_presence/ipc_trait.rs index ebcc3f1..8f4c6e2 100644 --- a/src/rich_presence/ipc_trait.rs +++ b/src/rich_presence/ipc_trait.rs @@ -11,6 +11,7 @@ use uuid::Uuid; /// A client that connects to and communicates with the Discord IPC. /// /// Implemented via the [`DiscordIpcClient`](struct@crate::rich_presence::DiscordIpcClient) struct. +#[allow(clippy::cast_possible_truncation)] #[async_trait] pub trait DiscordIpc { /// Connects the client to the Discord IPC. @@ -112,7 +113,7 @@ pub trait DiscordIpc { /// ``` async fn send(&mut self, data: Value, opcode: u8) -> Result<()> { let data_string = data.to_string(); - let header = pack(opcode.into(), data_string.len() as u32)?; + let header = pack(opcode.into(), data_string.len() as u32); self.write(&header).await?; self.write(data_string.as_bytes()).await?; @@ -143,12 +144,12 @@ pub trait DiscordIpc { let mut header = [0; 8]; self.read(&mut header).await?; - let (op, length) = unpack(header.to_vec())?; + let (op, length) = unpack(&header)?; let mut data = vec![0u8; length as usize]; self.read(&mut data).await?; - let response = String::from_utf8(data.to_vec())?; + let response = String::from_utf8(data.clone())?; let json_data = serde_json::from_str::(&response)?; Ok((op, json_data)) diff --git a/src/rich_presence/pack_unpack.rs b/src/rich_presence/pack_unpack.rs index f81a60b..e57f677 100644 --- a/src/rich_presence/pack_unpack.rs +++ b/src/rich_presence/pack_unpack.rs @@ -2,18 +2,17 @@ use anyhow::Result; use std::convert::TryInto; // Re-implement some packing methods in Rust -pub fn pack(opcode: u32, data_len: u32) -> Result> { +pub fn pack(opcode: u32, data_len: u32) -> Vec { let mut bytes = Vec::new(); for byte_array in &[opcode.to_le_bytes(), data_len.to_le_bytes()] { bytes.extend_from_slice(byte_array); } - Ok(bytes) + bytes } -pub fn unpack(data: Vec) -> Result<(u32, u32)> { - let data = data.as_slice(); +pub fn unpack(data: &[u8]) -> Result<(u32, u32)> { let (opcode, header) = data.split_at(std::mem::size_of::()); let opcode = u32::from_le_bytes(opcode.try_into()?);