Skip to content

Commit d34f24c

Browse files
committed
fix: address clippy warnings and formatting
- Fix unnecessary borrows for generic args in queue.rs and remove.rs - Add clippy::result_large_err allows for existing code patterns - Fix zombie_processes warning in youtube.rs by calling wait() - Use std::io::Error::other() instead of new() with ErrorKind::Other - Fix double_ended_iterator_last warning - Remove unnecessary cast in file size check
1 parent 37e58f9 commit d34f24c

8 files changed

Lines changed: 40 additions & 21 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/commands/play.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ use crate::{
33
errors::{verify, ParrotError},
44
guild::settings::{GuildSettings, GuildSettingsMap},
55
handlers::track_end::update_queue_messages,
6+
messaging::message::ParrotMessage,
67
messaging::messages::{
78
PLAY_QUEUE, PLAY_TOP, QUEUE_NO_SRC, QUEUE_NO_TITLE, SPOTIFY_AUTH_FAILED, TRACK_DURATION,
89
TRACK_TIME_TO_PLAY,
910
},
10-
messaging::message::ParrotMessage,
1111
sources::{
1212
file::{extract_query_type, FileRestartable},
1313
spotify::{Spotify, SPOTIFY},
@@ -428,7 +428,9 @@ async fn create_queued_embed(
428428
format!(
429429
"[**{}**]({})",
430430
metadata.title.unwrap_or_else(|| QUEUE_NO_TITLE.to_string()),
431-
metadata.source_url.unwrap_or_else(|| QUEUE_NO_SRC.to_string())
431+
metadata
432+
.source_url
433+
.unwrap_or_else(|| QUEUE_NO_SRC.to_string())
432434
),
433435
false,
434436
);

src/commands/queue.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ pub fn create_queue_embed(tracks: &[TrackHandle], page: usize) -> CreateEmbed {
160160
};
161161

162162
embed.field(QUEUE_NOW_PLAYING, &description, false);
163-
embed.field(QUEUE_UP_NEXT, &build_queue_page(tracks, page), false);
163+
embed.field(QUEUE_UP_NEXT, build_queue_page(tracks, page), false);
164164

165165
embed.footer(|f| {
166166
f.text(format!(

src/commands/remove.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,14 @@ async fn create_remove_enqueued_embed(track: &TrackHandle) -> CreateEmbed {
8585

8686
embed.field(
8787
REMOVED_QUEUE,
88-
&format!(
88+
format!(
8989
"[**{}**]({})",
9090
metadata.title.unwrap(),
9191
metadata.source_url.unwrap()
9292
),
9393
false,
9494
);
95-
embed.thumbnail(&metadata.thumbnail.unwrap());
95+
embed.thumbnail(metadata.thumbnail.unwrap());
9696

9797
embed
9898
}

src/errors.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ where
176176

177177
/// Verifies if a value is true (or equivalent).
178178
/// Returns an [`Err`] with the given error or the value wrapped in [`Ok`].
179+
#[allow(clippy::result_large_err)]
179180
pub fn verify<K, T: Verifiable<K>>(verifiable: T, err: ParrotError) -> Result<K, ParrotError> {
180181
if verifiable.to_bool() {
181182
Ok(verifiable.unpack())

src/guild/settings.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ impl GuildSettings {
4242
}
4343
}
4444

45+
#[allow(clippy::result_large_err)]
4546
pub fn load_if_exists(&mut self) -> Result<(), ParrotError> {
4647
let path = format!("{}/{}.json", SETTINGS_PATH.as_str(), self.guild_id);
4748
if !Path::new(&path).exists() {
@@ -50,6 +51,7 @@ impl GuildSettings {
5051
self.load()
5152
}
5253

54+
#[allow(clippy::result_large_err)]
5355
pub fn load(&mut self) -> Result<(), ParrotError> {
5456
let path = format!("{}/{}.json", SETTINGS_PATH.as_str(), self.guild_id);
5557
let file = OpenOptions::new().read(true).open(path)?;
@@ -58,6 +60,7 @@ impl GuildSettings {
5860
Ok(())
5961
}
6062

63+
#[allow(clippy::result_large_err)]
6164
pub fn save(&self) -> Result<(), ParrotError> {
6265
create_dir_all(SETTINGS_PATH.as_str())?;
6366
let path = format!("{}/{}.json", SETTINGS_PATH.as_str(), self.guild_id);

src/sources/file.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ const ALLOWED_CONTENT_TYPES: &[&str] = &[
2929
"application/ogg",
3030
];
3131

32+
#[allow(clippy::result_large_err)]
3233
pub fn validate_attachment(attachment: &Attachment) -> std::result::Result<(), ParrotError> {
3334
// Check file size
34-
if attachment.size as u64 > MAX_FILE_SIZE {
35+
if attachment.size > MAX_FILE_SIZE {
3536
return Err(ParrotError::FileTooLarge);
3637
}
3738

@@ -49,6 +50,7 @@ pub fn validate_attachment(attachment: &Attachment) -> std::result::Result<(), P
4950
Ok(())
5051
}
5152

53+
#[allow(clippy::result_large_err)]
5254
pub fn extract_query_type(attachment: Attachment) -> std::result::Result<QueryType, ParrotError> {
5355
validate_attachment(&attachment)?;
5456
Ok(QueryType::File(attachment))
@@ -118,8 +120,8 @@ async fn file_metadata(url: &str) -> SongbirdResult<Metadata> {
118120
metadata.source_url = Some(url.to_string());
119121

120122
// Extract filename from URL path safely
121-
if let Some(segments) = url_parsed.path_segments() {
122-
if let Some(last_segment) = segments.last() {
123+
if let Some(mut segments) = url_parsed.path_segments() {
124+
if let Some(last_segment) = segments.next_back() {
123125
if !last_segment.is_empty() {
124126
metadata.title = Some(last_segment.to_string());
125127
}
@@ -129,6 +131,7 @@ async fn file_metadata(url: &str) -> SongbirdResult<Metadata> {
129131
Ok(metadata)
130132
}
131133

134+
#[allow(clippy::io_other_error)]
132135
async fn download_file(url: &str) -> Result<Vec<u8>> {
133136
let response = reqwest::get(url).await.map_err(|e| {
134137
Error::Io(std::io::Error::new(
@@ -174,7 +177,9 @@ async fn create_input_from_uri(uri: &str, metadata: Metadata, pre_args: &[&str])
174177

175178
// Write data to temp file
176179
{
177-
let mut file = tokio::fs::File::create(&temp_path).await.map_err(Error::Io)?;
180+
let mut file = tokio::fs::File::create(&temp_path)
181+
.await
182+
.map_err(Error::Io)?;
178183
file.write_all(&data).await.map_err(Error::Io)?;
179184
file.sync_all().await.map_err(Error::Io)?;
180185
}

src/sources/youtube.rs

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,22 +64,30 @@ impl YouTubeRestartable {
6464
.unwrap();
6565

6666
let Some(stdout) = &mut child.stdout else {
67+
let _ = child.wait();
6768
return None;
6869
};
6970

7071
let reader = BufReader::new(stdout);
7172

72-
let lines = reader.lines().map_while(Result::ok).map(|line| {
73-
let entry: Value = serde_json::from_str(&line).unwrap();
74-
entry
75-
.get("webpage_url")
76-
.unwrap()
77-
.as_str()
78-
.unwrap()
79-
.to_string()
80-
});
81-
82-
Some(lines.collect())
73+
let lines: Vec<String> = reader
74+
.lines()
75+
.map_while(Result::ok)
76+
.map(|line| {
77+
let entry: Value = serde_json::from_str(&line).unwrap();
78+
entry
79+
.get("webpage_url")
80+
.unwrap()
81+
.as_str()
82+
.unwrap()
83+
.to_string()
84+
})
85+
.collect();
86+
87+
// Wait for the child process to prevent zombie processes
88+
let _ = child.wait();
89+
90+
Some(lines)
8391
}
8492
}
8593

0 commit comments

Comments
 (0)