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