about summary refs log tree commit diff stats
path: root/pkgs/by-name/mp/mpdpopm/src/playcounts.rs
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--pkgs/by-name/mp/mpdpopm/src/playcounts.rs86
1 files changed, 16 insertions, 70 deletions
diff --git a/pkgs/by-name/mp/mpdpopm/src/playcounts.rs b/pkgs/by-name/mp/mpdpopm/src/playcounts.rs
index 6ae8f903..7d646b4c 100644
--- a/pkgs/by-name/mp/mpdpopm/src/playcounts.rs
+++ b/pkgs/by-name/mp/mpdpopm/src/playcounts.rs
@@ -26,67 +26,13 @@
 //!
 
 use crate::clients::{Client, PlayerStatus};
-use crate::storage::{self, last_played, play_count, skipped};
+use crate::storage::{last_played, play_count, skipped};
 
-use backtrace::Backtrace;
+use anyhow::{Context, Error, Result, anyhow};
 use tracing::{debug, info};
 
-use std::path::PathBuf;
 use std::time::SystemTime;
 
-#[derive(Debug)]
-pub enum Error {
-    PlayerStopped,
-    BadPath {
-        pth: PathBuf,
-    },
-    SystemTime {
-        source: std::time::SystemTimeError,
-        back: Backtrace,
-    },
-    Client {
-        source: crate::clients::Error,
-        back: Backtrace,
-    },
-}
-
-impl std::fmt::Display for Error {
-    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
-        match self {
-            Error::PlayerStopped => write!(f, "The MPD player is stopped"),
-            Error::BadPath { pth } => write!(f, "Bad path: {:?}", pth),
-            Error::SystemTime { source, back: _ } => {
-                write!(f, "Couldn't get system time: {}", source)
-            }
-            Error::Client { source, back: _ } => write!(f, "Client error: {}", source),
-        }
-    }
-}
-
-impl std::error::Error for Error {
-    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
-        match &self {
-            Error::SystemTime { source, back: _ } => Some(source),
-            Error::Client { source, back: _ } => Some(source),
-            _ => None,
-        }
-    }
-}
-
-impl From<storage::Error> for Error {
-    fn from(value: storage::Error) -> Self {
-        match value {
-            storage::Error::PlayerStopped => Self::PlayerStopped,
-            storage::Error::BadPath { pth } => Self::BadPath { pth },
-            storage::Error::SystemTime { source, back } => Self::SystemTime { source, back },
-            storage::Error::Client { source, back } => Self::Client { source, back },
-            _ => unreachable!(),
-        }
-    }
-}
-
-type Result<T> = std::result::Result<T, Error>;
-
 /// Current server state in terms of the play status (stopped/paused/playing, current track, elapsed
 /// time in current track, &c)
 #[derive(Debug)]
@@ -109,7 +55,7 @@ impl PlayState {
     pub async fn new(
         client: &mut Client,
         played_thresh: f64,
-    ) -> std::result::Result<PlayState, crate::clients::Error> {
+    ) -> std::result::Result<PlayState, Error> {
         Ok(PlayState {
             last_server_stat: client.status().await?,
             have_incr_play_count: false,
@@ -126,10 +72,10 @@ impl PlayState {
     /// Poll the server-- update our status; maybe increment the current track's play count; the
     /// caller must arrange to have this method invoked periodically to keep our state fresh
     pub async fn update(&mut self, client: &mut Client) -> Result<()> {
-        let new_stat = client.status().await.map_err(|err| Error::Client {
-            source: err,
-            back: Backtrace::new(),
-        })?;
+        let new_stat = client
+            .status()
+            .await
+            .context("Failed to get client status")?;
 
         match (&self.last_server_stat, &new_stat) {
             (PlayerStatus::Play(last), PlayerStatus::Play(curr))
@@ -176,8 +122,8 @@ impl PlayState {
                         curr.elapsed / curr.duration
                     );
 
-                    let file = curr.file.to_str().ok_or_else(|| Error::BadPath {
-                        pth: curr.file.clone(),
+                    let file = curr.file.to_str().ok_or_else(|| {
+                        anyhow!("Failed to parse path as utf8: `{}`", curr.file.display())
                     })?;
 
                     let curr_pc = play_count::get(client, file).await?.unwrap_or_default();
@@ -189,10 +135,7 @@ impl PlayState {
                         file,
                         SystemTime::now()
                             .duration_since(SystemTime::UNIX_EPOCH)
-                            .map_err(|err| Error::SystemTime {
-                                source: err,
-                                back: Backtrace::new(),
-                            })?
+                            .context("Failed to get system time")?
                             .as_secs(),
                     )
                     .await?;
@@ -213,8 +156,8 @@ impl PlayState {
                         last.elapsed / last.duration
                     );
 
-                    let file = last.file.to_str().ok_or_else(|| Error::BadPath {
-                        pth: last.file.clone(),
+                    let file = last.file.to_str().ok_or_else(|| {
+                        anyhow!("Failed to parse path as utf8: `{}`", last.file.display())
                     })?;
 
                     let skip_count = skipped::get(client, file).await?.unwrap_or_default();
@@ -350,7 +293,10 @@ OK
                 ),
                 "OK\n",
             ),
-            ("sticker set song \"E/Enya - Wild Child.mp3\" unwoundstack.com:playcount 12", "OK\n"),
+            (
+                "sticker set song \"E/Enya - Wild Child.mp3\" unwoundstack.com:playcount 12",
+                "OK\n",
+            ),
         ]));
 
         let mut cli = Client::new(mock).unwrap();