From 04c7ddb87698052fce8b959a654f48a6613a015a Mon Sep 17 00:00:00 2001
From: G2-Games <ke0bhogsg@gmail.com>
Date: Wed, 24 Jan 2024 02:47:57 -0600
Subject: [PATCH] Cleaned up code, updated crates

---
 Cargo.toml                 |  6 +++---
 src/music_player.rs        | 24 ++++++++++++++++--------
 src/music_storage/utils.rs |  4 ++--
 3 files changed, 21 insertions(+), 13 deletions(-)

diff --git a/Cargo.toml b/Cargo.toml
index 9c8d51a..0267611 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -13,12 +13,11 @@ categories = []
 
 [dependencies]
 file-format = { version = "0.23.0", features = ["reader-asf", "reader-ebml", "reader-mp4", "reader-rm", "reader-txt", "reader-xml", "serde"] }
-lofty = "0.18.0"
+lofty = "0.18.2"
 serde = { version = "1.0.195", features = ["derive"] }
 walkdir = "2.4.0"
 chrono = { version = "0.4.31", features = ["serde"] }
 bincode = { version = "2.0.0-rc.3", features = ["serde"] }
-unidecode = "0.3.0"
 rayon = "1.8.0"
 log = "0.4"
 base64 = "0.21.5"
@@ -35,4 +34,5 @@ m3u8-rs = "5.0.5"
 thiserror = "1.0.56"
 font = "0.27.0"
 uuid = { version = "1.6.1", features = ["v4", "serde"]}
-serde_json = "1.0.111"
\ No newline at end of file
+serde_json = "1.0.111"
+deunicode = "1.4.2"
diff --git a/src/music_player.rs b/src/music_player.rs
index dff9748..4e7de23 100644
--- a/src/music_player.rs
+++ b/src/music_player.rs
@@ -83,7 +83,8 @@ enum PlaybackStats {
     Playing{
         start: Duration,
         end:   Duration,
-    }
+    },
+    Finished // When this is sent, the thread will die!
 }
 
 /// An instance of a music player with a GStreamer backend
@@ -142,10 +143,10 @@ impl Player {
             let mut stats = PlaybackStats::Idle;
             let mut pos_temp;
             loop {
-                match stat_rx.recv_timeout(std::time::Duration::from_millis(100)) {
-                    Ok(res) => stats = res,
-                    Err(_) => {}
-                };
+                // Check for new messages or updates about how to proceed
+                if let Ok(res) = stat_rx.recv_timeout(std::time::Duration::from_millis(100)) {
+                    stats = res
+                }
 
                 pos_temp = playbin_arc
                     .read()
@@ -171,8 +172,13 @@ impl Player {
                         // This has to be done AFTER the current time in the file
                         // is calculated, or everything else is wrong
                         pos_temp = Some(pos_temp.unwrap() - start)
-                    }
-                    _ => println!("waiting!")
+                    },
+                    PlaybackStats::Finished => {
+                        *position_update.write().unwrap() = None;
+                        break
+                    },
+                    PlaybackStats::Idle | PlaybackStats::Switching => println!("waiting!"),
+                    _ => ()
                 }
 
                 *position_update.write().unwrap() = pos_temp;
@@ -483,11 +489,13 @@ impl Player {
 }
 
 impl Drop for Player {
-    /// Cleans up `GStreamer` pipeline when `Backend` is dropped.
+    /// Cleans up the `GStreamer` pipeline and the monitoring
+    /// thread when [Player] is dropped.
     fn drop(&mut self) {
         self.playbin_mut()
             .unwrap()
             .set_state(gst::State::Null)
             .expect("Unable to set the pipeline to the `Null` state");
+        let _ = self.playback_tx.send(PlaybackStats::Finished);
     }
 }
diff --git a/src/music_storage/utils.rs b/src/music_storage/utils.rs
index a14dbe1..0d6fb47 100644
--- a/src/music_storage/utils.rs
+++ b/src/music_storage/utils.rs
@@ -6,13 +6,13 @@ use std::error::Error;
 use walkdir::WalkDir;
 use file_format::{FileFormat, Kind};
 use snap;
-use unidecode::unidecode;
+use deunicode::deunicode_with_tofu;
 
 use super::library::{AlbumArt, URI};
 
 pub(super) fn normalize(input_string: &str) -> String {
     // Normalize the string to latin characters... this needs a lot of work
-    let mut normalized = unidecode(input_string);
+    let mut normalized = deunicode_with_tofu(input_string, " ");
 
     // Remove non alphanumeric characters
     normalized.retain(|c| c.is_alphanumeric());