about summary refs log tree commit diff stats
path: root/pkgs/by-name/mp/mpdpopm/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'pkgs/by-name/mp/mpdpopm/src/lib.rs')
-rw-r--r--pkgs/by-name/mp/mpdpopm/src/lib.rs116
1 files changed, 25 insertions, 91 deletions
diff --git a/pkgs/by-name/mp/mpdpopm/src/lib.rs b/pkgs/by-name/mp/mpdpopm/src/lib.rs
index e4579db2..4fe523ea 100644
--- a/pkgs/by-name/mp/mpdpopm/src/lib.rs
+++ b/pkgs/by-name/mp/mpdpopm/src/lib.rs
@@ -49,14 +49,15 @@ pub mod filters {
     include!(concat!(env!("OUT_DIR"), "/src/filters.rs"));
 }
 
-use clients::{Client, IdleClient, IdleSubSystem};
-use config::Config;
-use config::Connection;
-use filters_ast::FilterStickerNames;
-use messages::MessageProcessor;
-use playcounts::PlayState;
+use crate::{
+    clients::{Client, IdleClient, IdleSubSystem},
+    config::{Config, Connection},
+    filters_ast::FilterStickerNames,
+    messages::MessageProcessor,
+    playcounts::PlayState,
+};
 
-use backtrace::Backtrace;
+use anyhow::{Context, Error};
 use futures::{future::FutureExt, pin_mut, select};
 use tokio::{
     signal,
@@ -65,100 +66,39 @@ use tokio::{
 };
 use tracing::{debug, error, info};
 
-use std::path::PathBuf;
-
-////////////////////////////////////////////////////////////////////////////////////////////////////
-
-#[derive(Debug)]
-#[non_exhaustive]
-pub enum Error {
-    BadPath {
-        pth: PathBuf,
-    },
-    Client {
-        source: crate::clients::Error,
-        back: Backtrace,
-    },
-    Playcounts {
-        source: crate::playcounts::Error,
-        back: Backtrace,
-    },
-}
-
-impl std::fmt::Display for Error {
-    #[allow(unreachable_patterns)] // the _ arm is *currently* unreachable
-    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
-        match self {
-            Error::BadPath { pth } => write!(f, "Bad path: {:?}", pth),
-            Error::Client { source, back: _ } => write!(f, "Client error: {}", source),
-            Error::Playcounts { source, back: _ } => write!(f, "Playcount error: {}", source),
-        }
-    }
-}
-
-impl std::error::Error for Error {
-    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
-        match &self {
-            Error::Client { source, back: _ } => Some(source),
-            _ => None,
-        }
-    }
-}
-
-pub type Result<T> = std::result::Result<T, Error>;
-
-////////////////////////////////////////////////////////////////////////////////////////////////////
-
 /// Core `mppopmd' logic
 pub async fn mpdpopm(cfg: Config) -> std::result::Result<(), Error> {
     info!("mpdpopm {} beginning.", vars::VERSION);
 
     let filter_stickers = FilterStickerNames::new();
 
-    let mut client = match cfg.conn {
-        Connection::Local { ref path } => {
-            Client::open(path).await.map_err(|err| Error::Client {
-                source: err,
-                back: Backtrace::new(),
-            })?
-        }
-        Connection::TCP { ref host, port } => Client::connect(format!("{}:{}", host, port))
-            .await
-            .map_err(|err| Error::Client {
-            source: err,
-            back: Backtrace::new(),
-        })?,
-    };
+    let mut client =
+        match cfg.conn {
+            Connection::Local { ref path } => Client::open(path)
+                .await
+                .with_context(|| format!("Failed to open socket at `{}`", path.display()))?,
+            Connection::TCP { ref host, port } => Client::connect(format!("{}:{}", host, port))
+                .await
+                .with_context(|| format!("Failed to connect to client at `{}:{}`", host, port))?,
+        };
 
     let mut state = PlayState::new(&mut client, cfg.played_thresh)
         .await
-        .map_err(|err| Error::Client {
-            source: err,
-            back: Backtrace::new(),
-        })?;
+        .context("Failed to construct PlayState")?;
 
     let mut idle_client = match cfg.conn {
-        Connection::Local { ref path } => {
-            IdleClient::open(path).await.map_err(|err| Error::Client {
-                source: err,
-                back: Backtrace::new(),
-            })?
-        }
+        Connection::Local { ref path } => IdleClient::open(path)
+            .await
+            .context("Failed to open idle client")?,
         Connection::TCP { ref host, port } => IdleClient::connect(format!("{}:{}", host, port))
             .await
-            .map_err(|err| Error::Client {
-                source: err,
-                back: Backtrace::new(),
-            })?,
+            .context("Failed to connect to TCP idle client")?,
     };
 
     idle_client
         .subscribe(&cfg.commands_chan)
         .await
-        .map_err(|err| Error::Client {
-            source: err,
-            back: Backtrace::new(),
-        })?;
+        .context("Failed to subscribe to idle_client")?;
 
     let mut hup = signal(SignalKind::hangup()).unwrap();
     let mut kill = signal(SignalKind::terminate()).unwrap();
@@ -200,10 +140,7 @@ pub async fn mpdpopm(cfg: Config) -> std::result::Result<(), Error> {
                         tick.set(sleep(Duration::from_millis(cfg.poll_interval_ms)).fuse());
                         state.update(&mut client)
                                 .await
-                                .map_err(|err| Error::Playcounts {
-                                    source: err,
-                                    back: Backtrace::new()
-                                })?
+                                .context("PlayState update failed")?
                     },
                     // next = cmds.next() => match next {
                     //     Some(out) => {
@@ -229,10 +166,7 @@ pub async fn mpdpopm(cfg: Config) -> std::result::Result<(), Error> {
                             if subsys == IdleSubSystem::Player {
                                 state.update(&mut client)
                                     .await
-                                    .map_err(|err| Error::Playcounts {
-                                        source: err,
-                                        back: Backtrace::new()
-                                    })?
+                                    .context("PlayState update failed")?
                             } else if subsys == IdleSubSystem::Message {
                                 msg_check_needed = true;
                             }