hyperstack_interpreter/
proto_router.rs1use prost_types::Any;
2use serde_json::Value;
3use std::collections::HashMap;
4
5pub type ProtoDecoder = fn(&[u8]) -> Result<(Value, String), Box<dyn std::error::Error>>;
6
7#[derive(Debug)]
8pub struct ProtoRouter {
9 decoders: HashMap<String, ProtoDecoder>,
10}
11
12impl ProtoRouter {
13 pub fn new() -> Self {
14 ProtoRouter {
15 decoders: HashMap::new(),
16 }
17 }
18
19 pub fn register(&mut self, type_url: String, decoder: ProtoDecoder) {
20 self.decoders.insert(type_url, decoder);
21 }
22
23 pub fn decode(&self, any: Any) -> Result<(Value, String), Box<dyn std::error::Error>> {
24 let decoder = self
25 .decoders
26 .get(&any.type_url)
27 .ok_or_else(|| format!("No decoder found for type_url: {}", any.type_url))?;
28
29 decoder(&any.value)
30 }
31}
32
33impl Default for ProtoRouter {
34 fn default() -> Self {
35 Self::new()
36 }
37}