use anyhow::{ensure, Error};
use exonum_proto::ProtobufConvert;
use std::convert::TryFrom;
use crate::helpers::{Height, Round, ValidatorId};
pub mod schema;
impl ProtobufConvert for Height {
type ProtoStruct = u64;
fn to_pb(&self) -> Self::ProtoStruct {
self.0
}
fn from_pb(pb: Self::ProtoStruct) -> Result<Self, Error> {
Ok(Self(pb))
}
}
impl ProtobufConvert for Round {
type ProtoStruct = u32;
fn to_pb(&self) -> Self::ProtoStruct {
self.0
}
fn from_pb(pb: Self::ProtoStruct) -> Result<Self, Error> {
Ok(Self(pb))
}
}
impl ProtobufConvert for ValidatorId {
type ProtoStruct = u32;
fn to_pb(&self) -> Self::ProtoStruct {
u32::from(self.0)
}
fn from_pb(pb: Self::ProtoStruct) -> Result<Self, Error> {
ensure!(
u16::try_from(pb).is_ok(),
"{} is out of range for valid ValidatorId",
pb
);
Ok(Self(pb as u16))
}
}