krpc_core/support/
triple.rs1use bytes::BufMut;
2use prost::Message;
3
4#[allow(clippy::derive_partial_eq_without_eq)]
5#[derive(Clone, PartialEq, ::prost::Message)]
6pub struct TripleRequestWrapper {
7 #[prost(string, tag = "1")]
10 pub serialize_type: ::prost::alloc::string::String,
11 #[prost(bytes = "vec", repeated, tag = "2")]
12 pub args: ::prost::alloc::vec::Vec<::prost::alloc::vec::Vec<u8>>,
13 #[prost(string, repeated, tag = "3")]
14 pub arg_types: ::prost::alloc::vec::Vec<::prost::alloc::string::String>,
15}
16#[allow(clippy::derive_partial_eq_without_eq)]
17#[derive(Clone, PartialEq, ::prost::Message)]
18pub struct TripleResponseWrapper {
19 #[prost(string, tag = "1")]
20 pub serialize_type: ::prost::alloc::string::String,
21 #[prost(bytes = "vec", tag = "2")]
22 pub data: ::prost::alloc::vec::Vec<u8>,
23 #[prost(string, tag = "3")]
24 pub r#type: ::prost::alloc::string::String,
25}
26#[allow(clippy::derive_partial_eq_without_eq)]
27#[derive(Clone, PartialEq, ::prost::Message)]
28pub struct TripleExceptionWrapper {
29 #[prost(string, tag = "1")]
30 pub language: ::prost::alloc::string::String,
31 #[prost(string, tag = "2")]
32 pub serialization: ::prost::alloc::string::String,
33 #[prost(string, tag = "3")]
34 pub class_name: ::prost::alloc::string::String,
35 #[prost(bytes = "vec", tag = "4")]
36 pub data: ::prost::alloc::vec::Vec<u8>,
37}
38
39
40impl TripleRequestWrapper {
41 pub fn get_buf(strs: Vec<String>) -> Vec<u8> {
42 let mut trip = TripleRequestWrapper::default();
43 trip.serialize_type = "fastjson".to_string();
44 trip.args = vec![];
45 for str in strs {
46 trip.args.push(str.as_bytes().to_vec());
47 }
48 return get_buf(trip.encode_to_vec());
49 }
50 pub fn get_req(self)-> Vec<String> {
51 let mut res = vec![];
52 for str in self.args {
53 res.push(String::from_utf8(str).unwrap());
54 }
55 return res;
56 }
57}
58
59impl TripleResponseWrapper {
60 pub fn get_buf(strs: String) -> Vec<u8> {
61 let mut trip = TripleResponseWrapper::default();
62 trip.serialize_type = "fastjson".to_string();
63 trip.data = strs.as_bytes().to_vec();
64 return get_buf(trip.encode_to_vec());
65 }
66 pub fn is_empty_body(&self) -> bool {
67 return self.data.starts_with("null".as_bytes());
68 }
69}
70
71impl TripleExceptionWrapper {
72 pub fn get_buf(strs: String) -> Vec<u8> {
73 let mut trip = TripleExceptionWrapper::default();
74 trip.serialization = "fastjson".to_string();
75 trip.data = strs.as_bytes().to_vec();
76 return get_buf(trip.encode_to_vec());
77 }
78 pub fn get_err_info(&self) -> String {
79 return serde_json::from_slice(&self.data[..]).unwrap_or("rpc error".to_owned());
80 }
81}
82
83
84fn get_buf(data : Vec<u8>) -> Vec<u8>{
85 let mut len = data.len();
86 let mut u8_array = [0 as u8;4];
87 for idx in 0..4 {
88 u8_array[idx] = len as u8 | 0;
89 len >>= 8;
90 }
91 let mut buf = bytes::BytesMut::default();
92 buf.put_u8(0);
93 for item in u8_array.iter().rev() {
94 buf.put_u8(*item);
95 }
96 buf.put_slice(&data);
97 return buf.to_vec();
98}