antelope/serializer/
formatter.rs

1use serde_json::Value;
2
3use crate::{api::v1::structs::EncodingError, util::hex_to_bytes};
4
5pub struct ValueTo {}
6
7impl ValueTo {
8    pub fn str(v: Option<&Value>) -> Result<&str, EncodingError> {
9        check_some(v, "str")?;
10        let value = v.unwrap();
11        if !value.is_string() {
12            return Err(EncodingError::new("Value is not String".into()));
13        }
14
15        Ok(value.as_str().unwrap())
16    }
17
18    pub fn string(v: Option<&Value>) -> Result<String, EncodingError> {
19        check_some(v, "String")?;
20        let value = v.unwrap();
21        if !value.is_string() {
22            return Err(EncodingError::new("Value is not String".into()));
23        }
24
25        Ok(value.as_str().unwrap().to_string())
26    }
27
28    pub fn bool(v: Option<&Value>) -> Result<bool, EncodingError> {
29        check_some(v, "bool")?;
30        let value = v.unwrap();
31        if !value.is_boolean() {
32            return Err(EncodingError::new("Value is not bool".into()));
33        }
34
35        Ok(value.as_bool().unwrap())
36    }
37
38    pub fn vec(v: Option<&Value>) -> Result<&Vec<Value>, EncodingError> {
39        check_some(v, "Vec")?;
40        let value = v.unwrap();
41        if !value.is_array() {
42            return Err(EncodingError::new("Value is not Vec".into()));
43        }
44
45        Ok(value.as_array().unwrap())
46    }
47
48    pub fn hex_bytes(v: Option<&Value>) -> Result<Vec<u8>, EncodingError> {
49        let value = Self::string(v)?;
50        return Ok(hex_to_bytes(value.as_str()));
51    }
52
53    pub fn u32(v: Option<&Value>) -> Result<u32, EncodingError> {
54        check_some(v, "u32")?;
55        let value = v.unwrap();
56        if !value.is_number() {
57            return Err(EncodingError::new("Value is not a number".into()));
58        }
59
60        Ok(value.as_number().unwrap().as_u64().unwrap() as u32)
61    }
62
63    pub fn u64(v: Option<&Value>) -> Result<u64, EncodingError> {
64        check_some(v, "u64")?;
65        let value = v.unwrap();
66        if !value.is_number() {
67            return Err(EncodingError::new("Value is not a number".into()));
68        }
69
70        Ok(value.as_number().unwrap().as_u64().unwrap())
71    }
72
73    pub fn json_object(v: Option<&Value>) -> Result<JSONObject, EncodingError> {
74        check_some(v, "JSON object")?;
75        let value = v.unwrap();
76        if !value.is_object() {
77            return Err(EncodingError::new("Value is not a JSON object".into()));
78        }
79
80        Ok(JSONObject::new(value.clone()))
81    }
82}
83
84#[derive(Debug)]
85pub struct JSONObject {
86    value: Value,
87}
88
89impl JSONObject {
90    pub fn new(value: Value) -> Self {
91        JSONObject { value }
92    }
93
94    pub fn has(&self, property: &str) -> bool {
95        self.value.get(property).is_some()
96    }
97
98    pub fn get_value(&self, property: &str) -> Result<Value, EncodingError> {
99        let value = self.value.get(property);
100        if value.is_none() {
101            return Err(EncodingError::new(format!(
102                "Unable to get property {}",
103                property
104            )));
105        }
106
107        Ok(value.unwrap().clone())
108    }
109
110    pub fn get_optional_string(&self, property: &str) -> Result<Option<String>, EncodingError> {
111        match self.value.get(property) {
112            Some(v) => match v.as_str() {
113                Some(s) => Ok(Some(s.to_string())),
114                None => Ok(None),
115            },
116            None => Ok(None),
117        }
118    }
119
120    pub fn get_optional_u32(&self, property: &str) -> Result<Option<u32>, EncodingError> {
121        match self.value.get(property) {
122            Some(v) => match v.as_u64() {
123                Some(n) => Ok(Some(n as u32)),
124                None => Ok(None),
125            },
126            None => Ok(None),
127        }
128    }
129
130    pub fn get_str(&self, property: &str) -> Result<&str, EncodingError> {
131        ValueTo::str(self.value.get(property))
132    }
133
134    pub fn get_string(&self, property: &str) -> Result<String, EncodingError> {
135        ValueTo::string(self.value.get(property))
136    }
137
138    pub fn get_bool(&self, property: &str) -> Result<bool, EncodingError> {
139        ValueTo::bool(self.value.get(property))
140    }
141
142    pub fn get_vec(&self, property: &str) -> Result<&Vec<Value>, EncodingError> {
143        ValueTo::vec(self.value.get(property))
144    }
145
146    pub fn get_hex_bytes(&self, property: &str) -> Result<Vec<u8>, EncodingError> {
147        ValueTo::hex_bytes(self.value.get(property))
148    }
149
150    pub fn get_u32(&self, property: &str) -> Result<u32, EncodingError> {
151        ValueTo::u32(self.value.get(property))
152    }
153
154    pub fn get_u64(&self, property: &str) -> Result<u64, EncodingError> {
155        ValueTo::u64(self.value.get(property))
156    }
157
158    pub fn get_json_object(&self, property: &str) -> Result<JSONObject, EncodingError> {
159        ValueTo::json_object(self.value.get(property))
160    }
161}
162
163pub fn check_some(o: Option<&Value>, type_name: &str) -> Result<String, EncodingError> {
164    if o.is_none() {
165        return Err(EncodingError::new(format!(
166            "Value is None, cannot convert to {}",
167            type_name
168        )));
169    }
170
171    Ok(String::from(""))
172}