Skip to main content

like_a_clockwork/transport/
mod.rs

1pub mod binary;
2pub mod json;
3pub mod text;
4
5use std::collections::HashMap;
6
7pub trait HeaderMap {
8    fn get(&self, key: &str) -> Option<&str>;
9    fn set(&mut self, key: String, value: String);
10    fn keys(&self) -> Vec<String>;
11}
12
13pub trait BinaryHeaderMap {
14    fn get(&self, key: &str) -> Option<&[u8]>;
15    fn set(&mut self, key: String, value: Vec<u8>);
16    fn keys(&self) -> Vec<String>;
17}
18
19impl HeaderMap for HashMap<String, String> {
20    fn get(&self, key: &str) -> Option<&str> {
21        HashMap::get(self, key).map(|v| v.as_str())
22    }
23
24    fn set(&mut self, key: String, value: String) {
25        self.insert(key, value);
26    }
27
28    fn keys(&self) -> Vec<String> {
29        self.keys().cloned().collect()
30    }
31}
32
33impl BinaryHeaderMap for HashMap<String, Vec<u8>> {
34    fn get(&self, key: &str) -> Option<&[u8]> {
35        HashMap::get(self, key).map(|v| v.as_slice())
36    }
37
38    fn set(&mut self, key: String, value: Vec<u8>) {
39        self.insert(key, value);
40    }
41
42    fn keys(&self) -> Vec<String> {
43        self.keys().cloned().collect()
44    }
45}
46
47#[derive(Debug, thiserror::Error)]
48pub enum TransportError {
49    #[error("malformed value for key '{key}': {reason}")]
50    MalformedValue { key: String, reason: String },
51
52    #[error("serialization error: {0}")]
53    SerializationError(String),
54
55    #[error("deserialization error: {0}")]
56    DeserializationError(String),
57
58    #[error("missing required key: {0}")]
59    MissingKey(String),
60}
61
62#[cfg(test)]
63mod tests {
64    use super::*;
65
66    #[test]
67    fn hashmap_header_map_set_and_get() {
68        let mut map = HashMap::<String, String>::new();
69        map.set("X-Key".to_string(), "value".to_string());
70        assert_eq!(HeaderMap::get(&map, "X-Key"), Some("value"));
71    }
72
73    #[test]
74    fn hashmap_header_map_get_missing_returns_none() {
75        let map = HashMap::<String, String>::new();
76        assert_eq!(HeaderMap::get(&map, "missing"), None);
77    }
78
79    #[test]
80    fn hashmap_header_map_keys() {
81        let mut map = HashMap::<String, String>::new();
82        map.set("A".to_string(), "1".to_string());
83        map.set("B".to_string(), "2".to_string());
84        let mut keys = HeaderMap::keys(&map);
85        keys.sort();
86        assert_eq!(keys, vec!["A", "B"]);
87    }
88
89    #[test]
90    fn hashmap_binary_header_map_set_and_get() {
91        let mut map = HashMap::<String, Vec<u8>>::new();
92        map.set("key".to_string(), vec![1, 2, 3]);
93        assert_eq!(BinaryHeaderMap::get(&map, "key"), Some([1u8, 2, 3].as_slice()));
94    }
95
96    #[test]
97    fn hashmap_binary_header_map_get_missing_returns_none() {
98        let map = HashMap::<String, Vec<u8>>::new();
99        assert_eq!(BinaryHeaderMap::get(&map, "missing"), None);
100    }
101
102    #[test]
103    fn hashmap_binary_header_map_keys() {
104        let mut map = HashMap::<String, Vec<u8>>::new();
105        map.set("X".to_string(), vec![]);
106        map.set("Y".to_string(), vec![]);
107        let mut keys = BinaryHeaderMap::keys(&map);
108        keys.sort();
109        assert_eq!(keys, vec!["X", "Y"]);
110    }
111}