diff options
| author | Benedikt Peetz <benedikt.peetz@b-peetz.de> | 2026-01-25 20:44:40 +0100 |
|---|---|---|
| committer | Benedikt Peetz <benedikt.peetz@b-peetz.de> | 2026-01-25 20:44:40 +0100 |
| commit | 42cb2a63a8143c3557b9304c3eed6973acbc7405 (patch) | |
| tree | af43e088da415bc7768898dc57f7972389ec04cd /pkgs/by-name/mp/mpdpopm/src/lib.rs | |
| parent | modules/river/keymap: Make media good/bad ratings once mappings (diff) | |
| download | nixos-config-42cb2a63a8143c3557b9304c3eed6973acbc7405.zip | |
pkgs/mpdpopm: Switch error handling from snafu to anyhow
This is not a library, as such we can just use anyhow and provide better and more concise errors to the user.
Diffstat (limited to '')
| -rw-r--r-- | pkgs/by-name/mp/mpdpopm/src/lib.rs | 116 |
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; } |
