From 9bc416e8027ea212d9f1c50ae53b48f099aa4d42 Mon Sep 17 00:00:00 2001 From: Luflosi Date: Fri, 31 May 2024 15:38:24 +0200 Subject: [PATCH] Use color-eyre instead of error-chain for errors I prefer this crate. --- Cargo.lock | 104 +++++++++++++++++++++++++++++++++++++++++++++++--- Cargo.toml | 2 +- src/config.rs | 23 +++++------ src/main.rs | 26 ++++--------- 4 files changed, 116 insertions(+), 39 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f0bf9e5..57cc7dc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -208,6 +208,33 @@ version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "98cc8fbded0c607b7ba9dd60cd98df59af97e84d24e49c8557331cfc26d301ce" +[[package]] +name = "color-eyre" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "55146f5e46f237f7423d74111267d4597b59b0dad0ffaf7303bce9945d843ad5" +dependencies = [ + "backtrace", + "color-spantrace", + "eyre", + "indenter", + "once_cell", + "owo-colors", + "tracing-error", +] + +[[package]] +name = "color-spantrace" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd6be1b2a7e382e2b98b43b2adcca6bb0e465af0bdd38123873ae61eb17a72c2" +dependencies = [ + "once_cell", + "owo-colors", + "tracing-core", + "tracing-error", +] + [[package]] name = "colorchoice" version = "1.0.1" @@ -256,8 +283,8 @@ version = "0.3.0" dependencies = [ "argon2", "clap", + "color-eyre", "env_logger", - "error-chain", "log", "serde", "serde_derive", @@ -305,13 +332,13 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" [[package]] -name = "error-chain" -version = "0.12.4" +name = "eyre" +version = "0.6.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2d2f06b9cac1506ece98fe3231e3cc9c4410ec3d5b1f24ae1c8946f0742cdefc" +checksum = "7cd915d99f24784cdc19fd37ef22b97e3ff0ae756c7e492e9fbfe897d61e2aec" dependencies = [ - "backtrace", - "version_check", + "indenter", + "once_cell", ] [[package]] @@ -544,6 +571,12 @@ dependencies = [ "unicode-normalization", ] +[[package]] +name = "indenter" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce23b50ad8242c51a442f3ff322d56b02f08852c77e4c0b4d3fd684abc89c683" + [[package]] name = "indexmap" version = "2.2.6" @@ -566,6 +599,12 @@ version = "1.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + [[package]] name = "libc" version = "0.2.155" @@ -673,6 +712,12 @@ version = "1.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" +[[package]] +name = "owo-colors" +version = "3.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c1b04fb49957986fdce4d6ee7a65027d55d4b6d2265e5848bbb507b58ccfdb6f" + [[package]] name = "parking_lot" version = "0.12.3" @@ -924,6 +969,15 @@ dependencies = [ "digest", ] +[[package]] +name = "sharded-slab" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f40ca3c46823713e0d4209592e8d6e826aa57e928f09752619fc696c499637f6" +dependencies = [ + "lazy_static", +] + [[package]] name = "signal-hook-registry" version = "1.4.2" @@ -1007,6 +1061,16 @@ dependencies = [ "syn", ] +[[package]] +name = "thread_local" +version = "1.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b9ef9bad013ada3808854ceac7b46812a6465ba368859a37e2100283d2d719c" +dependencies = [ + "cfg-if", + "once_cell", +] + [[package]] name = "tinyvec" version = "1.6.0" @@ -1135,6 +1199,28 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c06d3da6113f116aaee68e4d601191614c9053067f9ab7f6edbcb161237daa54" dependencies = [ "once_cell", + "valuable", +] + +[[package]] +name = "tracing-error" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d686ec1c0f384b1277f097b2f279a2ecc11afe8c133c1aabf036a27cb4cd206e" +dependencies = [ + "tracing", + "tracing-subscriber", +] + +[[package]] +name = "tracing-subscriber" +version = "0.3.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ad0f048c97dbd9faa9b7df56362b8ebcaa52adb06b498c050d2f4e32f90a7a8b" +dependencies = [ + "sharded-slab", + "thread_local", + "tracing-core", ] [[package]] @@ -1221,6 +1307,12 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" +[[package]] +name = "valuable" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" + [[package]] name = "version_check" version = "0.9.4" diff --git a/Cargo.toml b/Cargo.toml index 389082d..0b1b9f7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,8 +12,8 @@ license = "AGPL-3.0-only" [dependencies] argon2 = { version = "0.5", features = ["std"] } clap = { version = "4.5", features = ["derive"] } +color-eyre = "0.6" env_logger = "0.11" -error-chain = "0.12" log = "0.4" serde = "1.0" serde_derive = "1.0" diff --git a/src/config.rs b/src/config.rs index 6421419..8a29e34 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,12 +1,8 @@ // SPDX-FileCopyrightText: 2024 Luflosi // SPDX-License-Identifier: AGPL-3.0-only -mod errors { - error_chain! {} -} -use errors::{Error, Result, ResultExt}; - use argon2::password_hash::PasswordHash; +use color_eyre::eyre::{eyre, Result, WrapErr}; use serde_derive::Deserialize; use std::collections::HashMap; use std::fs; @@ -71,9 +67,10 @@ pub struct User<'a> { impl Config<'_> { pub fn read(filename: &Path) -> Result> { let contents = fs::read_to_string(filename) - .chain_err(|| format!("Cannot read config file `{}`", filename.display()))?; + .wrap_err_with(|| format!("Cannot read config file `{}`", filename.display()))?; let config_parse_err_msg = || format!("Cannot parse config file `{}`", filename.display()); - let raw_config: RawConfig = toml::from_str(&contents).chain_err(config_parse_err_msg)?; + let raw_config: RawConfig = + toml::from_str(&contents).wrap_err_with(config_parse_err_msg)?; let users: Result> = raw_config .users .into_iter() @@ -86,11 +83,9 @@ impl Config<'_> { }; if props.ipv6prefixlen > 128 { let prefixlen = props.ipv6prefixlen; - return Err(Error::from(format!( - "Prefix is longer than 128 bits: {prefixlen}" - )) - .chain_err(ipv6prefixlen_parse_err_msg) - .chain_err(config_parse_err_msg)); + return Err(eyre!("Prefix is longer than 128 bits: {prefixlen}")) + .wrap_err_with(ipv6prefixlen_parse_err_msg) + .wrap_err_with(config_parse_err_msg); }; } // TODO: figure out how to do this without leaking memory. I wish PasswordHash::new() took a String instead of &str @@ -98,8 +93,8 @@ impl Config<'_> { let user = User { // TODO: get rid of this piece of the code by somehow implementing deserialization for PasswordHash hash: PasswordHash::new(raw_hash) - .chain_err(|| format!("Cannot parse password hash of user {username}")) - .chain_err(config_parse_err_msg)?, + .wrap_err_with(|| format!("Cannot parse password hash of user {username}")) + .wrap_err_with(config_parse_err_msg)?, domains: raw_user.domains, }; Ok((username, user)) diff --git a/src/main.rs b/src/main.rs index e39faa4..3070b6f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,13 +5,11 @@ mod config; mod logging; mod process; -#[macro_use] -extern crate error_chain; - use crate::config::Config; use crate::process::{update, QueryParameters}; use clap::Parser; -use log::{error, info}; +use color_eyre::eyre::Result; +use log::info; use warp::Filter; #[derive(Parser, Debug)] @@ -23,24 +21,14 @@ struct Args { } #[tokio::main] -async fn main() { +async fn main() -> Result<()> { + color_eyre::install()?; + logging::setup(); let args = Args::parse(); - let config = match Config::read(&args.config) { - Ok(v) => v, - Err(e) => { - error!("ERROR: {e}"); - - /////// look at the chain of errors... /////// - for e in e.iter().skip(1) { - error!("caused by: {e}"); - } - - std::process::exit(1); - } - }; + let config = Config::read(&args.config)?; let listen = config.listen; let update = warp::get() @@ -51,4 +39,6 @@ async fn main() { info!("Listening on {listen}"); warp::serve(update).run(listen).await; + + Ok(()) }