Fixed cargo clippy suggestions

This commit is contained in:
G2-Games 2023-11-24 14:48:15 -06:00
parent 755c62f702
commit 6b58ac02cf
7 changed files with 23 additions and 29 deletions

View file

@ -1,6 +1,4 @@
pub mod music_tracker {
pub mod music_tracker;
}
pub mod music_storage {
pub mod music_db;
@ -10,5 +8,5 @@ pub mod music_storage {
pub mod music_controller {
pub mod config;
pub mod music_controller;
pub mod controller;
}

View file

@ -4,7 +4,7 @@ use std::path::PathBuf;
use serde::{Deserialize, Serialize};
use crate::music_tracker::music_tracker::{DiscordRPCConfig, LastFMConfig, ListenBrainzConfig};
use crate::music_tracker::{DiscordRPCConfig, LastFMConfig, ListenBrainzConfig};
#[derive(Serialize, Deserialize, PartialEq, Eq)]
pub struct Config {
@ -66,9 +66,8 @@ impl Config {
fs::write(&temp_file, toml)?;
// If configuration file already exists, delete it
match fs::metadata(config_file) {
Ok(_) => fs::remove_file(config_file)?,
Err(_) => {}
if fs::metadata(config_file).is_ok() {
fs::remove_file(config_file)?
}
fs::rename(temp_file, config_file)?;

View file

@ -199,6 +199,7 @@ pub struct Album<'a> {
discs: BTreeMap<usize, Vec<&'a Song>>,
}
#[allow(clippy::len_without_is_empty)]
impl Album<'_> {
/// Returns the album title
pub fn title(&self) -> &String {
@ -383,7 +384,7 @@ impl MusicLibrary {
} // TODO: Handle more of these errors
};
} else if extension == "cue" {
total += match self.add_cuesheet(&target_file.path().to_path_buf()) {
total += match self.add_cuesheet(target_file.path()) {
Ok(added) => added,
Err(error) => {
errors += 1;
@ -457,7 +458,7 @@ impl MusicLibrary {
}
// Find images around the music file that can be used
let mut found_images = find_images(&target_file.to_path_buf()).unwrap();
let mut found_images = find_images(target_file).unwrap();
album_art.append(&mut found_images);
// Get the format as a string
@ -497,10 +498,10 @@ impl MusicLibrary {
Ok(())
}
pub fn add_cuesheet(&mut self, cuesheet: &PathBuf) -> Result<usize, Box<dyn Error>> {
pub fn add_cuesheet(&mut self, cuesheet: &Path) -> Result<usize, Box<dyn Error>> {
let mut tracks_added = 0;
let cue_data = parse_from_file(&cuesheet.as_path().to_string_lossy(), false).unwrap();
let cue_data = parse_from_file(&cuesheet.to_string_lossy(), false).unwrap();
// Get album level information
let album_title = &cue_data.title;
@ -515,10 +516,7 @@ impl MusicLibrary {
}
// Try to remove the original audio file from the db if it exists
match self.remove_uri(&URI::Local(audio_location.clone())) {
Ok(_) => tracks_added -= 1,
Err(_) => ()
};
if self.remove_uri(&URI::Local(audio_location.clone())).is_ok() { tracks_added -= 1 }
let next_track = file.tracks.clone();
let mut next_track = next_track.iter().skip(1);
@ -626,12 +624,10 @@ impl MusicLibrary {
}
pub fn add_song(&mut self, new_song: Song) -> Result<(), Box<dyn Error>> {
match self.query_uri(&new_song.location) {
Some(_) => {
if self.query_uri(&new_song.location).is_some() {
return Err(format!("URI already in database: {:?}", new_song.location).into())
}
None => (),
}
match new_song.location {
URI::Local(_) if self.query_path(new_song.location.path()).is_some() => {
return Err(format!("Location exists for {:?}", new_song.location).into())
@ -854,7 +850,7 @@ impl MusicLibrary {
/// Queries a list of albums by title
pub fn query_albums(
&self,
query_string: &String, // The query itself
query_string: &str, // The query itself
) -> Result<Vec<Album>, Box<dyn Error>> {
let all_albums = self.albums();

View file

@ -1,6 +1,6 @@
use crate::music_controller::config::Config;
use std::path::Path;
pub fn playlist_add(_config: &Config, _playlist_name: &str, _song_paths: &Vec<&Path>) {
pub fn playlist_add(_config: &Config, _playlist_name: &str, _song_paths: &[&Path]) {
unimplemented!()
}

View file

@ -1,5 +1,6 @@
use std::io::{BufReader, BufWriter};
use std::{error::Error, fs, path::PathBuf};
use std::path::{Path, PathBuf};
use std::{error::Error, fs};
use walkdir::WalkDir;
use file_format::{FileFormat, Kind};
@ -8,7 +9,7 @@ use snap;
use super::music_db::{Song, AlbumArt, URI};
use unidecode::unidecode;
pub(super) fn normalize(input_string: &String) -> String {
pub(super) fn normalize(input_string: &str) -> String {
let mut normalized = unidecode(input_string);
// Remove non alphanumeric characters
@ -65,7 +66,7 @@ pub(super) fn write_library(
Ok(())
}
pub fn find_images(song_path: &PathBuf) -> Result<Vec<AlbumArt>, Box<dyn Error>> {
pub fn find_images(song_path: &Path) -> Result<Vec<AlbumArt>, Box<dyn Error>> {
let mut images: Vec<AlbumArt> = Vec::new();
let song_dir = song_path.parent().ok_or("")?;

View file

@ -422,12 +422,12 @@ impl ListenBrainz {
// Makes an api request to configured url with given json
pub async fn api_request(
&self,
request: &String,
request: &str,
endpoint: &String,
) -> Result<surf::Response, surf::Error> {
surf::post(format!("{}{}", &self.config.api_url, endpoint))
.body_string(request.clone())
.body_string(request.to_owned())
.header("Authorization", format!("Token {}", self.config.auth_token))
.await
}