Ran cargo clippy

This commit is contained in:
G2-Games 2023-11-24 14:25:31 -06:00
parent ba4ed346ce
commit 048254150c
5 changed files with 48 additions and 52 deletions

View file

@ -18,7 +18,7 @@ impl Default for Config {
fn default() -> Self {
let path = PathBuf::from("./music_database");
return Config {
Config {
db_path: Box::new(path),
lastfm: None,
@ -34,7 +34,7 @@ impl Default for Config {
api_url: String::from("https://api.listenbrainz.org"),
auth_token: String::from(""),
}),
};
}
}
}
@ -49,10 +49,10 @@ impl Config {
/// Loads config from given file path
pub fn from(config_file: &PathBuf) -> std::result::Result<Config, toml::de::Error> {
return toml::from_str(
toml::from_str(
&read_to_string(config_file)
.expect("Failed to initalize music config: File not found!"),
);
)
}
/// Saves config to given path

View file

@ -23,7 +23,7 @@ impl MusicController {
library,
};
return Ok(controller);
Ok(controller)
}
/// Creates new music controller from a config at given path
@ -39,7 +39,7 @@ impl MusicController {
library,
};
return Ok(controller);
Ok(controller)
}
/// Queries the [MusicLibrary], returning a `Vec<Song>`

View file

