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
use std::io::Read;
use serde::{Deserialize, Serialize};
pub trait BinProtSerializationType<'de>: Sized {
type T: From<Self> + Into<Self> + Serialize + Deserialize<'de>;
fn try_from_binprot(rdr: impl Read) -> Result<Self, bin_prot::error::Error> {
let binprot: Self::T = bin_prot::from_reader_strict(rdr)?;
Ok(binprot.into())
}
fn try_into_binprot(self) -> Result<Vec<u8>, bin_prot::error::Error> {
let binprot: Self::T = self.into();
let mut bytes = Vec::new();
bin_prot::to_writer(&mut bytes, &binprot)?;
Ok(bytes)
}
}
pub trait JsonSerializationType<'de>: Sized {
type T: From<Self> + Into<Self> + Serialize + Deserialize<'de>;
fn try_from_json(s: &'de str) -> Result<Self, serde_json::error::Error> {
let json: Self::T = serde_json::from_str(s)?;
Ok(json.into())
}
fn try_into_json(self) -> Result<String, serde_json::error::Error> {
let json: Self::T = self.into();
serde_json::to_string(&json)
}
}