epee_serde/value/
mod.rs

1mod de;
2mod ser;
3
4use std::collections::HashMap;
5
6use serde::de::Unexpected;
7
8macro_rules! get_type {
9    ($fn:ident, $ty:ty, $variant:ident) => {
10        pub fn $fn(&self) -> Option<&$ty> {
11            match self {
12                Value::$variant(val) => Some(val),
13                _ => None,
14            }
15        }
16    };
17}
18
19macro_rules! is_type {
20    ($fn:ident, $variant:ident) => {
21        pub fn $fn(&self) -> bool {
22            match self {
23                Value::$variant(_) => true,
24                _ => false,
25            }
26        }
27    };
28}
29
30#[derive(Debug)]
31pub enum Value {
32    I64(i64),
33    I32(i32),
34    I16(i16),
35    I8(i8),
36    U64(u64),
37    U32(u32),
38    U16(u16),
39    U8(u8),
40    F64(f64),
41    String(String),
42    Bytes(Vec<u8>),
43    Bool(bool),
44    Object(HashMap<String, Value>),
45    Seq(Vec<Value>),
46}
47
48impl Value {
49    pub fn get(&self, key: &str) -> Option<&Value> {
50        match self {
51            Value::Object(obj) => obj.get(key),
52            _ => None,
53        }
54    }
55
56    pub fn get_mut(&mut self, key: &str) -> Option<&mut Value> {
57        match self {
58            Value::Object(obj) => obj.get_mut(key),
59            _ => None,
60        }
61    }
62
63    pub fn get_value_type_as_unexpected(&self) -> Unexpected {
64        match self {
65            Value::I64(v) => Unexpected::Signed(*v),
66            Value::I32(v) => Unexpected::Signed(*v as i64),
67            Value::I16(v) => Unexpected::Signed(*v as i64),
68            Value::I8(v) => Unexpected::Signed(*v as i64),
69            Value::U64(v) => Unexpected::Unsigned(*v),
70            Value::U32(v) => Unexpected::Unsigned(*v as u64),
71            Value::U16(v) => Unexpected::Unsigned(*v as u64),
72            Value::U8(v) => Unexpected::Unsigned(*v as u64),
73            Value::F64(v) => Unexpected::Float(*v),
74            Value::String(v) => Unexpected::Str(v),
75            Value::Bytes(v) => Unexpected::Bytes(v),
76            Value::Bool(v) => Unexpected::Bool(*v),
77            Value::Object(_) => Unexpected::Map,
78            Value::Seq(_) => Unexpected::Seq,
79        }
80    }
81
82    get_type!(get_i64, i64, I64);
83    get_type!(get_i32, i32, I32);
84    get_type!(get_i16, i16, I16);
85    get_type!(get_i8, i8, I8);
86    get_type!(get_u64, u64, U64);
87    get_type!(get_u32, u32, U32);
88    get_type!(get_u16, u16, U16);
89    get_type!(get_u8, u8, U8);
90    get_type!(get_f64, f64, F64);
91    get_type!(get_string, String, String);
92    get_type!(get_bytes, Vec<u8>, Bytes);
93    get_type!(get_bool, bool, Bool);
94    get_type!(get_raw_hashmap, HashMap<String, Value>, Object);
95    get_type!(get_seq, Vec<Value>, Seq);
96
97    is_type!(is_i64, I64);
98    is_type!(is_i32, I32);
99    is_type!(is_i16, I16);
100    is_type!(is_i8, I8);
101    is_type!(is_u64, U64);
102    is_type!(is_u32, U32);
103    is_type!(is_u16, U16);
104    is_type!(is_u8, U8);
105    is_type!(is_f64, F64);
106    is_type!(is_string, String);
107    is_type!(is_bytes, Bytes);
108    is_type!(is_bool, Bool);
109    is_type!(is_object, Object);
110    is_type!(is_seq, Seq);
111}