xCommonLib/serial/
mod.rs

1use std::collections::HashMap;
2
3use protobuf::{CodedInputStream, CodedOutputStream};
4
5use crate::base::status::Status;
6
7pub mod api_descriptor;
8pub mod json_to_proto;
9pub mod proto_to_json;
10pub mod request_message;
11
12// api 描述
13
14pub fn serial_string(content: &str) -> Vec<u8> {
15    let buffer_size = ::protobuf::rt::string_size(1, content) as usize;
16    let mut buffer: Vec<u8> = Vec::with_capacity(buffer_size);
17    let mut os = CodedOutputStream::vec(&mut buffer);
18    os.write_string(1, content).unwrap();
19    os.flush().unwrap();
20    drop(os);
21    buffer
22}
23
24pub fn read_string(proto_bytes: &[u8]) -> String {
25    let mut input_stream = CodedInputStream::from_bytes(proto_bytes);
26    let _tag = input_stream.read_raw_tag_or_eof().unwrap();
27    input_stream.read_string().unwrap()
28}
29
30pub fn read_bool(proto_bytes: &[u8]) -> bool {
31    let mut input_stream = CodedInputStream::from_bytes(proto_bytes);
32    let _tag = input_stream.read_raw_tag_or_eof().unwrap();
33    let result = input_stream.read_bool().unwrap();
34    result
35}
36
37pub fn read_sfixed64(proto_bytes: &[u8]) -> i64 {
38    let mut input_stream = CodedInputStream::from_bytes(proto_bytes);
39    let result = input_stream.read_sfixed64().unwrap();
40    result
41}
42
43pub fn read_fixed64(proto_bytes: &[u8]) -> u64 {
44  let mut input_stream = CodedInputStream::from_bytes(proto_bytes);
45  let result = input_stream.read_fixed64().unwrap();
46  result
47}
48
49pub fn read_bytes(proto_bytes: &[u8]) -> Vec<u8> {
50    let mut input_stream = CodedInputStream::from_bytes(proto_bytes);
51    let _tag = input_stream.read_raw_tag_or_eof().unwrap();
52    input_stream.read_bytes().unwrap()
53}
54
55pub fn serial_db_colums(size: u64, columns: &HashMap<String, u32>, os: &mut CodedOutputStream<'_>) {
56    os.write_uint64_no_tag(size).unwrap();
57    for (k, v) in columns {
58        let mut entry_size = 0;
59        entry_size += ::protobuf::rt::string_size(1, &k);
60        entry_size += ::protobuf::rt::uint32_size(2, *v);
61        os.write_raw_varint32(10).unwrap(); // Tag.
62        os.write_raw_varint32(entry_size as u32).unwrap();
63        os.write_string(1, &k).unwrap();
64        os.write_uint32(2, *v).unwrap();
65    }
66}
67
68pub fn read_db_colums(
69    is: &mut ::protobuf::CodedInputStream<'_>,
70) -> Result<HashMap<u32, String>, Status> {
71    let mut columns: HashMap<u32, String> = HashMap::new();
72    //
73    let size = is.read_uint64().unwrap();
74    let old_limit = is.push_limit(size).unwrap();
75    while let Some(tag) = is.read_raw_tag_or_eof().unwrap() {
76        match tag {
77            10 => {
78                let len = is.read_raw_varint32().unwrap();
79                let old_limit = is.push_limit(len as u64).unwrap();
80                let mut key = ::std::default::Default::default();
81                let mut value = ::std::default::Default::default();
82                while let Some(tag) = is.read_raw_tag_or_eof().unwrap() {
83                    match tag {
84                        10 => key = is.read_string().unwrap(),
85                        16 => value = is.read_uint32().unwrap(),
86                        _ => ::protobuf::rt::skip_field_for_tag(tag, is).unwrap(),
87                    };
88                }
89                is.pop_limit(old_limit);
90                columns.insert(value, key);
91            }
92            _ => {}
93        };
94    }
95    is.pop_limit(old_limit);
96    Ok(columns)
97}