Skip to content

Commit

Permalink
Refactoring: rename serializers (#264)
Browse files Browse the repository at this point in the history
* rename/flatten serializer methods (primitives and time_duration)

* Fix all clippy warnings:

 - `cargo clippy --all --all-targets -- -Dwarnings -Drust-2018-idioms`
  • Loading branch information
liamsi authored May 8, 2020
1 parent b9bbb40 commit 89b13ae
Show file tree
Hide file tree
Showing 17 changed files with 93 additions and 100 deletions.
2 changes: 1 addition & 1 deletion tendermint/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ where
#[derive(Deserialize)]
struct TmpCommit {
pub height: Height,
#[serde(with = "serializers::primitives::string")]
#[serde(with = "serializers::from_str")]
pub round: u64,
#[serde(deserialize_with = "serializers::parse_non_empty_block_id")]
pub block_id: Option<Id>,
Expand Down
2 changes: 1 addition & 1 deletion tendermint/src/block/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub struct Commit {
pub height: Height,

/// Round
#[serde(with = "serializers::primitives::string")]
#[serde(with = "serializers::from_str")]
pub round: u64,

/// Block ID
Expand Down
4 changes: 2 additions & 2 deletions tendermint/src/block/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ pub struct Header {
#[derive(Serialize, Deserialize, Clone, PartialEq, Debug)]
pub struct Version {
/// Block version
#[serde(with = "serializers::primitives::string")]
#[serde(with = "serializers::from_str")]
pub block: u64,

/// App version
#[serde(with = "serializers::primitives::string")]
#[serde(with = "serializers::from_str")]
pub app: u64,
}

Expand Down
2 changes: 1 addition & 1 deletion tendermint/src/block/parts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use {
#[derive(Serialize, Deserialize, Clone, Debug, Hash, Eq, PartialEq, PartialOrd, Ord)]
pub struct Header {
/// Number of parts in this block
#[serde(with = "serializers::primitives::string")]
#[serde(with = "serializers::from_str")]
pub total: u64,

/// Hash of the parts set header,
Expand Down
4 changes: 2 additions & 2 deletions tendermint/src/block/size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ use {
#[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq)]
pub struct Size {
/// Maximum number of bytes in a block
#[serde(with = "serializers::primitives::string")]
#[serde(with = "serializers::from_str")]
pub max_bytes: u64,

/// Maximum amount of gas which can be spent on a block
#[serde(with = "serializers::primitives::string")]
#[serde(with = "serializers::from_str")]
pub max_gas: i64,
}
8 changes: 4 additions & 4 deletions tendermint/src/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@ pub struct Channel {
pub id: Id,

/// Capacity of the send queue
#[serde(rename = "SendQueueCapacity", with = "serializers::primitives::string")]
#[serde(rename = "SendQueueCapacity", with = "serializers::from_str")]
pub send_queue_capacity: u64,

/// Size of the send queue
#[serde(rename = "SendQueueSize", with = "serializers::primitives::string")]
#[serde(rename = "SendQueueSize", with = "serializers::from_str")]
pub send_queue_size: u64,

/// Priority value
#[serde(rename = "Priority", with = "serializers::primitives::string")]
#[serde(rename = "Priority", with = "serializers::from_str")]
pub priority: u64,

/// Amount of data recently sent
#[serde(rename = "RecentlySent", with = "serializers::primitives::string")]
#[serde(rename = "RecentlySent", with = "serializers::from_str")]
pub recently_sent: u64,
}

Expand Down
2 changes: 1 addition & 1 deletion tendermint/src/consensus/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub struct State {
pub height: block::Height,

/// Current consensus round
#[serde(with = "serializers::primitives::string")]
#[serde(with = "serializers::from_str")]
pub round: i64,

/// Current consensus step
Expand Down
4 changes: 2 additions & 2 deletions tendermint/src/evidence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ impl AsRef<[Evidence]> for Data {
#[derive(Deserialize, Serialize, Clone, Debug, Eq, PartialEq)]
pub struct Params {
/// Maximum allowed age for evidence to be collected
#[serde(with = "serializers::primitives::string")]
#[serde(with = "serializers::from_str")]
pub max_age_num_blocks: u64,

/// Max age duration
Expand All @@ -81,7 +81,7 @@ pub struct Params {
/// essentially, to keep the usages look cleaner
/// i.e. you can avoid using serde annotations everywhere
#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
pub struct Duration(#[serde(with = "serializers::timeduration::string")] std::time::Duration);
pub struct Duration(#[serde(with = "serializers::time_duration")] std::time::Duration);

impl From<Duration> for std::time::Duration {
fn from(d: Duration) -> std::time::Duration {
Expand Down
4 changes: 2 additions & 2 deletions tendermint/src/lite/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ pub trait TrustThreshold: Copy + Clone + Debug + Serialize + DeserializeOwned {
/// [`TrustThreshold`] which can be passed into all relevant methods.
#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct TrustThresholdFraction {
#[serde(with = "serializers::primitives::string")]
#[serde(with = "serializers::from_str")]
numerator: u64,
#[serde(with = "serializers::primitives::string")]
#[serde(with = "serializers::from_str")]
denominator: u64,
}

Expand Down
6 changes: 3 additions & 3 deletions tendermint/src/node/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ pub struct Info {
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct ProtocolVersionInfo {
/// P2P protocol version
#[serde(with = "serializers::primitives::string")]
#[serde(with = "serializers::from_str")]
pub p2p: u64,

/// Block version
#[serde(with = "serializers::primitives::string")]
#[serde(with = "serializers::from_str")]
pub block: u64,

/// App version
#[serde(with = "serializers::primitives::string")]
#[serde(with = "serializers::from_str")]
pub app: u64,
}

Expand Down
2 changes: 1 addition & 1 deletion tendermint/src/rpc/endpoint/abci_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub struct AbciInfo {
pub version: String,

/// App version
#[serde(with = "serializers::primitives::string")]
#[serde(with = "serializers::from_str")]
pub app_version: u64,

/// Last block height
Expand Down
2 changes: 1 addition & 1 deletion tendermint/src/rpc/endpoint/abci_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ pub struct AbciQuery {
pub info: String,

/// Index
#[serde(with = "serializers::primitives::string")]
#[serde(with = "serializers::from_str")]
pub index: i64,

/// Key
Expand Down
24 changes: 12 additions & 12 deletions tendermint/src/rpc/endpoint/net_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub struct Response {
pub listeners: Vec<Listener>,

/// Number of connected peers
#[serde(with = "serializers::primitives::string")]
#[serde(with = "serializers::from_str")]
pub n_peers: u64,

/// Peer information
Expand Down Expand Up @@ -69,7 +69,7 @@ pub struct PeerInfo {
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct ConnectionStatus {
/// Duration of this connection
#[serde(rename = "Duration", with = "serializers::timeduration::string")]
#[serde(rename = "Duration", with = "serializers::time_duration")]
pub duration: Duration,

/// Send monitor
Expand Down Expand Up @@ -97,43 +97,43 @@ pub struct Monitor {
pub start: Time,

/// Duration of this monitor
#[serde(rename = "Duration", with = "serializers::timeduration::string")]
#[serde(rename = "Duration", with = "serializers::time_duration")]
pub duration: Duration,

/// Idle duration for this monitor
#[serde(rename = "Idle", with = "serializers::timeduration::string")]
#[serde(rename = "Idle", with = "serializers::time_duration")]
pub idle: Duration,

/// Bytes
#[serde(rename = "Bytes", with = "serializers::primitives::string")]
#[serde(rename = "Bytes", with = "serializers::from_str")]
bytes: u64,

/// Samples
#[serde(rename = "Samples", with = "serializers::primitives::string")]
#[serde(rename = "Samples", with = "serializers::from_str")]
samples: u64,

/// Instant rate
#[serde(rename = "InstRate", with = "serializers::primitives::string")]
#[serde(rename = "InstRate", with = "serializers::from_str")]
inst_rate: u64,

/// Current rate
#[serde(rename = "CurRate", with = "serializers::primitives::string")]
#[serde(rename = "CurRate", with = "serializers::from_str")]
cur_rate: u64,

/// Average rate
#[serde(rename = "AvgRate", with = "serializers::primitives::string")]
#[serde(rename = "AvgRate", with = "serializers::from_str")]
avg_rate: u64,

/// Peak rate
#[serde(rename = "PeakRate", with = "serializers::primitives::string")]
#[serde(rename = "PeakRate", with = "serializers::from_str")]
peak_rate: u64,

/// Bytes remaining
#[serde(rename = "BytesRem", with = "serializers::primitives::string")]
#[serde(rename = "BytesRem", with = "serializers::from_str")]
bytes_rem: u64,

/// Time remaining
#[serde(rename = "TimeRem", with = "serializers::primitives::string")]
#[serde(rename = "TimeRem", with = "serializers::from_str")]
time_rem: u64,

/// Progress
Expand Down
100 changes: 47 additions & 53 deletions tendermint/src/serializers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
//! ```
//!
//! Available serializers:
//! i64 <-> string: #[serde(with="serializers::primitives::string")]
//! u64 <-> string: #[serde(with="serializers::primitives::string")]
//! std::time::Dureation <-> nanoseconds as string #[serde(with="serializers::timeduration::string")]
//! i64 <-> string: #[serde(with="serializers::from_str")]
//! u64 <-> string: #[serde(with="serializers::from_str")]
//! std::time::Dureation <-> nanoseconds as string #[serde(with="serializers::time_duration")]
//! Vec<u8> <-> HexString: #[serde(with="serializers::bytes::hexstring")]
//! Vec<u8> <-> Base64String: #[serde(with="serializers::bytes::base64string")]
//! Vec<u8> <-> String: #[serde(with="serializers::bytes::string")]
Expand All @@ -40,63 +40,57 @@ use crate::{block, Hash, Signature};
use serde::{de::Error as _, Deserialize, Deserializer, Serialize, Serializer};
use std::str::FromStr;

/// Serialize/deserialize primitive types (i64, u64, etc)
pub mod primitives {
/// Serialize and deserialize any `T` that implements [[std::str::FromStr]]
/// and [[std::fmt::Display]] from or into string. Note this be used for
/// all primitive data types (e.g. .
pub mod from_str {
use serde::{de::Error as _, Deserialize, Deserializer, Serialize, Serializer};

/// Serialize into string, deserialize from string
pub mod string {
use serde::{de::Error as _, Deserialize, Deserializer, Serialize, Serializer};

/// Deserialize string into T
pub fn deserialize<'de, D, T>(deserializer: D) -> Result<T, D::Error>
where
D: Deserializer<'de>,
T: std::str::FromStr,
<T as std::str::FromStr>::Err: std::fmt::Display,
{
String::deserialize(deserializer)?
.parse::<T>()
.map_err(|e| D::Error::custom(format!("{}", e)))
}
/// Deserialize string into T
pub fn deserialize<'de, D, T>(deserializer: D) -> Result<T, D::Error>
where
D: Deserializer<'de>,
T: std::str::FromStr,
<T as std::str::FromStr>::Err: std::fmt::Display,
{
String::deserialize(deserializer)?
.parse::<T>()
.map_err(|e| D::Error::custom(format!("{}", e)))
}

/// Serialize from T into string
pub fn serialize<S, T>(value: &T, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
T: std::fmt::Display,
{
format!("{}", value).serialize(serializer)
}
/// Serialize from T into string
pub fn serialize<S, T>(value: &T, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
T: std::fmt::Display,
{
format!("{}", value).serialize(serializer)
}
}

/// Serialize/deserialize std::time::Duration type
pub mod timeduration {

/// Serialize into string, deserialize from string
pub mod string {
use serde::{de::Error as _, Deserialize, Deserializer, Serialize, Serializer};
use std::time::Duration;
/// Serialize/deserialize std::time::Duration type from and into string:
pub mod time_duration {
use serde::{de::Error as _, Deserialize, Deserializer, Serialize, Serializer};
use std::time::Duration;

/// Deserialize string into Duration
pub fn deserialize<'de, D>(deserializer: D) -> Result<Duration, D::Error>
where
D: Deserializer<'de>,
{
let value = String::deserialize(deserializer)?
.parse::<u64>()
.map_err(|e| D::Error::custom(format!("{}", e)))?;
/// Deserialize string into Duration
pub fn deserialize<'de, D>(deserializer: D) -> Result<Duration, D::Error>
where
D: Deserializer<'de>,
{
let value = String::deserialize(deserializer)?
.parse::<u64>()
.map_err(|e| D::Error::custom(format!("{}", e)))?;

Ok(Duration::from_nanos(value))
}
Ok(Duration::from_nanos(value))
}

/// Serialize from Duration into string
pub fn serialize<S>(value: &Duration, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
format!("{}", value.as_nanos()).serialize(serializer)
}
/// Serialize from Duration into string
pub fn serialize<S>(value: &Duration, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
format!("{}", value.as_nanos()).serialize(serializer)
}
}

Expand Down Expand Up @@ -245,7 +239,7 @@ where
{
#[derive(Deserialize)]
struct Parts {
#[serde(with = "primitives::string")]
#[serde(with = "from_str")]
total: u64,
hash: String,
}
Expand Down
4 changes: 2 additions & 2 deletions tendermint/src/vote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub struct Vote {
pub height: block::Height,

/// Round
#[serde(with = "serializers::primitives::string")]
#[serde(with = "serializers::from_str")]
pub round: u64,

/// Block ID
Expand All @@ -39,7 +39,7 @@ pub struct Vote {
pub validator_address: account::Id,

/// Validator index
#[serde(with = "serializers::primitives::string")]
#[serde(with = "serializers::from_str")]
pub validator_index: u64,

/// Signature
Expand Down
1 change: 0 additions & 1 deletion tendermint/tests/lite.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use anomaly::fail;
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use serde_json;
use std::collections::HashMap;
use std::convert::TryInto;
use std::{fs, path::PathBuf};
Expand Down
Loading

0 comments on commit 89b13ae

Please sign in to comment.