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;
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(); 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)
}