# HG changeset patch # User Floris Bruynooghe # Date 1604259007 -3600 # Sun Nov 01 20:30:07 2020 +0100 # Node ID a769d5d05f29e1288d63a4ac3a1240520d5b047f # Parent b45e9aabbbb3a5e5669113d8dee4b96d5149ca96 Remove failure entirely diff --git a/Cargo.lock b/Cargo.lock --- a/Cargo.lock +++ b/Cargo.lock @@ -111,8 +111,6 @@ "clap", "ctrlc", "dbus", - "failure", - "failure_derive", "log", "simplelog", "systemd", diff --git a/Cargo.toml b/Cargo.toml --- a/Cargo.toml +++ b/Cargo.toml @@ -14,8 +14,6 @@ clap = "2.31" ctrlc = {version = "3.1.2", features = ["termination"]} dbus = "0.6.4" -failure = "0.1.5" -failure_derive = "0.1.5" log = "0.4.1" simplelog = "0.5.2" systemd = "0.4.0" diff --git a/src/bin/brightd.rs b/src/bin/brightd.rs --- a/src/bin/brightd.rs +++ b/src/bin/brightd.rs @@ -3,25 +3,11 @@ use std::rc::Rc; use std::sync::mpsc; -use failure::ResultExt; +use anyhow::{Context, Result}; use log::{debug, error, info}; -fn main() { +fn main() -> Result<()> { let argv: Vec = std::env::args().collect(); - run(argv).unwrap_or_else(|err| { - log_error(err); - std::process::exit(1); - }); -} - -fn log_error(err: failure::Error) { - error!("{}", err); - for cause in err.iter_causes() { - error!("{}", cause) - } -} - -fn run(argv: Vec) -> Result<(), failure::Error> { let cfg = AppConfig::new(&argv)?; let cfg2 = cfg.clone(); let (busname_tx, busname_rx) = mpsc::channel::(); @@ -36,15 +22,16 @@ } let exited = pond.wait(); if exited.result.is_err() { - return Err(failure::err_msg(format!( + return Err(anyhow::anyhow!( "Thread \"{}\" failed: {:?}", - exited.name, exited.result - ))); + exited.name, + exited.result + )); } Ok(()) } -fn run_acpi_listener(cfg: AppConfig, busname_rx: mpsc::Receiver) -> failure::Fallible<()> { +fn run_acpi_listener(cfg: AppConfig, busname_rx: mpsc::Receiver) -> Result<()> { let peer = busname_rx .recv() .context("Peer D-Bus busname receiver died")?; @@ -69,13 +56,13 @@ Ok(()) } -fn run_dbus_svc(cfg: AppConfig, busname_tx: mpsc::Sender) -> Result<(), failure::Error> { +fn run_dbus_svc(cfg: AppConfig, busname_tx: mpsc::Sender) -> Result<()> { let devices = bright::create_devices(Vec::new())?; let mut dbus_svc = match bright::dbus::DBusService::new(cfg.bustype, Rc::new(devices)) { Ok(svc) => svc, Err(err) => { error!("{}", err); - return Err(failure::err_msg("Failed to create dbus service")); + return Err(anyhow::anyhow!("Failed to create dbus service")); } }; busname_tx.send(dbus_svc.unique_name()).unwrap(); @@ -132,7 +119,7 @@ // } // } -// Application configuration +/// Application configuration #[derive(Debug, Clone)] struct AppConfig { bustype: dbus::BusType, @@ -143,7 +130,7 @@ } impl AppConfig { - fn new(argv: &[String]) -> Result { + fn new(argv: &[String]) -> Result { let matches = bright::cli::brightd_app().get_matches_from(argv); let level = match matches.value_of("loglevel") { Some("debug") => log::LevelFilter::Debug, @@ -154,8 +141,7 @@ None => panic!("No loglevel"), }; if matches.is_present("systemd") { - log::set_logger(&LOGGER) - .map_err(|e| failure::err_msg(format!("Failed to intialise logging: {:?}", e)))?; + log::set_logger(&LOGGER).context("Failed to intialise logging")?; log::set_max_level(level); } else { simplelog::TermLogger::init(level, simplelog::Config::default())?; diff --git a/src/dbus.rs b/src/dbus.rs --- a/src/dbus.rs +++ b/src/dbus.rs @@ -34,6 +34,7 @@ use std::rc::Rc; use std::sync::Arc; +use anyhow::Result; use dbus::{self, stdintf::org_freedesktop_dbus::Properties}; use log::{debug, error, info}; @@ -100,10 +101,7 @@ Ok(svc) } - pub fn incoming( - &mut self, - timeout_ms: u32, - ) -> Result, failure::Error> { + pub fn incoming(&mut self, timeout_ms: u32) -> Result> { for (name, val) in self.state_rx.try_iter() { self.states.insert(name, val); } @@ -126,7 +124,7 @@ device.get()?, curr_state, ) - .map_err(|_err| failure::err_msg("WTF Rust error handling"))?; + .map_err(|_err| anyhow::anyhow!("WTF Rust error handling"))?; } } Ok(self.connection.incoming(timeout_ms)) @@ -141,10 +139,7 @@ /// should be called, in which case the timeout will be ignored and /// the function will return early. Otherwise /// Ok(SvcStatus::Running) is returned. - pub fn handle_msgs( - &mut self, - timeout: std::time::Duration, - ) -> Result { + pub fn handle_msgs(&mut self, timeout: std::time::Duration) -> Result { let start = std::time::Instant::now(); while start.elapsed() < timeout { let remaining = timeout @@ -170,7 +165,7 @@ /// /// We don't really shut down until the connection object is destroyed which happens on /// [Drop]. This only unregisters the name and handles all outstanding messages. - pub fn shutdown(&self) -> Result<(), failure::Error> { + pub fn shutdown(&self) -> Result<()> { match self.connection.release_name(BUSNAME) { Ok(action) => info!("Releasing busname {}: {:?}", BUSNAME, action), Err(err) => error!("Failed releasing busname {}: {}", BUSNAME, err), @@ -517,23 +512,22 @@ /// Terminates the daemon. /// /// This call is non-blocking. - pub fn term(&self) -> Result<(), failure::Error> { + pub fn term(&self) -> Result<()> { let msg = dbus::Message::new_method_call(self.peer.clone(), MANAGER_PATH, MANAGER_IFACE, "Term") .unwrap(); self.conn .send(msg) .and(Ok(())) - .map_err(|_| failure::err_msg("Failed sending Term() message")) + .map_err(|_| anyhow::anyhow!("Failed sending Term() message")) } /// Pings the peer. /// /// # Errors /// - /// Returns a `ClientError` with a cause which is a - /// `failure::Context` which has a `ClientErrorKind::CallFailed` - /// context if the peer did not respond. + /// Returns a `ClientError` with a cause which is a which has a + /// `ClientErrorKind::CallFailed` context if the peer did not respond. pub fn ping(&self) -> Result<(), ClientError> { let meth = dbus::Message::new_method_call( self.peer.clone(),