@ -115,10 +115,7 @@ impl Song {
"location" => Some(self.location.clone().path_string()),
"plays" => Some(self.plays.clone().to_string()),
"format" => match self.format {
Some(format) => match format.short_name() {
Some(short) => Some(short.to_string()),
None => None,
},
Some(format) => format.short_name().map(|short| short.to_string()),
None => None,
},
_ => todo!(), // Other field types are not yet supported
@ -321,10 +318,9 @@ impl MusicLibrary {
/// with matching `PathBuf`s
fn query_path(&self, path: &PathBuf) -> Option<Vec<&Song>> {
let result: Arc<Mutex<Vec<&Song>>> = Arc::new(Mutex::new(Vec::new()));
let _ = self.library.par_iter().for_each(|track| {
self.library.par_iter().for_each(|track| {
if path == track.location.path() {
result.clone().lock().unwrap().push(&track);
return;
result.clone().lock().unwrap().push(track);
}
});
if result.lock().unwrap().len() > 0 {
@ -368,7 +364,7 @@ impl MusicLibrary {
}
*/
let format = FileFormat::from_file(&path)?;
let format = FileFormat::from_file(path)?;
let extension = match path.extension() {
Some(ext) => ext.to_string_lossy().to_ascii_lowercase(),
None => String::new(),
@ -379,7 +375,7 @@ impl MusicLibrary {
if (format.kind() == Kind::Audio || format.kind() == Kind::Video)
&& !BLOCKED_EXTENSIONS.contains(&extension.as_str())
{
match self.add_file(&target_file.path()) {
match self.add_file(target_file.path()) {
Ok(_) => total += 1,
Err(_error) => {
errors += 1;
@ -399,7 +395,7 @@ impl MusicLibrary {
}
// Save the database after scanning finishes
self.save(&config).unwrap();
self.save(config).unwrap();
println!("ERRORS: {}", errors);
@ -455,7 +451,7 @@ impl MusicLibrary {
// Get all the album artwork information from the file
let mut album_art: Vec<AlbumArt> = Vec::new();
for (i, _art) in tag.pictures().iter().enumerate() {
let new_art = AlbumArt::Embedded(i as usize);
let new_art = AlbumArt::Embedded(i);
album_art.push(new_art)
}
@ -547,10 +543,10 @@ impl MusicLibrary {
None => Duration::from_secs(0)
}
None => {
match lofty::read_from_path(&audio_location) {
match lofty::read_from_path(audio_location) {
Ok(tagged_file) => tagged_file.properties().duration() - start,
Err(_) => match Probe::open(&audio_location)?.read() {
Err(_) => match Probe::open(audio_location)?.read() {
Ok(tagged_file) => tagged_file.properties().duration() - start,
Err(_) => Duration::from_secs(0),
@ -561,7 +557,7 @@ impl MusicLibrary {
let end = start + duration + postgap;
// Get the format as a string
let format: Option<FileFormat> = match FileFormat::from_file(&audio_location) {
let format: Option<FileFormat> = match FileFormat::from_file(audio_location) {
Ok(fmt) => Some(fmt),
Err(_) => None,
};
@ -637,7 +633,7 @@ impl MusicLibrary {
None => (),
}
match new_song.location {
URI::Local(_) if self.query_path(&new_song.location.path()).is_some() => {
URI::Local(_) if self.query_path(new_song.location.path()).is_some() => {
return Err(format!("Location exists for {:?}", new_song.location).into())
}
_ => (),
@ -664,7 +660,7 @@ impl MusicLibrary {
pub fn update_uri(&mut self, target_uri: &URI, new_tags: Vec<Tag>) -> Result<(), Box<dyn std::error::Error>> {
match self.query_uri(target_uri) {
Some(_) => (),
None => return Err(format!("URI not in database!").into()),
None => return Err("URI not in database!".to_string().into()),
}
for tag in new_tags {
@ -709,11 +705,11 @@ impl MusicLibrary {
self.library.par_iter().for_each(|track| {
for tag in target_tags {
let track_result = match tag {
Tag::Field(target) => match track.get_field(&target) {
Tag::Field(target) => match track.get_field(target) {
Some(value) => value,
None => continue,
},
_ => match track.get_tag(&tag) {
_ => match track.get_tag(tag) {
Some(value) => value.clone(),
None => continue,
},
@ -749,7 +745,7 @@ impl MusicLibrary {
Some(field_value) => field_value,
None => continue,
},
_ => match a.get_tag(&sort_option) {
_ => match a.get_tag(sort_option) {
Some(tag_value) => tag_value.to_owned(),
None => continue,
},
@ -760,7 +756,7 @@ impl MusicLibrary {
Some(field_value) => field_value,
None => continue,
},
_ => match b.get_tag(&sort_option) {
_ => match b.get_tag(sort_option) {
Some(tag_value) => tag_value.to_owned(),
None => continue,
},
@ -782,7 +778,7 @@ impl MusicLibrary {
path_a.file_name().cmp(&path_b.file_name())
});
if new_songs.len() > 0 {
if !new_songs.is_empty() {
Some(new_songs)
} else {
None

View file

@ -45,12 +45,12 @@ pub(super) fn write_library(
backup_name.set_extension("bkp");
// Create a new BufWriter on the file and make a snap frame encoer for it too
let writer = BufWriter::new(fs::File::create(writer_name.to_path_buf())?);
let writer = BufWriter::new(fs::File::create(&writer_name)?);
let mut e = snap::write::FrameEncoder::new(writer);
// Write out the data using bincode
bincode::serde::encode_into_std_write(
&library,
library,
&mut e,
bincode::config::standard()
.with_little_endian()
@ -83,7 +83,7 @@ pub fn find_images(song_path: &PathBuf) -> Result<Vec<AlbumArt>, Box<dyn Error>>
continue;
}
let format = FileFormat::from_file(&path)?.kind();
let format = FileFormat::from_file(path)?.kind();
if format != Kind::Image {
break
}

View file

@ -41,7 +41,7 @@ pub enum TrackerError {
impl TrackerError {
pub fn from_surf_error(error: surf::Error) -> TrackerError {
return match error.status() {
match error.status() {
StatusCode::Forbidden => TrackerError::InvalidAuth,
StatusCode::Unauthorized => TrackerError::InvalidAuth,
StatusCode::NetworkAuthenticationRequired => TrackerError::InvalidAuth,
@ -50,7 +50,7 @@ impl TrackerError {
StatusCode::ServiceUnavailable => TrackerError::ServiceUnavailable,
StatusCode::NotFound => TrackerError::ServiceUnavailable,
_ => TrackerError::Unknown,
};
}
}
}
@ -85,8 +85,8 @@ impl MusicTracker for LastFM {
};
params.insert("method", "track.scrobble");
params.insert("artist", &artist);
params.insert("track", &track);
params.insert("artist", artist);
params.insert("track", track);
params.insert("timestamp", &string_timestamp);
return match self.api_request(params).await {
@ -104,8 +104,8 @@ impl MusicTracker for LastFM {
};
params.insert("method", "track.updateNowPlaying");
params.insert("artist", &artist);
params.insert("track", &track);
params.insert("artist", artist);
params.insert("track", track);
return match self.api_request(params).await {
Ok(_) => Ok(()),
@ -160,7 +160,7 @@ impl LastFM {
auth_token.token
);
return Ok(auth_url);
Ok(auth_url)
}
/// Returns a LastFM session key
@ -186,15 +186,15 @@ impl LastFM {
// Sets session key from received response
let session_response: Session = serde_json::from_str(&response)?;
return Ok(session_response.session.key);
Ok(session_response.session.key)
}
/// Creates a new LastFM struct with a given config
pub fn new(config: &LastFMConfig) -> LastFM {
let last_fm = LastFM {
LastFM {
config: config.clone(),
};
return last_fm;
}
}
// Creates an api request with the given parameters
@ -221,9 +221,9 @@ impl LastFM {
let url = "http://ws.audioscrobbler.com/2.0/";
let response = surf::post(url).body_string(string_params).await;
return response;
surf::post(url).body_string(string_params).await
}
// Returns an api signature as defined in the last.fm api documentation
@ -242,7 +242,7 @@ impl LastFM {
let hash_result = md5_hasher.finalize();
let hashed_sig = format!("{:#02x}", hash_result);
return hashed_sig;
hashed_sig
}
// Removes last.fm account from dango-music-player
@ -265,11 +265,11 @@ pub struct DiscordRPC {
impl DiscordRPC {
pub fn new(config: &DiscordRPCConfig) -> Self {
let rpc = DiscordRPC {
DiscordRPC {
client: discord_presence::client::Client::new(config.dango_client_id),
config: config.clone(),
};
return rpc;
}
}
}
@ -307,8 +307,8 @@ impl MusicTracker for DiscordRPC {
// Sets discord account activity to current playing song
let send_activity = self.client.set_activity(|activity| {
activity
.state(format!("{}", album))
.details(format!("{}", song_name))
.state(album.to_string())
.details(song_name.to_string())
.assets(|assets| assets.large_image(&self.config.dango_icon))
.timestamps(|time| time.start(start_time))
});
@ -425,10 +425,10 @@ impl ListenBrainz {
request: &String,
endpoint: &String,
) -> Result<surf::Response, surf::Error> {
let reponse = surf::post(format!("{}{}", &self.config.api_url, endpoint))
surf::post(format!("{}{}", &self.config.api_url, endpoint))
.body_string(request.clone())
.header("Authorization", format!("Token {}", self.config.auth_token))
.await;
return reponse;
.await
}
}