about summary refs log tree commit diff stats
path: root/pkgs/by-name/mp/mpdpopm/src/bin/mpdpopmd.rs
diff options
context:
space:
mode:
Diffstat (limited to 'pkgs/by-name/mp/mpdpopm/src/bin/mpdpopmd.rs')
-rw-r--r--pkgs/by-name/mp/mpdpopm/src/bin/mpdpopmd.rs117
1 files changed, 17 insertions, 100 deletions
diff --git a/pkgs/by-name/mp/mpdpopm/src/bin/mpdpopmd.rs b/pkgs/by-name/mp/mpdpopm/src/bin/mpdpopmd.rs
index e903774c..643611d6 100644
--- a/pkgs/by-name/mp/mpdpopm/src/bin/mpdpopmd.rs
+++ b/pkgs/by-name/mp/mpdpopm/src/bin/mpdpopmd.rs
@@ -25,94 +25,17 @@
 //! the sticker database, by invoking external commands to keep your tags up-to-date (something
 //! along the lines of [mpdcron](https://alip.github.io/mpdcron)).
 
-use mpdpopm::config;
-use mpdpopm::config::Config;
-use mpdpopm::mpdpopm;
+use mpdpopm::{
+    config::{self, Config},
+    mpdpopm,
+};
 
-use backtrace::Backtrace;
+use anyhow::{Context, Result, bail};
 use clap::Parser;
 use tracing::{info, level_filters::LevelFilter};
 use tracing_subscriber::{EnvFilter, Layer, Registry, layer::SubscriberExt};
 
-use std::{fmt, io, path::PathBuf, sync::MutexGuard};
-
-////////////////////////////////////////////////////////////////////////////////////////////////////
-//                                 mppopmd application Error type                                 //
-////////////////////////////////////////////////////////////////////////////////////////////////////
-
-#[non_exhaustive]
-pub enum Error {
-    NoConfigArg,
-    NoConfig {
-        config: std::path::PathBuf,
-        cause: std::io::Error,
-    },
-    Filter {
-        source: tracing_subscriber::filter::FromEnvError,
-        back: Backtrace,
-    },
-    Fork {
-        errno: errno::Errno,
-        back: Backtrace,
-    },
-    PathContainsNull {
-        back: Backtrace,
-    },
-    OpenLockFile {
-        errno: errno::Errno,
-        back: Backtrace,
-    },
-    LockFile {
-        errno: errno::Errno,
-        back: Backtrace,
-    },
-    WritePid {
-        errno: errno::Errno,
-        back: Backtrace,
-    },
-    Config {
-        source: crate::config::Error,
-        back: Backtrace,
-    },
-    MpdPopm {
-        source: Box<mpdpopm::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::NoConfigArg => write!(f, "No configuration file given"),
-            Error::NoConfig { config, cause } => {
-                write!(f, "Configuration error ({:?}): {}", config, cause)
-            }
-            Error::Fork { errno, back: _ } => write!(f, "When forking, got errno {}", errno),
-            Error::PathContainsNull { back: _ } => write!(f, "Path contains a null character"),
-            Error::OpenLockFile { errno, back: _ } => {
-                write!(f, "While opening lock file, got errno {}", errno)
-            }
-            Error::LockFile { errno, back: _ } => {
-                write!(f, "While locking the lock file, got errno {}", errno)
-            }
-            Error::WritePid { errno, back: _ } => {
-                write!(f, "While writing pid file, got errno {}", errno)
-            }
-            Error::Config { source, back: _ } => write!(f, "Configuration error: {}", source),
-            Error::MpdPopm { source, back: _ } => write!(f, "mpdpopm error: {}", source),
-            _ => write!(f, "Unknown mppopmd error"),
-        }
-    }
-}
-
-impl fmt::Debug for Error {
-    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-        write!(f, "{}", self)
-    }
-}
-
-type Result = std::result::Result<(), Error>;
+use std::{io, path::PathBuf, sync::MutexGuard};
 
 pub struct MyMutexGuardWriter<'a>(MutexGuard<'a, std::fs::File>);
 
@@ -164,22 +87,21 @@ struct Args {
     debug: bool,
 }
 
-/// Entry point for `mppopmd'.
+/// Entry point for `mpdopmd'.
 ///
 /// Do *not* use the #[tokio::main] attribute here! If this program is asked to daemonize (the usual
 /// case), we will fork after tokio has started its thread pool, with disastrous consequences.
 /// Instead, stay synchronous until we've daemonized (or figured out that we don't need to), and
 /// only then fire-up the tokio runtime.
-fn main() -> Result {
+fn main() -> Result<()> {
     use mpdpopm::vars::VERSION;
 
     let args = Args::parse();
 
     let config = if let Some(cfgpath) = &args.config {
         match std::fs::read_to_string(cfgpath) {
-            Ok(text) => config::from_str(&text).map_err(|err| Error::Config {
-                source: err,
-                back: Backtrace::new(),
+            Ok(text) => config::from_str(&text).with_context(|| {
+                format!("Failed to parse config file at: `{}`", cfgpath.display())
             })?,
             // The config file (defaulted or not) either didn't exist, or we were unable to read its
             // contents...
@@ -187,10 +109,10 @@ fn main() -> Result {
                 // Either they did _not_, in which case they probably want to know that the config
                 // file they explicitly asked for does not exist, or there was some other problem,
                 // in which case we're out of options, anyway. Either way:
-                return Err(Error::NoConfig {
-                    config: PathBuf::from(cfgpath),
-                    cause: err,
-                });
+                bail!(
+                    "No config file could be read at: `{}`, because: {err}",
+                    cfgpath.display()
+                )
             }
         }
     } else {
@@ -208,10 +130,7 @@ fn main() -> Result {
     let filter = EnvFilter::builder()
         .with_default_directive(lf.into())
         .from_env()
-        .map_err(|err| Error::Filter {
-            source: err,
-            back: Backtrace::new(),
-        })?;
+        .context("Failed to construct env filter")?;
 
     let formatter: Box<dyn Layer<Registry> + Send + Sync> = {
         Box::new(
@@ -226,8 +145,6 @@ fn main() -> Result {
 
     info!("mppopmd {VERSION} logging at level {lf:#?}.");
     let rt = tokio::runtime::Runtime::new().unwrap();
-    rt.block_on(mpdpopm(config)).map_err(|err| Error::MpdPopm {
-        source: Box::new(err),
-        back: Backtrace::new(),
-    })
+
+    rt.block_on(mpdpopm(config)).context("Main mpdpopm failed")
 }