xmr-btc-swap/swap/src/database.rs

63 lines
1.3 KiB
Rust
Raw Normal View History

pub use self::sled::SledDatabase;
pub use alice::Alice;
pub use bob::Bob;
use anyhow::{bail, Result};
use serde::{Deserialize, Serialize};
use std::fmt::Display;
2020-11-03 00:44:04 -05:00
2021-01-07 19:04:32 -05:00
mod alice;
mod bob;
mod sled;
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
pub enum Swap {
Alice(Alice),
Bob(Bob),
}
impl From<Alice> for Swap {
fn from(from: Alice) -> Self {
Swap::Alice(from)
}
}
impl From<Bob> for Swap {
fn from(from: Bob) -> Self {
Swap::Bob(from)
}
}
impl Display for Swap {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Swap::Alice(alice) => Display::fmt(alice, f),
Swap::Bob(bob) => Display::fmt(bob, f),
}
}
}
#[derive(thiserror::Error, Debug, Clone, Copy, PartialEq)]
#[error("Not in the role of Alice")]
struct NotAlice;
#[derive(thiserror::Error, Debug, Clone, Copy, PartialEq)]
#[error("Not in the role of Bob")]
struct NotBob;
impl Swap {
pub fn try_into_alice(self) -> Result<Alice> {
match self {
Swap::Alice(alice) => Ok(alice),
Swap::Bob(_) => bail!(NotAlice),
}
}
pub fn try_into_bob(self) -> Result<Bob> {
match self {
Swap::Bob(bob) => Ok(bob),
Swap::Alice(_) => bail!(NotBob),
}
}
}