Skip to content
This repository has been archived by the owner on Jun 11, 2022. It is now read-only.

Commit

Permalink
mockchain: Implement FromStr for BlockDate
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikhail Zabaluev committed Feb 13, 2019
1 parent 0bd775b commit feb9b98
Showing 1 changed file with 46 additions and 1 deletion.
47 changes: 46 additions & 1 deletion chain-impl-mockchain/src/block.rs
Expand Up @@ -3,7 +3,7 @@ use crate::key::{Hash, PrivateKey, PublicKey, Signature};
use crate::transaction::*;
use chain_core::property;

use std::fmt;
use std::{error, fmt, num::ParseIntError, str};

/// Non unique identifier of the transaction position in the
/// blockchain. There may be many transactions related to the same
Expand Down Expand Up @@ -107,6 +107,51 @@ impl fmt::Display for BlockDate {
}
}

#[derive(Debug)]
pub enum BlockDateParseError {
DotMissing,
BadEpochId(ParseIntError),
BadSlotId(ParseIntError),
}

const EXPECT_FORMAT_MESSAGE: &'static str = "expected block date format EPOCH.SLOT";

impl fmt::Display for BlockDateParseError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use BlockDateParseError::*;
match self {
DotMissing => write!(f, "{}", EXPECT_FORMAT_MESSAGE),
BadEpochId(_) => write!(f, "invalid epoch ID, {}", EXPECT_FORMAT_MESSAGE),
BadSlotId(_) => write!(f, "invalid slot ID, {}", EXPECT_FORMAT_MESSAGE),
}
}
}

impl error::Error for BlockDateParseError {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
use BlockDateParseError::*;
match self {
DotMissing => None,
BadEpochId(e) => Some(e),
BadSlotId(e) => Some(e),
}
}
}

impl str::FromStr for BlockDate {
type Err = BlockDateParseError;

fn from_str(s: &str) -> Result<BlockDate, BlockDateParseError> {
let (ep, sp) = match s.find('.') {
None => return Err(BlockDateParseError::DotMissing),
Some(pos) => s.split_at(pos),
};
let epoch = str::parse::<u64>(ep).map_err(|e| BlockDateParseError::BadEpochId(e))?;
let slot_id = str::parse::<u64>(sp).map_err(|e| BlockDateParseError::BadSlotId(e))?;
Ok(BlockDate { epoch, slot_id })
}
}

impl property::Block for Block {
type Id = Hash;
type Date = BlockDate;
Expand Down

0 comments on commit feb9b98

Please sign in to comment.