2021-01-21 01:09:53 -05:00
|
|
|
pub use alice::Alice;
|
|
|
|
pub use bob::Bob;
|
2021-09-20 19:14:33 -04:00
|
|
|
pub use sqlite::SqliteDatabase;
|
2021-01-21 01:09:53 -05:00
|
|
|
|
2021-09-22 21:18:39 -04:00
|
|
|
use crate::fs::ensure_directory_exists;
|
|
|
|
use crate::protocol::{Database, State};
|
2021-09-22 02:14:49 -04:00
|
|
|
use anyhow::{bail, Result};
|
2021-03-03 19:28:58 -05:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use std::fmt::Display;
|
2021-09-22 21:18:39 -04:00
|
|
|
use std::path::Path;
|
|
|
|
use std::sync::Arc;
|
2020-11-03 00:44:04 -05:00
|
|
|
|
2021-01-07 19:04:32 -05:00
|
|
|
mod alice;
|
|
|
|
mod bob;
|
2021-09-20 19:14:33 -04:00
|
|
|
mod sqlite;
|
2021-01-04 22:08:36 -05:00
|
|
|
|
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
|
|
|
|
pub enum Swap {
|
|
|
|
Alice(Alice),
|
|
|
|
Bob(Bob),
|
|
|
|
}
|
2021-09-27 20:15:31 -04:00
|
|
|
|
|
|
|
impl From<State> for Swap {
|
|
|
|
fn from(state: State) -> Self {
|
|
|
|
match state {
|
|
|
|
State::Alice(state) => Swap::Alice(state.into()),
|
|
|
|
State::Bob(state) => Swap::Bob(state.into()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Swap> for State {
|
|
|
|
fn from(value: Swap) -> Self {
|
|
|
|
match value {
|
|
|
|
Swap::Alice(alice) => State::Alice(alice.into()),
|
|
|
|
Swap::Bob(bob) => State::Bob(bob.into()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-04 22:08:36 -05:00
|
|
|
|
|
|
|
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),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-12-22 23:26:08 -05:00
|
|
|
|
2021-03-26 00:16:19 -04:00
|
|
|
#[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;
|
|
|
|
|
2021-03-02 21:17:09 -05:00
|
|
|
impl Swap {
|
2021-03-26 00:16:19 -04:00
|
|
|
pub fn try_into_alice(self) -> Result<Alice> {
|
|
|
|
match self {
|
|
|
|
Swap::Alice(alice) => Ok(alice),
|
|
|
|
Swap::Bob(_) => bail!(NotAlice),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-02 21:17:09 -05:00
|
|
|
pub fn try_into_bob(self) -> Result<Bob> {
|
|
|
|
match self {
|
|
|
|
Swap::Bob(bob) => Ok(bob),
|
2021-03-26 00:16:19 -04:00
|
|
|
Swap::Alice(_) => bail!(NotBob),
|
2021-03-02 21:17:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-09-22 21:18:39 -04:00
|
|
|
|
2021-10-12 19:38:56 -04:00
|
|
|
pub async fn open_db(sqlite_path: impl AsRef<Path>) -> Result<Arc<dyn Database + Send + Sync>> {
|
|
|
|
if sqlite_path.as_ref().exists() {
|
|
|
|
tracing::debug!("Using existing sqlite database.");
|
|
|
|
let sqlite = SqliteDatabase::open(sqlite_path).await?;
|
|
|
|
Ok(Arc::new(sqlite))
|
|
|
|
} else {
|
|
|
|
tracing::debug!("Creating and using new sqlite database.");
|
|
|
|
ensure_directory_exists(sqlite_path.as_ref())?;
|
|
|
|
tokio::fs::File::create(&sqlite_path).await?;
|
|
|
|
let sqlite = SqliteDatabase::open(sqlite_path).await?;
|
|
|
|
Ok(Arc::new(sqlite))
|
2021-09-22 21:18:39 -04:00
|
|
|
}
|
|
|
|
}
|