cfx_rpc_cfx_types/
apis.rs1use std::{
6 collections::HashSet,
7 fmt::{Display, Formatter},
8 str::FromStr,
9};
10
11#[derive(Debug, PartialEq, Clone, Eq, Hash)]
12pub enum Api {
13 Cfx,
14 Debug,
15 Pubsub,
16 Test,
17 Trace,
18 TxPool,
19 Pos,
20}
21
22impl FromStr for Api {
23 type Err = String;
24
25 fn from_str(s: &str) -> Result<Self, Self::Err> {
26 use self::Api::*;
27 match s {
28 "cfx" => Ok(Cfx),
29 "debug" => Ok(Debug),
30 "pubsub" => Ok(Pubsub),
31 "test" => Ok(Test),
32 "trace" => Ok(Trace),
33 "txpool" => Ok(TxPool),
34 "pos" => Ok(Pos),
35 _ => Err("Unknown api type".into()),
36 }
37 }
38}
39
40impl Display for Api {
41 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
42 match self {
43 Api::Cfx => write!(f, "cfx"),
44 Api::Debug => write!(f, "debug"),
45 Api::Pubsub => write!(f, "pubsub"),
46 Api::Test => write!(f, "test"),
47 Api::Trace => write!(f, "trace"),
48 Api::TxPool => write!(f, "txpool"),
49 Api::Pos => write!(f, "pos"),
50 }
51 }
52}
53
54#[derive(Debug, Clone, Eq, PartialEq)]
55pub enum ApiSet {
56 All, Safe,
58 List(HashSet<Api>),
59}
60
61impl ApiSet {
62 pub fn list_apis(&self) -> HashSet<Api> {
63 match *self {
64 ApiSet::List(ref apis) => apis.clone(),
65 ApiSet::All => [
66 Api::Cfx,
67 Api::Debug,
68 Api::Pubsub,
69 Api::Test,
70 Api::Trace,
71 Api::Pos,
72 Api::TxPool,
73 ]
74 .iter()
75 .cloned()
76 .collect(),
77 ApiSet::Safe => [Api::Cfx, Api::Pubsub, Api::TxPool]
78 .iter()
79 .cloned()
80 .collect(),
81 }
82 }
83}
84
85impl Default for ApiSet {
86 fn default() -> Self { ApiSet::Safe }
87}
88
89impl FromStr for ApiSet {
90 type Err = String;
91
92 fn from_str(s: &str) -> Result<Self, Self::Err> {
93 let mut apis = HashSet::new();
94
95 for api in s.split(',') {
96 match api {
97 "all" => {
98 apis.extend(ApiSet::All.list_apis());
99 }
100 "safe" => {
101 apis.extend(ApiSet::Safe.list_apis());
103 }
104 api if api.starts_with("-") => {
106 let api = api[1..].parse()?;
107 apis.remove(&api);
108 }
109 api => {
110 let api = api.parse()?;
111 apis.insert(api);
112 }
113 }
114 }
115
116 Ok(ApiSet::List(apis))
117 }
118}