Skip to content

Commit

Permalink
Implement TryFrom<i64> for Height
Browse files Browse the repository at this point in the history
  • Loading branch information
mappum committed Dec 2, 2019
1 parent 4cbe3a8 commit e5826c3
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 27 deletions.
7 changes: 4 additions & 3 deletions tendermint/src/amino_types/proposal.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::convert::TryFrom;
use super::{
block_id::{BlockId, CanonicalBlockId, CanonicalPartSetHeader},
remote_error::RemoteError,
Expand Down Expand Up @@ -35,7 +36,7 @@ pub struct Proposal {
// TODO(tony): custom derive proc macro for this e.g. `derive(ParseBlockHeight)`
impl block::ParseHeight for Proposal {
fn parse_block_height(&self) -> Result<block::Height, Error> {
block::Height::try_from_i64(self.height)
block::Height::try_from(self.height)
}
}

Expand Down Expand Up @@ -74,7 +75,7 @@ impl chain::ParseId for CanonicalProposal {

impl block::ParseHeight for CanonicalProposal {
fn parse_block_height(&self) -> Result<block::Height, Error> {
block::Height::try_from_i64(self.height)
block::Height::try_from(self.height)
}
}

Expand Down Expand Up @@ -136,7 +137,7 @@ impl SignableMsg for SignProposalRequest {
fn consensus_state(&self) -> Option<consensus::State> {
match self.proposal {
Some(ref p) => Some(consensus::State {
height: match block::Height::try_from_i64(p.height) {
height: match block::Height::try_from(p.height) {
Ok(h) => h,
Err(_err) => return None, // TODO(tarcieri): return an error?
},
Expand Down
7 changes: 4 additions & 3 deletions tendermint/src/amino_types/vote.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::convert::TryFrom;
use super::{
block_id::{BlockId, CanonicalBlockId, CanonicalPartSetHeader},
remote_error::RemoteError,
Expand Down Expand Up @@ -51,7 +52,7 @@ impl Vote {

impl block::ParseHeight for Vote {
fn parse_block_height(&self) -> Result<block::Height, Error> {
block::Height::try_from_i64(self.height)
block::Height::try_from(self.height)
}
}

Expand Down Expand Up @@ -97,7 +98,7 @@ impl chain::ParseId for CanonicalVote {

impl block::ParseHeight for CanonicalVote {
fn parse_block_height(&self) -> Result<block::Height, Error> {
block::Height::try_from_i64(self.height)
block::Height::try_from(self.height)
}
}

Expand Down Expand Up @@ -162,7 +163,7 @@ impl SignableMsg for SignVoteRequest {
fn consensus_state(&self) -> Option<consensus::State> {
match self.vote {
Some(ref v) => Some(consensus::State {
height: match block::Height::try_from_i64(v.height) {
height: match block::Height::try_from(v.height) {
Ok(h) => h,
Err(_err) => return None, // TODO(tarcieri): return an error?
},
Expand Down
32 changes: 11 additions & 21 deletions tendermint/src/block/height.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::error::{Error, ErrorKind};
use serde::{de::Error as _, Deserialize, Deserializer, Serialize, Serializer};
use std::{
convert::TryFrom,
fmt::{self, Debug, Display},
str::FromStr,
};
Expand All @@ -13,23 +14,6 @@ use std::{
pub struct Height(u64);

impl Height {
/// Convert `u64` to block height.
///
/// Note that this method will never error and is just for backwards
/// compatibility.
pub fn try_from_u64(n: u64) -> Result<Self, Error> {
Ok(Height(n))
}

/// Convert `i64` (used in e.g. Amino messages) to block height.
pub fn try_from_i64(n: i64) -> Result<Self, Error> {
if n >= 0 {
Ok(Height(n as u64))
} else {
Err(ErrorKind::OutOfRange.into())
}
}

/// Get inner integer value. Alternative to `.0` or `.into()`
pub fn value(self) -> u64 {
self.0
Expand Down Expand Up @@ -59,9 +43,15 @@ impl Display for Height {
}
}

impl From<i64> for Height {
fn from(n: i64) -> Height {
Self::try_from_i64(n).unwrap()
impl TryFrom<i64> for Height {
type Error = Error;

fn try_from(n: i64) -> Result<Height, Error> {
if n >= 0 {
Ok(Height(n as u64))
} else {
Err(ErrorKind::OutOfRange.into())
}
}
}

Expand All @@ -87,7 +77,7 @@ impl FromStr for Height {
type Err = Error;

fn from_str(s: &str) -> Result<Self, Error> {
Self::try_from_u64(s.parse::<u64>().map_err(|_| ErrorKind::Parse)?)
Ok(s.parse::<u64>().map_err(|_| ErrorKind::Parse)?.into())
}
}

Expand Down

0 comments on commit e5826c3

Please sign in to comment.