whisky_common/data/
primitives.rs

1use serde_json::{json, Value};
2
3use super::{con_str0, con_str1};
4
5pub fn bool(b: bool) -> Value {
6    if b {
7        con_str1(json!([]))
8    } else {
9        con_str0(json!([]))
10    }
11}
12
13pub fn byte_string(bytes: &str) -> Value {
14    json!({ "bytes": bytes })
15}
16
17pub fn builtin_byte_string(bytes: &str) -> Value {
18    json!({ "bytes": bytes })
19}
20
21pub fn integer(int: i64) -> Value {
22    json!({ "int": int })
23}
24
25pub fn list<T: Into<Value>>(p_list: Vec<T>) -> Value {
26    let list: Vec<Value> = p_list.into_iter().map(|item| item.into()).collect();
27    json!({ "list": list })
28}
29
30pub fn assoc_map<K: Into<Value>, V: Into<Value>>(items_map: Vec<(K, V)>) -> Value {
31    let map: Vec<Value> = items_map
32        .into_iter()
33        .map(|(k, v)| json!({"k": k.into(), "v": v.into()}))
34        .collect();
35    json!({ "map": map })
36}
37
38pub fn tuple<K: Into<Value>, V: Into<Value>>(key: K, value: V) -> Value {
39    con_str0(vec![key.into(), value.into()])
40}
41
42pub fn pairs<K: Into<Value>, V: Into<Value>>(items_map: Vec<(K, V)>) -> Value {
43    let map: Vec<Value> = items_map
44        .into_iter()
45        .map(|(k, v)| json!({"k": k.into(), "v": v.into()}))
46        .collect();
47    json!({ "map": map })
48}