iota_json_rpc/
routing_layer.rs1use std::collections::{HashMap, HashSet};
6
7use iota_open_rpc::MethodRouting;
8
9#[derive(Debug, Clone)]
10pub struct RpcRouter {
11 routes: HashMap<String, MethodRouting>,
12 route_to_methods: HashSet<String>,
13 disable_routing: bool,
14}
15
16impl RpcRouter {
17 pub fn new(routes: HashMap<String, MethodRouting>, disable_routing: bool) -> Self {
18 let route_to_methods = routes.values().map(|v| v.route_to.clone()).collect();
19
20 Self {
21 routes,
22 route_to_methods,
23 disable_routing,
24 }
25 }
26
27 pub fn route<'c, 'a: 'c, 'b: 'c>(&'a self, method: &'b str, version: Option<&str>) -> &'c str {
28 if self.route_to_methods.contains(method) {
30 "INVALID_ROUTING"
31 } else if self.disable_routing {
32 method
33 } else {
34 match (version, self.routes.get(method)) {
36 (Some(v), Some(route)) if route.matches(v) => route.route_to.as_str(),
37 _ => method,
38 }
39 }
40 }
41}