gmp/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
use anyhow::Result;
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use time_primitives::{IConnect, NetworkId};

pub use gmp_evm::sol::Gateway;

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum Backend {
	Evm,
	Grpc,
	Rust,
}

impl std::str::FromStr for Backend {
	type Err = anyhow::Error;

	fn from_str(backend: &str) -> Result<Self> {
		Ok(match backend {
			"evm" => Self::Evm,
			"grpc" => Self::Grpc,
			"rust" => Self::Rust,
			_ => anyhow::bail!("unsupported backend"),
		})
	}
}

impl std::fmt::Display for Backend {
	fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
		let backend = match self {
			Self::Evm => "evm",
			Self::Grpc => "grpc",
			Self::Rust => "rust",
		};
		f.write_str(backend)
	}
}

impl Backend {
	pub fn chain(self, network: NetworkId, mnemonic: &str) -> Result<Arc<dyn IConnect>> {
		Ok(match self {
			Self::Evm => Arc::new(gmp_evm::Chain::new(network, mnemonic)?),
			Self::Grpc => Arc::new(gmp_grpc::Chain::new(network, mnemonic)?),
			Self::Rust => Arc::new(gmp_rust::Chain::new(network, mnemonic)),
		})
	}
}