1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
use std::collections::HashMap;

use protobuf::{CodedInputStream, CodedOutputStream};

use crate::base::status::Status;

pub mod api_descriptor;
pub mod json_to_proto;
pub mod proto_to_json;
pub mod request_message;

// api 描述

pub fn serial_string(content: &str) -> Vec<u8> {
    let buffer_size = ::protobuf::rt::string_size(1, content) as usize;
    let mut buffer: Vec<u8> = Vec::with_capacity(buffer_size);
    let mut os = CodedOutputStream::vec(&mut buffer);
    os.write_string(1, content).unwrap();
    os.flush().unwrap();
    drop(os);
    buffer
}

pub fn read_string(proto_bytes: &[u8]) -> String {
    let mut input_stream = CodedInputStream::from_bytes(proto_bytes);
    let _tag = input_stream.read_raw_tag_or_eof().unwrap();
     input_stream.read_string().unwrap()

}

pub fn read_bool(proto_bytes: &[u8]) -> bool {
    let mut input_stream = CodedInputStream::from_bytes(proto_bytes);
    let _tag = input_stream.read_raw_tag_or_eof().unwrap();
    let result = input_stream.read_bool().unwrap();
    result
}


pub fn read_sfixed64(proto_bytes: &[u8]) -> i64 {
  let mut input_stream = CodedInputStream::from_bytes(proto_bytes);
  let result = input_stream.read_sfixed64().unwrap();
  result
}


pub fn serial_db_colums(size: u64, columns: &HashMap<String, u32>, os: &mut CodedOutputStream<'_>) {
    os.write_uint64_no_tag(size).unwrap();
    for (k, v) in columns {
        let mut entry_size = 0;
        entry_size += ::protobuf::rt::string_size(1, &k);
        entry_size += ::protobuf::rt::uint32_size(2, *v);
        os.write_raw_varint32(10).unwrap(); // Tag.
        os.write_raw_varint32(entry_size as u32).unwrap();
        os.write_string(1, &k).unwrap();
        os.write_uint32(2, *v).unwrap();
    }
}

pub fn read_db_colums(
    is: &mut ::protobuf::CodedInputStream<'_>,
) -> Result<HashMap<u32, String>, Status> {
    let mut columns: HashMap<u32, String> = HashMap::new();
    //
    let size = is.read_uint64().unwrap();
    let old_limit = is.push_limit(size).unwrap();
    while let Some(tag) = is.read_raw_tag_or_eof().unwrap() {
        match tag {
            10 => {
                let len = is.read_raw_varint32().unwrap();
                let old_limit = is.push_limit(len as u64).unwrap();
                let mut key = ::std::default::Default::default();
                let mut value = ::std::default::Default::default();
                while let Some(tag) = is.read_raw_tag_or_eof().unwrap() {
                    match tag {
                        10 => key = is.read_string().unwrap(),
                        16 => value = is.read_uint32().unwrap(),
                        _ => ::protobuf::rt::skip_field_for_tag(tag, is).unwrap(),
                    };
                }
                is.pop_limit(old_limit);
                columns.insert(value, key);
            }
            _ => {}
        };
    }
    is.pop_limit(old_limit);
    Ok(columns)
}