vantage_table/
cbor_ext.rs1use ciborium::Value as CborValue;
5
6pub trait CborValueExt {
8 fn as_str(&self) -> Option<&str>;
10
11 fn as_i64(&self) -> Option<i64>;
14
15 fn as_u64(&self) -> Option<u64>;
18
19 fn as_f64(&self) -> Option<f64>;
21
22 fn get(&self, key: &str) -> Option<&CborValue>;
25
26 fn get_mut(&mut self, key: &str) -> Option<&mut CborValue>;
28}
29
30impl CborValueExt for CborValue {
31 fn as_str(&self) -> Option<&str> {
32 self.as_text()
33 }
34
35 fn as_i64(&self) -> Option<i64> {
36 self.as_integer().and_then(|i| i64::try_from(i).ok())
37 }
38
39 fn as_u64(&self) -> Option<u64> {
40 self.as_integer().and_then(|i| u64::try_from(i).ok())
41 }
42
43 fn as_f64(&self) -> Option<f64> {
44 self.as_float()
45 }
46
47 fn get(&self, key: &str) -> Option<&CborValue> {
48 self.as_map()?
49 .iter()
50 .find(|(k, _)| k.as_text() == Some(key))
51 .map(|(_, v)| v)
52 }
53
54 fn get_mut(&mut self, key: &str) -> Option<&mut CborValue> {
55 self.as_map_mut()?
56 .iter_mut()
57 .find(|(k, _)| k.as_text() == Some(key))
58 .map(|(_, v)| v)
59 }
60}