parity-zcash/pzec/rpc_apis.rs

65 lines
1.7 KiB
Rust
Raw Normal View History

2016-12-07 05:14:52 -08:00
use std::str::FromStr;
use std::collections::HashSet;
2016-12-07 11:39:12 -08:00
use rpc::Dependencies;
use ethcore_rpc::MetaIoHandler;
2016-12-07 05:14:52 -08:00
#[derive(Debug, PartialEq, Eq, Hash, Copy, Clone)]
pub enum Api {
2016-12-12 10:49:01 -08:00
/// Raw methods
2016-12-07 05:14:52 -08:00
Raw,
2016-12-12 10:49:01 -08:00
/// Miner-related methods
2016-12-09 05:43:00 -08:00
Miner,
2016-12-12 10:49:01 -08:00
/// BlockChain-related methods
BlockChain,
2016-12-12 10:18:43 -08:00
/// Network
Network,
2016-12-07 05:14:52 -08:00
}
#[derive(Debug, PartialEq, Eq)]
pub enum ApiSet {
List(HashSet<Api>),
}
impl Default for ApiSet {
fn default() -> Self {
ApiSet::List(vec![Api::Raw, Api::Miner, Api::BlockChain, Api::Network].into_iter().collect())
2016-12-07 05:14:52 -08:00
}
}
impl FromStr for Api {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"raw" => Ok(Api::Raw),
2019-01-15 01:16:27 -08:00
"miner" => Ok(Api::Miner),
2016-12-12 10:49:01 -08:00
"blockchain" => Ok(Api::BlockChain),
2016-12-12 10:18:43 -08:00
"network" => Ok(Api::Network),
2016-12-07 05:14:52 -08:00
api => Err(format!("Unknown api: {}", api)),
}
}
}
impl ApiSet {
pub fn list_apis(&self) -> HashSet<Api> {
match *self {
ApiSet::List(ref apis) => apis.clone(),
}
}
}
pub fn setup_rpc(mut handler: MetaIoHandler<()>, apis: ApiSet, deps: Dependencies) -> MetaIoHandler<()> {
2016-12-07 05:14:52 -08:00
use ethcore_rpc::v1::*;
for api in apis.list_apis() {
match api {
Api::Raw => handler.extend_with(RawClient::new(RawClientCore::new(deps.local_sync_node.clone())).to_delegate()),
2019-01-15 01:16:27 -08:00
Api::Miner => handler.extend_with(MinerClient::new(MinerClientCore::new(deps.local_sync_node.clone(), deps.miner_address.clone())).to_delegate()),
2019-01-14 00:32:32 -08:00
Api::BlockChain => handler.extend_with(BlockChainClient::new(BlockChainClientCore::new(deps.consensus.clone(), deps.storage.clone())).to_delegate()),
Api::Network => handler.extend_with(NetworkClient::new(NetworkClientCore::new(deps.p2p_context.clone())).to_delegate()),
2016-12-07 05:14:52 -08:00
}
}
handler
2016-12-07 05:14:52 -08:00
}