Remove duplicated macro definitions

This commit is contained in:
rishflab 2020-10-09 10:17:36 +11:00
parent 666c121bb3
commit 51760041f0
5 changed files with 73 additions and 250 deletions

View file

@ -19,7 +19,7 @@ use sha2::Sha256;
use std::convert::{TryFrom, TryInto};
pub mod message;
pub use message::{Message, Message0, Message1, Message2, Message3, UnexpectedMessage};
pub use message::{Message, Message0, Message1, Message2, Message3};
pub async fn next_state<
R: RngCore + CryptoRng,
@ -85,44 +85,19 @@ pub enum State {
State5(State5),
}
macro_rules! impl_try_from_parent_state {
($type:ident) => {
impl TryFrom<State> for $type {
type Error = anyhow::Error;
fn try_from(from: State) -> Result<Self> {
if let State::$type(state) = from {
Ok(state)
} else {
Err(anyhow!("Failed to convert parent state to child state"))
}
}
}
};
}
impl_try_from_parent_enum!(State0, State);
impl_try_from_parent_enum!(State1, State);
impl_try_from_parent_enum!(State2, State);
impl_try_from_parent_enum!(State3, State);
impl_try_from_parent_enum!(State4, State);
impl_try_from_parent_enum!(State5, State);
impl_try_from_parent_state!(State0);
impl_try_from_parent_state!(State1);
impl_try_from_parent_state!(State2);
impl_try_from_parent_state!(State3);
impl_try_from_parent_state!(State4);
impl_try_from_parent_state!(State5);
macro_rules! impl_from_child_state {
($type:ident) => {
impl From<$type> for State {
fn from(from: $type) -> Self {
State::$type(from)
}
}
};
}
impl_from_child_state!(State0);
impl_from_child_state!(State1);
impl_from_child_state!(State2);
impl_from_child_state!(State3);
impl_from_child_state!(State4);
impl_from_child_state!(State5);
impl_from_child_enum!(State0, State);
impl_from_child_enum!(State1, State);
impl_from_child_enum!(State2, State);
impl_from_child_enum!(State3, State);
impl_from_child_enum!(State4, State);
impl_from_child_enum!(State5, State);
#[derive(Debug)]
pub struct State0 {