From cad32f7045be3e8092567004e354463ceb595b15 Mon Sep 17 00:00:00 2001 From: Jakub Zajkowski Date: Tue, 26 Nov 2024 23:33:32 +0100 Subject: [PATCH] Applied lint fixes --- node/src/testing.rs | 28 ---------------- types/src/system/auction/delegator_kind.rs | 39 ++++++++++------------ types/src/system/auction/unbond.rs | 38 ++++++++++----------- 3 files changed, 37 insertions(+), 68 deletions(-) diff --git a/node/src/testing.rs b/node/src/testing.rs index f9f45a31bd..42a9ea5f59 100644 --- a/node/src/testing.rs +++ b/node/src/testing.rs @@ -26,7 +26,6 @@ use derive_more::From; use futures::channel::oneshot; use once_cell::sync::Lazy; use rand::Rng; -use regex::Regex; use serde_json::Value; use tempfile::TempDir; use tokio::runtime::{self, Runtime}; @@ -425,33 +424,6 @@ pub fn assert_schema(schema_path: String, actual_schema: String) { temp_file_path.display() ); assert_json_eq!(actual_schema, expected_schema); - - // Check for the following pattern in the JSON as this points to a byte array or vec (e.g. - // a hash digest) not being represented as a hex-encoded string: - // - // ```json - // "type": "array", - // "items": { - // "type": "integer", - // "format": "uint8", - // "minimum": 0.0 - // }, - // ``` - // - // The type/variant in question (most easily identified from the git diff) might be easily - // fixed via application of a serde attribute, e.g. - // `#[serde(with = "serde_helpers::raw_32_byte_array")]`. It will likely require a - // schemars attribute too, indicating it is a hex-encoded string. See for example - // `TransactionInvocationTarget::Package::addr`. - let schema = fs::read_to_string(&schema_path).unwrap(); - let regex = Regex::new( - r#"\s*"type":\s*"array",\s*"items":\s*\{\s*"type":\s*"integer",\s*"format":\s*"uint8",\s*"minimum":\s*0\.0\s*\},"# - ).unwrap(); - assert!( - !regex.is_match(&schema), - "seems like a byte array is not hex-encoded - see comment in `json_schema_check` for \ - further info" - ); } #[cfg(test)] diff --git a/types/src/system/auction/delegator_kind.rs b/types/src/system/auction/delegator_kind.rs index 5485bb190b..6227b9e76b 100644 --- a/types/src/system/auction/delegator_kind.rs +++ b/types/src/system/auction/delegator_kind.rs @@ -1,11 +1,9 @@ -#[cfg(any(feature = "std", test))] -use crate::checksummed_hex; use crate::{ bytesrepr, bytesrepr::{FromBytes, ToBytes, U8_SERIALIZED_LENGTH}, - CLType, CLTyped, PublicKey, URef, URefAddr, + checksummed_hex, CLType, CLTyped, PublicKey, URef, URefAddr, }; -use alloc::vec::Vec; +use alloc::{string::String, vec::Vec}; use core::{ fmt, fmt::{Display, Formatter}, @@ -19,13 +17,8 @@ use rand::{ }; #[cfg(feature = "json-schema")] use schemars::JsonSchema; -#[cfg(any(feature = "std", test))] -use serde::{de::Error as SerdeError, Deserializer, Serializer}; -use serde::{Deserialize, Serialize}; -#[cfg(any(feature = "std", test))] +use serde::{de::Error as SerdeError, Deserialize, Deserializer, Serialize, Serializer}; use serde_helpers::{HumanReadableDelegatorKind, NonHumanReadableDelegatorKind}; -#[cfg(any(feature = "std", test))] -use thiserror::Error; /// DelegatorKindTag variants. #[repr(u8)] @@ -163,7 +156,6 @@ impl Distribution for Standard { } } -#[cfg(any(feature = "std", test))] impl Serialize for DelegatorKind { fn serialize(&self, serializer: S) -> Result { if serializer.is_human_readable() { @@ -174,14 +166,21 @@ impl Serialize for DelegatorKind { } } -#[cfg(any(feature = "std", test))] -#[derive(Error, Debug)] +#[derive(Debug)] enum DelegatorKindError { - #[error("Error when deserializing DelegatorKind: {0}")] DeserializationError(String), } -#[cfg(any(feature = "std", test))] +impl Display for DelegatorKindError { + fn fmt(&self, f: &mut Formatter) -> fmt::Result { + match self { + DelegatorKindError::DeserializationError(error) => { + write!(f, "Error when deserializing DelegatorKind: {}", error) + } + } + } +} + impl TryFrom for DelegatorKind { type Error = DelegatorKindError; @@ -191,7 +190,7 @@ impl TryFrom for DelegatorKind { Ok(DelegatorKind::PublicKey(public_key)) } HumanReadableDelegatorKind::Purse(encoded) => { - let decoded = checksummed_hex::decode(&encoded).map_err(|e| { + let decoded = checksummed_hex::decode(encoded).map_err(|e| { DelegatorKindError::DeserializationError(format!( "Failed to decode encoded URefAddr: {}", e @@ -219,7 +218,7 @@ impl From for DelegatorKind { } } } -#[cfg(any(feature = "std", test))] + impl<'de> Deserialize<'de> for DelegatorKind { fn deserialize>(deserializer: D) -> Result { if deserializer.is_human_readable() { @@ -233,10 +232,10 @@ impl<'de> Deserialize<'de> for DelegatorKind { } } -#[cfg(any(feature = "std", test))] mod serde_helpers { use super::DelegatorKind; use crate::{PublicKey, URefAddr}; + use alloc::string::String; use serde::{Deserialize, Serialize}; #[derive(Serialize, Deserialize)] @@ -270,9 +269,7 @@ mod serde_helpers { DelegatorKind::PublicKey(public_key) => { NonHumanReadableDelegatorKind::PublicKey(public_key.clone()) } - DelegatorKind::Purse(uref_addr) => { - NonHumanReadableDelegatorKind::Purse(uref_addr.clone()) - } + DelegatorKind::Purse(uref_addr) => NonHumanReadableDelegatorKind::Purse(*uref_addr), } } } diff --git a/types/src/system/auction/unbond.rs b/types/src/system/auction/unbond.rs index 083c6e337c..7e5114844d 100644 --- a/types/src/system/auction/unbond.rs +++ b/types/src/system/auction/unbond.rs @@ -1,23 +1,17 @@ -#[cfg(any(feature = "std", test))] -use crate::checksummed_hex; -use alloc::vec::Vec; +use alloc::{string::String, vec::Vec}; +use core::fmt::{self, Display, Formatter}; #[cfg(feature = "datasize")] use datasize::DataSize; #[cfg(feature = "json-schema")] use schemars::JsonSchema; -use serde::{Deserialize, Serialize}; use super::{BidAddr, DelegatorKind, UnbondingPurse, WithdrawPurse}; use crate::{ bytesrepr::{self, FromBytes, ToBytes, U8_SERIALIZED_LENGTH}, - CLType, CLTyped, EraId, PublicKey, URef, URefAddr, U512, + checksummed_hex, CLType, CLTyped, EraId, PublicKey, URef, URefAddr, U512, }; -#[cfg(any(feature = "std", test))] -use serde::{de::Error as SerdeError, Deserializer, Serializer}; -#[cfg(any(feature = "std", test))] +use serde::{de::Error as SerdeError, Deserialize, Deserializer, Serialize, Serializer}; use serde_helpers::{HumanReadableUnbondKind, NonHumanReadableUnbondKind}; -#[cfg(any(feature = "std", test))] -use thiserror::Error; /// UnbondKindTag variants. #[allow(clippy::large_enum_variant)] @@ -151,7 +145,6 @@ impl From for UnbondKind { } } -#[cfg(any(feature = "std", test))] impl Serialize for UnbondKind { fn serialize(&self, serializer: S) -> Result { if serializer.is_human_readable() { @@ -162,14 +155,21 @@ impl Serialize for UnbondKind { } } -#[cfg(any(feature = "std", test))] -#[derive(Error, Debug)] +#[derive(Debug)] enum UnbondKindError { - #[error("Error when deserializing UnbondKind: {0}")] DeserializationError(String), } -#[cfg(any(feature = "std", test))] +impl Display for UnbondKindError { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + match self { + UnbondKindError::DeserializationError(msg) => { + write!(f, "Error when deserializing UnbondKind: {}", msg) + } + } + } +} + impl TryFrom for UnbondKind { type Error = UnbondKindError; @@ -180,7 +180,7 @@ impl TryFrom for UnbondKind { Ok(UnbondKind::DelegatedPublicKey(public_key)) } HumanReadableUnbondKind::DelegatedPurse(encoded) => { - let decoded = checksummed_hex::decode(&encoded).map_err(|e| { + let decoded = checksummed_hex::decode(encoded).map_err(|e| { UnbondKindError::DeserializationError(format!( "Failed to decode encoded URefAddr: {}", e @@ -211,7 +211,7 @@ impl From for UnbondKind { } } } -#[cfg(any(feature = "std", test))] + impl<'de> Deserialize<'de> for UnbondKind { fn deserialize>(deserializer: D) -> Result { if deserializer.is_human_readable() { @@ -552,10 +552,10 @@ impl CLTyped for UnbondEra { } } -#[cfg(any(feature = "std", test))] mod serde_helpers { use super::UnbondKind; use crate::{PublicKey, URefAddr}; + use alloc::string::String; use serde::{Deserialize, Serialize}; #[derive(Serialize, Deserialize)] @@ -598,7 +598,7 @@ mod serde_helpers { NonHumanReadableUnbondKind::DelegatedPublicKey(public_key.clone()) } UnbondKind::DelegatedPurse(uref_addr) => { - NonHumanReadableUnbondKind::DelegatedPurse(uref_addr.clone()) + NonHumanReadableUnbondKind::DelegatedPurse(*uref_addr) } } }