Remove duplicated macro definitions

pull/2/head
rishflab 4 years ago
parent 666c121bb3
commit 51760041f0

@ -15,7 +15,7 @@ use sha2::Sha256;
use std::convert::{TryFrom, TryInto};
pub mod message;
pub use message::{Message, Message0, Message1, Message2, UnexpectedMessage};
pub use message::{Message, Message0, Message1, Message2};
pub async fn next_state<
R: RngCore + CryptoRng,
@ -88,46 +88,21 @@ pub enum State {
State6(State6),
}
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_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);
impl_try_from_parent_state!(State6);
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_state!(State6);
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_enum!(State6, State);
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);
impl_from_child_enum!(State6, State);
impl State {
pub fn new<R: RngCore + CryptoRng>(

@ -33,80 +33,10 @@ pub struct Message2 {
pub(crate) tx_lock_proof: monero::TransferProof,
}
impl From<Message0> for Message {
fn from(m: Message0) -> Self {
Message::Message0(m)
}
}
impl TryFrom<Message> for Message0 {
type Error = UnexpectedMessage;
fn try_from(m: Message) -> Result<Self, Self::Error> {
match m {
Message::Message0(m) => Ok(m),
_ => Err(UnexpectedMessage {
expected_type: "Create0".to_string(),
received: m,
}),
}
}
}
impl From<Message1> for Message {
fn from(m: Message1) -> Self {
Message::Message1(m)
}
}
impl TryFrom<Message> for Message1 {
type Error = UnexpectedMessage;
fn try_from(m: Message) -> Result<Self, Self::Error> {
match m {
Message::Message1(m) => Ok(m),
_ => Err(UnexpectedMessage {
expected_type: "Create1".to_string(),
received: m,
}),
}
}
}
impl_try_from_parent_enum!(Message0, Message);
impl_try_from_parent_enum!(Message1, Message);
impl_try_from_parent_enum!(Message2, Message);
impl From<Message2> for Message {
fn from(m: Message2) -> Self {
Message::Message2(m)
}
}
impl TryFrom<Message> for Message2 {
type Error = UnexpectedMessage;
fn try_from(m: Message) -> Result<Self, Self::Error> {
match m {
Message::Message2(m) => Ok(m),
_ => Err(UnexpectedMessage {
expected_type: "Create2".to_string(),
received: m,
}),
}
}
}
#[derive(Debug, thiserror::Error)]
#[error("expected message of type {expected_type}, got {received:?}")]
pub struct UnexpectedMessage {
expected_type: String,
received: Message,
}
impl UnexpectedMessage {
pub fn new<T>(received: Message) -> Self {
let expected_type = std::any::type_name::<T>();
Self {
expected_type: expected_type.to_string(),
received,
}
}
}
impl_from_child_enum!(Message0, Message);
impl_from_child_enum!(Message1, Message);
impl_from_child_enum!(Message2, Message);

@ -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_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_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_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 {

@ -37,100 +37,12 @@ pub struct Message3 {
pub(crate) tx_redeem_encsig: EncryptedSignature,
}
impl From<Message0> for Message {
fn from(m: Message0) -> Self {
Message::Message0(m)
}
}
impl TryFrom<Message> for Message0 {
type Error = UnexpectedMessage;
fn try_from(m: Message) -> Result<Self, Self::Error> {
match m {
Message::Message0(m) => Ok(m),
_ => Err(UnexpectedMessage {
expected_type: "Create0".to_string(),
received: m,
}),
}
}
}
impl From<Message1> for Message {
fn from(m: Message1) -> Self {
Message::Message1(m)
}
}
impl TryFrom<Message> for Message1 {
type Error = UnexpectedMessage;
fn try_from(m: Message) -> Result<Self, Self::Error> {
match m {
Message::Message1(m) => Ok(m),
_ => Err(UnexpectedMessage {
expected_type: "Create0".to_string(),
received: m,
}),
}
}
}
impl From<Message2> for Message {
fn from(m: Message2) -> Self {
Message::Message2(m)
}
}
impl TryFrom<Message> for Message2 {
type Error = UnexpectedMessage;
fn try_from(m: Message) -> Result<Self, Self::Error> {
match m {
Message::Message2(m) => Ok(m),
_ => Err(UnexpectedMessage {
expected_type: "Create0".to_string(),
received: m,
}),
}
}
}
impl From<Message3> for Message {
fn from(m: Message3) -> Self {
Message::Message3(m)
}
}
impl TryFrom<Message> for Message3 {
type Error = UnexpectedMessage;
fn try_from(m: Message) -> Result<Self, Self::Error> {
match m {
Message::Message3(m) => Ok(m),
_ => Err(UnexpectedMessage {
expected_type: "Create0".to_string(),
received: m,
}),
}
}
}
#[derive(Debug, thiserror::Error)]
#[error("expected message of type {expected_type}, got {received:?}")]
pub struct UnexpectedMessage {
expected_type: String,
received: Message,
}
impl UnexpectedMessage {
pub fn new<T>(received: Message) -> Self {
let expected_type = std::any::type_name::<T>();
Self {
expected_type: expected_type.to_string(),
received,
}
}
}
impl_try_from_parent_enum!(Message0, Message);
impl_try_from_parent_enum!(Message1, Message);
impl_try_from_parent_enum!(Message2, Message);
impl_try_from_parent_enum!(Message3, Message);
impl_from_child_enum!(Message0, Message);
impl_from_child_enum!(Message1, Message);
impl_from_child_enum!(Message2, Message);
impl_from_child_enum!(Message3, Message);

@ -14,6 +14,37 @@
#![forbid(unsafe_code)]
#![allow(non_snake_case)]
#[macro_use]
mod utils {
macro_rules! impl_try_from_parent_enum {
($type:ident, $parent:ident) => {
impl TryFrom<$parent> for $type {
type Error = anyhow::Error;
fn try_from(from: $parent) -> Result<Self> {
if let $parent::$type(inner) = from {
Ok(inner)
} else {
Err(anyhow::anyhow!(
"Failed to convert parent state to child state"
))
}
}
}
};
}
macro_rules! impl_from_child_enum {
($type:ident, $parent:ident) => {
impl From<$type> for $parent {
fn from(from: $type) -> Self {
$parent::$type(from)
}
}
};
}
}
pub mod alice;
pub mod bitcoin;
pub mod bob;

Loading…
Cancel
Save