Skip to content

Commit

Permalink
Applied lint fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Jakub Zajkowski committed Nov 26, 2024
1 parent 3b4c746 commit cad32f7
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 68 deletions.
28 changes: 0 additions & 28 deletions node/src/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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)]
Expand Down
39 changes: 18 additions & 21 deletions types/src/system/auction/delegator_kind.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand All @@ -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)]
Expand Down Expand Up @@ -163,7 +156,6 @@ impl Distribution<DelegatorKind> for Standard {
}
}

#[cfg(any(feature = "std", test))]
impl Serialize for DelegatorKind {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
if serializer.is_human_readable() {
Expand All @@ -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<HumanReadableDelegatorKind> for DelegatorKind {
type Error = DelegatorKindError;

Expand All @@ -191,7 +190,7 @@ impl TryFrom<HumanReadableDelegatorKind> 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
Expand Down Expand Up @@ -219,7 +218,7 @@ impl From<NonHumanReadableDelegatorKind> for DelegatorKind {
}
}
}
#[cfg(any(feature = "std", test))]

impl<'de> Deserialize<'de> for DelegatorKind {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
if deserializer.is_human_readable() {
Expand All @@ -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)]
Expand Down Expand Up @@ -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),
}
}
}
Expand Down
38 changes: 19 additions & 19 deletions types/src/system/auction/unbond.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down Expand Up @@ -151,7 +145,6 @@ impl From<DelegatorKind> for UnbondKind {
}
}

#[cfg(any(feature = "std", test))]
impl Serialize for UnbondKind {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
if serializer.is_human_readable() {
Expand All @@ -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<HumanReadableUnbondKind> for UnbondKind {
type Error = UnbondKindError;

Expand All @@ -180,7 +180,7 @@ impl TryFrom<HumanReadableUnbondKind> 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
Expand Down Expand Up @@ -211,7 +211,7 @@ impl From<NonHumanReadableUnbondKind> for UnbondKind {
}
}
}
#[cfg(any(feature = "std", test))]

impl<'de> Deserialize<'de> for UnbondKind {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
if deserializer.is_human_readable() {
Expand Down Expand Up @@ -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)]
Expand Down Expand Up @@ -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)
}
}
}
Expand Down

0 comments on commit cad32f7

Please sign in to comment.