cargo fmt

This commit is contained in:
G2-Games 2023-11-04 18:42:21 -05:00
parent 37b393246f
commit 7ddc829dac
2 changed files with 62 additions and 55 deletions

View file

@ -1,17 +1,17 @@
// Crate things // Crate things
use crate::music_controller::config::Config;
use super::utils::{normalize, read_library, write_library}; use super::utils::{normalize, read_library, write_library};
use crate::music_controller::config::Config;
// Various std things // Various std things
use std::collections::BTreeMap; use std::collections::BTreeMap;
use std::error::Error; use std::error::Error;
// Files // Files
use cue::cd::CD;
use file_format::{FileFormat, Kind}; use file_format::{FileFormat, Kind};
use jwalk::WalkDir; use jwalk::WalkDir;
use lofty::{AudioFile, ItemKey, ItemValue, Probe, TagType, TaggedFileExt}; use lofty::{AudioFile, ItemKey, ItemValue, Probe, TagType, TaggedFileExt};
use std::ffi::OsStr; use std::ffi::OsStr;
use cue::cd::CD;
use std::fs; use std::fs;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
@ -105,14 +105,12 @@ impl Song {
match target_field { match target_field {
"location" => Some(self.location.clone().path_string()), "location" => Some(self.location.clone().path_string()),
"plays" => Some(self.plays.clone().to_string()), "plays" => Some(self.plays.clone().to_string()),
"format" => { "format" => match self.format {
match self.format { Some(format) => match format.short_name() {
Some(format) => match format.short_name() { Some(short) => Some(short.to_string()),
Some(short) => Some(short.to_string()), None => None,
None => None },
}, None => None,
None => None
}
}, },
_ => None, // Other field types are not yet supported _ => None, // Other field types are not yet supported
} }
@ -136,10 +134,7 @@ impl URI {
match self { match self {
URI::Local(_) => Err("\"Local\" has no stored index".into()), URI::Local(_) => Err("\"Local\" has no stored index".into()),
URI::Remote(_, _) => Err("\"Remote\" has no stored index".into()), URI::Remote(_, _) => Err("\"Remote\" has no stored index".into()),
URI::Cue { URI::Cue { index, .. } => Ok(index),
index,
..
} => Ok(index),
} }
} }
@ -149,10 +144,7 @@ impl URI {
match self { match self {
URI::Local(_) => Err("\"Local\" has no starting time".into()), URI::Local(_) => Err("\"Local\" has no starting time".into()),
URI::Remote(_, _) => Err("\"Remote\" has no starting time".into()), URI::Remote(_, _) => Err("\"Remote\" has no starting time".into()),
URI::Cue { URI::Cue { start, .. } => Ok(start),
start,
..
} => Ok(start),
} }
} }
@ -162,10 +154,7 @@ impl URI {
match self { match self {
URI::Local(_) => Err("\"Local\" has no starting time".into()), URI::Local(_) => Err("\"Local\" has no starting time".into()),
URI::Remote(_, _) => Err("\"Remote\" has no starting time".into()), URI::Remote(_, _) => Err("\"Remote\" has no starting time".into()),
URI::Cue { URI::Cue { end, .. } => Ok(end),
end,
..
} => Ok(end),
} }
} }
@ -173,10 +162,7 @@ impl URI {
pub fn path(&self) -> &PathBuf { pub fn path(&self) -> &PathBuf {
match self { match self {
URI::Local(location) => location, URI::Local(location) => location,
URI::Cue { URI::Cue { location, .. } => location,
location,
..
} => location,
URI::Remote(_, location) => location, URI::Remote(_, location) => location,
} }
} }
@ -184,10 +170,7 @@ impl URI {
pub fn path_string(&self) -> String { pub fn path_string(&self) -> String {
let path_str = match self { let path_str = match self {
URI::Local(location) => location.as_path().to_string_lossy(), URI::Local(location) => location.as_path().to_string_lossy(),
URI::Cue { URI::Cue { location, .. } => location.as_path().to_string_lossy(),
location,
..
} => location.as_path().to_string_lossy(),
URI::Remote(_, location) => location.as_path().to_string_lossy(), URI::Remote(_, location) => location.as_path().to_string_lossy(),
}; };
path_str.to_string() path_str.to_string()
@ -273,7 +256,7 @@ impl MusicLibrary {
match global_config.db_path.exists() { match global_config.db_path.exists() {
true => { true => {
library = read_library(*global_config.db_path.clone())?; library = read_library(*global_config.db_path.clone())?;
}, }
false => { false => {
// Create the database if it does not exist // Create the database if it does not exist
// possibly from the backup file // possibly from the backup file
@ -718,7 +701,9 @@ impl MusicLibrary {
}, },
}; };
if normalize(&track_result.to_string()).contains(&normalize(&query_string.to_owned())) { if normalize(&track_result.to_string())
.contains(&normalize(&query_string.to_owned()))
{
songs.lock().unwrap().push(track); songs.lock().unwrap().push(track);
return; return;
} }
@ -782,17 +767,21 @@ impl MusicLibrary {
for result in &self.library { for result in &self.library {
let title = match result.get_tag(&Tag::Album) { let title = match result.get_tag(&Tag::Album) {
Some(title) => title, Some(title) => title,
None => continue None => continue,
}; };
let norm_title = normalize(title); let norm_title = normalize(title);
let disc_num = result.get_tag(&Tag::Disk).unwrap_or(&"".to_string()).parse::<usize>().unwrap_or(1); let disc_num = result
.get_tag(&Tag::Disk)
.unwrap_or(&"".to_string())
.parse::<usize>()
.unwrap_or(1);
match albums.get_mut(&norm_title) { match albums.get_mut(&norm_title) {
// If the album is in the list, add the track to the appropriate disc in it // If the album is in the list, add the track to the appropriate disc in it
Some(album) => { Some(album) => match album.discs.get_mut(&disc_num) {
match album.discs.get_mut(&disc_num) { Some(disc) => disc.push(result),
Some(disc) => disc.push(result), None => {
None => {album.discs.insert(disc_num, vec![result]);} album.discs.insert(disc_num, vec![result]);
} }
}, },
// If the album is not in the list, make a new one and add it // If the album is not in the list, make a new one and add it
@ -816,7 +805,8 @@ impl MusicLibrary {
let a_track = a.get_tag(&Tag::Track).unwrap_or(&blank); let a_track = a.get_tag(&Tag::Track).unwrap_or(&blank);
let b_track = b.get_tag(&Tag::Track).unwrap_or(&blank); let b_track = b.get_tag(&Tag::Track).unwrap_or(&blank);
if let (Ok(num_a), Ok(num_b)) = (a_track.parse::<i32>(), b_track.parse::<i32>()) { if let (Ok(num_a), Ok(num_b)) = (a_track.parse::<i32>(), b_track.parse::<i32>())
{
// If parsing the track numbers succeeds, compare as numbers // If parsing the track numbers succeeds, compare as numbers
num_a.cmp(&num_b) num_a.cmp(&num_b)
} else { } else {
@ -834,19 +824,23 @@ impl MusicLibrary {
albums albums
} }
pub fn query_albums(&self, pub fn query_albums(
query_string: &String, // The query itself &self,
query_string: &String, // The query itself
) -> Result<Vec<Album>, Box<dyn Error>> { ) -> Result<Vec<Album>, Box<dyn Error>> {
let all_albums = self.albums(); let all_albums = self.albums();
let normalized_query = normalize(query_string); let normalized_query = normalize(query_string);
let albums: Vec<Album> = all_albums.par_iter().filter_map(|album| let albums: Vec<Album> = all_albums
if normalize(album.0).contains(&normalized_query) { .par_iter()
Some(album.1.clone()) .filter_map(|album| {
} else { if normalize(album.0).contains(&normalized_query) {
None Some(album.1.clone())
} } else {
).collect(); None
}
})
.collect();
Ok(albums) Ok(albums)
} }

View file

@ -1,10 +1,10 @@
use std::io::{BufReader, BufWriter}; use std::io::{BufReader, BufWriter};
use std::{path::PathBuf, error::Error, fs}; use std::{error::Error, fs, path::PathBuf};
use snap; use snap;
use unidecode::unidecode;
use crate::music_storage::music_db::Song; use crate::music_storage::music_db::Song;
use unidecode::unidecode;
pub fn normalize(input_string: &String) -> String { pub fn normalize(input_string: &String) -> String {
// Normalize the unicode and convert everything to lowercase // Normalize the unicode and convert everything to lowercase
@ -17,19 +17,26 @@ pub fn normalize(input_string: &String) -> String {
} }
pub fn read_library(path: PathBuf) -> Result<Vec<Song>, Box<dyn Error>> { pub fn read_library(path: PathBuf) -> Result<Vec<Song>, Box<dyn Error>> {
// Create a new snap reader over the database file // Create a new snap reader over the database file
let database = fs::File::open(path)?; let database = fs::File::open(path)?;
let reader = BufReader::new(database); let reader = BufReader::new(database);
let mut d = snap::read::FrameDecoder::new(reader); let mut d = snap::read::FrameDecoder::new(reader);
// Decode the library from the serialized data into the vec // Decode the library from the serialized data into the vec
let library: Vec<Song> = bincode::serde::decode_from_std_read(&mut d, bincode::config::standard().with_little_endian().with_variable_int_encoding())?; let library: Vec<Song> = bincode::serde::decode_from_std_read(
&mut d,
bincode::config::standard()
.with_little_endian()
.with_variable_int_encoding(),
)?;
Ok(library) Ok(library)
} }
pub fn write_library(library: &Vec<Song>, path: PathBuf, take_backup: bool) -> Result<(), Box<dyn Error>> { pub fn write_library(
library: &Vec<Song>,
path: PathBuf,
take_backup: bool,
) -> Result<(), Box<dyn Error>> {
// Create 2 new names for the file, a temporary one for writing out, and a backup // Create 2 new names for the file, a temporary one for writing out, and a backup
let mut writer_name = path.clone(); let mut writer_name = path.clone();
writer_name.set_extension("tmp"); writer_name.set_extension("tmp");
@ -41,7 +48,13 @@ pub fn write_library(library: &Vec<Song>, path: PathBuf, take_backup: bool) -> R
let mut e = snap::write::FrameEncoder::new(writer); let mut e = snap::write::FrameEncoder::new(writer);
// Write out the data using bincode // Write out the data using bincode
bincode::serde::encode_into_std_write(&library, &mut e, bincode::config::standard().with_little_endian().with_variable_int_encoding())?; bincode::serde::encode_into_std_write(
&library,
&mut e,
bincode::config::standard()
.with_little_endian()
.with_variable_int_encoding(),
)?;
if path.exists() && take_backup { if path.exists() && take_backup {
fs::rename(&path, backup_name)?; fs::rename(&path, backup_name)?;