-
Notifications
You must be signed in to change notification settings - Fork 933
/
helpers.rs
40 lines (32 loc) · 858 Bytes
/
helpers.rs
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
use prost::{
bytes::{Bytes, BytesMut},
DecodeError, Message,
};
use prost_types::Any;
use tonic::Code;
use crate::pb;
pub(super) trait IntoAny {
fn into_any(self) -> Any;
}
#[allow(dead_code)]
pub(super) trait FromAny {
fn from_any(any: Any) -> Result<Self, DecodeError>
where
Self: Sized;
}
pub(super) trait FromAnyRef {
fn from_any_ref(any: &Any) -> Result<Self, DecodeError>
where
Self: Sized;
}
pub(super) fn gen_details_bytes(code: Code, message: &str, details: Vec<Any>) -> Bytes {
let status = pb::Status {
code: code as i32,
message: message.to_owned(),
details,
};
let mut buf = BytesMut::with_capacity(status.encoded_len());
// Should never panic since `buf` is initialized with sufficient capacity
status.encode(&mut buf).unwrap();
buf.freeze()
}