Skip to main content

rs_jsonmaps2protomaps/
lib.rs

1use std::io;
2
3use std::collections::HashMap;
4
5use io::BufRead;
6use io::BufWriter;
7use io::Write;
8
9use protobuf::Message;
10
11use protobuf::well_known_types::struct_::ListValue;
12use protobuf::well_known_types::struct_::NullValue;
13use protobuf::well_known_types::struct_::Struct;
14use protobuf::well_known_types::struct_::Value;
15
16pub fn null2val() -> Value {
17    let mut ret: Value = Value::default();
18    ret.set_null_value(NullValue::default());
19    ret
20}
21
22pub fn f2value(f: f64) -> Value {
23    let mut ret: Value = Value::default();
24    ret.set_number_value(f);
25    ret
26}
27
28pub fn s2value(s: String) -> Value {
29    let mut ret: Value = Value::default();
30    ret.set_string_value(s);
31    ret
32}
33
34pub fn b2value(b: bool) -> Value {
35    let mut ret: Value = Value::default();
36    ret.set_bool_value(b);
37    ret
38}
39
40pub fn struct2value(s: Struct) -> Value {
41    let mut ret: Value = Value::default();
42    ret.set_struct_value(s);
43    ret
44}
45
46pub fn l2value(l: ListValue) -> Value {
47    let mut ret: Value = Value::default();
48    ret.set_list_value(l);
49    ret
50}
51
52pub fn m2struct(m: HashMap<String, Value>) -> Struct {
53    Struct {
54        fields: m,
55        ..Default::default()
56    }
57}
58
59pub fn jnum2value(jnum: serde_json::Number) -> Option<Value> {
60    let of: Option<f64> = jnum.as_f64();
61    let f: f64 = of?;
62    Some(f2value(f))
63}
64
65pub fn jval2value(jval: serde_json::Value) -> Option<Value> {
66    match jval {
67        serde_json::Value::Null => Some(null2val()),
68        serde_json::Value::Bool(b) => Some(b2value(b)),
69        serde_json::Value::Number(n) => jnum2value(n),
70        serde_json::Value::String(s) => Some(s2value(s)),
71        serde_json::Value::Array(arr) => {
72            let mut list = ListValue::default();
73            for item in arr {
74                let oval: Option<Value> = jval2value(item);
75                let val: Value = oval?;
76                list.values.push(val);
77            }
78            Some(l2value(list))
79        }
80        serde_json::Value::Object(obj) => {
81            let mut map = HashMap::new();
82            for (k, v) in obj {
83                let oval: Option<Value> = jval2value(v);
84                let val: Value = oval?;
85                map.insert(k, val);
86            }
87            Some(struct2value(m2struct(map)))
88        }
89    }
90}
91
92pub fn jmap2struct(jmap: serde_json::Map<String, serde_json::Value>) -> Option<Struct> {
93    let mut fields = HashMap::new();
94    for (k, v) in jmap {
95        let oproto_val: Option<Value> = jval2value(v);
96        let proto_val: Value = oproto_val?;
97        fields.insert(k, proto_val);
98    }
99    Some(m2struct(fields))
100}
101
102pub fn reader2jsonmaps2structs2writer<R, W>(rdr: R, mut wtr: W) -> Result<(), io::Error>
103where
104    R: BufRead,
105    W: Write,
106{
107    let deser = serde_json::de::Deserializer::from_reader(rdr);
108    let maps = deser.into_iter();
109    let mapd = maps.map(|r: Result<serde_json::Map<String, serde_json::Value>, _>| {
110        r.map_err(io::Error::other)
111    });
112    let converted = mapd.map(|r| {
113        r.and_then(|jm| {
114            let ostr: Option<Struct> = jmap2struct(jm);
115            ostr.ok_or(io::Error::other("unable to convert the json map"))
116        })
117    });
118    for rstr in converted {
119        let pstr: Struct = rstr?;
120        pstr.write_length_delimited_to_writer(&mut wtr)?;
121    }
122    wtr.flush()
123}
124
125pub fn stdin2jsonmaps2structs2stdout() -> Result<(), io::Error> {
126    let o = io::stdout();
127    let mut ol = o.lock();
128    reader2jsonmaps2structs2writer(io::stdin().lock(), BufWriter::new(&mut ol))?;
129    ol.flush()
130}