cfx_rpc_primitives/
rpc_module.rs

1//! Types for the `rpc` API.
2use serde::{Deserialize, Serialize};
3use std::collections::HashMap;
4
5/// Represents the `rpc_modules` response, which returns the
6/// list of all available modules on that transport and their version
7#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
8#[serde(transparent)]
9pub struct RpcModules {
10    module_map: HashMap<String, String>,
11}
12
13impl RpcModules {
14    /// Create a new instance of `RPCModules`
15    pub const fn new(module_map: HashMap<String, String>) -> Self {
16        Self { module_map }
17    }
18
19    /// Consumes self and returns the inner hashmap mapping module names to
20    /// their versions
21    pub fn into_modules(self) -> HashMap<String, String> { self.module_map }
22}
23
24#[cfg(test)]
25mod tests {
26    use super::*;
27    #[test]
28    fn test_parse_module_versions_roundtrip() {
29        let s = r#"{"txpool":"1.0","trace":"1.0","eth":"1.0","web3":"1.0","net":"1.0"}"#;
30        let module_map = HashMap::from([
31            ("txpool".to_owned(), "1.0".to_owned()),
32            ("trace".to_owned(), "1.0".to_owned()),
33            ("eth".to_owned(), "1.0".to_owned()),
34            ("web3".to_owned(), "1.0".to_owned()),
35            ("net".to_owned(), "1.0".to_owned()),
36        ]);
37        let m = RpcModules::new(module_map);
38        let de_serialized: RpcModules = serde_json::from_str(s).unwrap();
39        assert_eq!(de_serialized, m);
40    }
41}