From c6f575319d86c764f96b1af9c6e995d661dceddf Mon Sep 17 00:00:00 2001 From: Marek Date: Mon, 17 Jun 2024 17:10:41 +0200 Subject: [PATCH] fix: Stop using `displaydoc` (#8614) * Stop using `displaydoc` * Fix comment alignment This commit is unrelated to the solution in this branch. There's a new lint that produces a warning when lists in comments are not aligned well. --- Cargo.lock | 14 ----------- zebra-chain/Cargo.toml | 1 - zebra-chain/src/amount.rs | 30 ++++++++++++++++++++++- zebra-chain/src/value_balance.rs | 14 ++++++++++- zebra-consensus/Cargo.toml | 1 - zebra-consensus/src/router.rs | 17 +++++++++++-- zebra-script/Cargo.toml | 1 - zebra-script/src/lib.rs | 28 +++++++++++++++------ zebra-test/src/command/arguments/tests.rs | 6 ++--- 9 files changed, 80 insertions(+), 32 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c75730e5c..96ba23e3d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1251,17 +1251,6 @@ dependencies = [ "windows-sys 0.48.0", ] -[[package]] -name = "displaydoc" -version = "0.2.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "487585f4d0c6655fe74905e2504d8ad6908e4db67f744eb140876906c2f3175d" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.66", -] - [[package]] name = "document-features" version = "0.2.8" @@ -6012,7 +6001,6 @@ dependencies = [ "chrono", "color-eyre", "criterion", - "displaydoc", "ed25519-zebra", "equihash", "futures", @@ -6070,7 +6058,6 @@ dependencies = [ "bls12_381", "chrono", "color-eyre", - "displaydoc", "futures", "futures-util", "halo2_proofs", @@ -6255,7 +6242,6 @@ dependencies = [ name = "zebra-script" version = "1.0.0-beta.37" dependencies = [ - "displaydoc", "hex", "lazy_static", "thiserror", diff --git a/zebra-chain/Cargo.toml b/zebra-chain/Cargo.toml index 169c0cfc3..ba3b5bee1 100644 --- a/zebra-chain/Cargo.toml +++ b/zebra-chain/Cargo.toml @@ -107,7 +107,6 @@ chrono = { version = "0.4.38", default-features = false, features = ["clock", "s humantime = "2.1.0" # Error Handling & Formatting -displaydoc = "0.2.4" static_assertions = "1.1.0" thiserror = "1.0.61" tracing = "0.1.39" diff --git a/zebra-chain/src/amount.rs b/zebra-chain/src/amount.rs index ae7f57263..7251e0e24 100644 --- a/zebra-chain/src/amount.rs +++ b/zebra-chain/src/amount.rs @@ -429,8 +429,8 @@ where } } -#[derive(thiserror::Error, Debug, displaydoc::Display, Clone, PartialEq, Eq)] #[allow(missing_docs)] +#[derive(thiserror::Error, Debug, Clone, PartialEq, Eq)] /// Errors that can be returned when validating `Amount`s pub enum Error { /// input {value} is outside of valid range for zatoshi Amount, valid_range={range:?} @@ -462,6 +462,34 @@ pub enum Error { }, } +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str(&match self { + Error::Constraint { value, range } => format!( + "input {value} is outside of valid range for zatoshi Amount, valid_range={range:?}" + ), + Error::Convert { value, .. } => { + format!("{value} could not be converted to an i64 Amount") + } + Error::MultiplicationOverflow { + amount, + multiplier, + overflowing_result, + } => format!( + "overflow when calculating {amount}i64 * {multiplier}u64 = {overflowing_result}i128" + ), + Error::DivideByZero { amount } => format!("cannot divide amount {amount} by zero"), + Error::SumOverflow { + partial_sum, + remaining_items, + } => format!( + "overflow when summing i64 amounts; \ + partial sum: {partial_sum}, number of remaining items: {remaining_items}" + ), + }) + } +} + impl Error { /// Returns the invalid value for this error. /// diff --git a/zebra-chain/src/value_balance.rs b/zebra-chain/src/value_balance.rs index dc56f70b8..eb82a37df 100644 --- a/zebra-chain/src/value_balance.rs +++ b/zebra-chain/src/value_balance.rs @@ -6,6 +6,7 @@ use crate::{ transparent, }; +use core::fmt; use std::{borrow::Borrow, collections::HashMap}; #[cfg(any(test, feature = "proptest-impl"))] @@ -385,7 +386,7 @@ impl ValueBalance { } } -#[derive(thiserror::Error, Debug, displaydoc::Display, Clone, PartialEq, Eq)] +#[derive(thiserror::Error, Debug, Clone, PartialEq, Eq)] /// Errors that can be returned when validating a [`ValueBalance`] pub enum ValueBalanceError { /// transparent amount error {0} @@ -401,6 +402,17 @@ pub enum ValueBalanceError { Orchard(amount::Error), } +impl fmt::Display for ValueBalanceError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str(&match self { + Transparent(e) => format!("transparent amount err: {e}"), + Sprout(e) => format!("sprout amount err: {e}"), + Sapling(e) => format!("sapling amount err: {e}"), + Orchard(e) => format!("orchard amount err: {e}"), + }) + } +} + impl std::ops::Add for ValueBalance where C: Constraint, diff --git a/zebra-consensus/Cargo.toml b/zebra-consensus/Cargo.toml index 210d22701..e1b1a0eb3 100644 --- a/zebra-consensus/Cargo.toml +++ b/zebra-consensus/Cargo.toml @@ -44,7 +44,6 @@ rand = "0.8.5" rayon = "1.10.0" chrono = { version = "0.4.38", default-features = false, features = ["clock", "std"] } -displaydoc = "0.2.4" lazy_static = "1.4.0" once_cell = "1.18.0" serde = { version = "1.0.203", features = ["serde_derive"] } diff --git a/zebra-consensus/src/router.rs b/zebra-consensus/src/router.rs index d9545ff09..ba42896e5 100644 --- a/zebra-consensus/src/router.rs +++ b/zebra-consensus/src/router.rs @@ -12,13 +12,13 @@ //! Otherwise, verification of out-of-order and invalid blocks and transactions can hang //! indefinitely. +use core::fmt; use std::{ future::Future, pin::Pin, task::{Context, Poll}, }; -use displaydoc::Display; use futures::{FutureExt, TryFutureExt}; use thiserror::Error; use tokio::task::JoinHandle; @@ -91,7 +91,7 @@ where /// An error while semantically verifying a block. // // One or both of these error variants are at least 140 bytes -#[derive(Debug, Display, Error)] +#[derive(Debug, Error)] #[allow(missing_docs)] pub enum RouterError { /// Block could not be checkpointed @@ -100,6 +100,19 @@ pub enum RouterError { Block { source: Box }, } +impl fmt::Display for RouterError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str(&match self { + RouterError::Checkpoint { source } => { + format!("block could not be checkpointed due to: {source}") + } + RouterError::Block { source } => { + format!("block could not be full-verified due to: {source}") + } + }) + } +} + impl From for RouterError { fn from(err: VerifyCheckpointError) -> Self { RouterError::Checkpoint { diff --git a/zebra-script/Cargo.toml b/zebra-script/Cargo.toml index e3789f609..7793f2d83 100644 --- a/zebra-script/Cargo.toml +++ b/zebra-script/Cargo.toml @@ -20,7 +20,6 @@ zcash_script = "0.1.15" zebra-chain = { path = "../zebra-chain", version = "1.0.0-beta.37" } thiserror = "1.0.61" -displaydoc = "0.2.4" [dev-dependencies] hex = "0.4.3" diff --git a/zebra-script/src/lib.rs b/zebra-script/src/lib.rs index f34de6874..419daa3b6 100644 --- a/zebra-script/src/lib.rs +++ b/zebra-script/src/lib.rs @@ -5,9 +5,9 @@ // We allow unsafe code, so we can call zcash_script #![allow(unsafe_code)] +use core::fmt; use std::sync::Arc; -use displaydoc::Display; use thiserror::Error; use zcash_script::{ @@ -22,27 +22,39 @@ use zebra_chain::{ transparent, }; -#[derive(Copy, Clone, Debug, Display, Error, PartialEq, Eq)] -#[non_exhaustive] /// An Error type representing the error codes returned from zcash_script. +#[derive(Copy, Clone, Debug, Error, PartialEq, Eq)] +#[non_exhaustive] pub enum Error { - /// script failed to verify + /// script verification failed #[non_exhaustive] ScriptInvalid, - /// could not to deserialize tx + /// could not deserialize tx #[non_exhaustive] TxDeserialize, - /// input index out of bounds for transaction's inputs + /// input index out of bounds #[non_exhaustive] TxIndex, - /// tx is an invalid size for it's protocol + /// tx has an invalid size #[non_exhaustive] TxSizeMismatch, - /// encountered unknown error kind from zcash_script: {0} + /// unknown error from zcash_script: {0} #[non_exhaustive] Unknown(zcash_script_error_t), } +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str(&match self { + Error::ScriptInvalid => "script verification failed".to_owned(), + Error::TxDeserialize => "could not deserialize tx".to_owned(), + Error::TxIndex => "input index out of bounds".to_owned(), + Error::TxSizeMismatch => "tx has an invalid size".to_owned(), + Error::Unknown(e) => format!("unknown error from zcash_script: {e}"), + }) + } +} + impl From for Error { #[allow(non_upper_case_globals)] fn from(err_code: zcash_script_error_t) -> Error { diff --git a/zebra-test/src/command/arguments/tests.rs b/zebra-test/src/command/arguments/tests.rs index df5907d95..f6ca129fc 100644 --- a/zebra-test/src/command/arguments/tests.rs +++ b/zebra-test/src/command/arguments/tests.rs @@ -168,9 +168,9 @@ impl Argument { /// /// # Implementation /// - /// 1. Generate a list with less than ten random strings. Then proceed by selecting which strings - /// will become key value pairs, and generate a new random string for each value that needs to - /// be paired to an argument key. + /// 1. Generate a list with less than ten random strings. Then proceed by selecting which + /// strings will become key value pairs, and generate a new random string for each value that + /// needs to be paired to an argument key. pub fn list_strategy() -> impl Strategy> { // Generate a list with less than ten unique random strings. hash_set("\\PC+", 0..10)