1use std::fmt::Debug;
2
3pub use bsint::BsInt;
4use rmp::{decode::RmpRead, encode::RmpWrite};
5
6mod bsint;
7
8#[derive(Clone, Debug)]
9pub enum Data {
10 Bool(bool),
11 F64(f64),
12 Int(BsInt),
13 F32(f32),
14 String(String),
15 BoolArray(Vec<bool>),
16 F64Array(Vec<f64>),
17 IntArray(Vec<BsInt>),
18 F32Array(Vec<f32>),
19 StringArray(Vec<String>),
20}
21impl Data {
22 pub fn from<R: RmpRead>(rd: &mut R, data_type: u8) -> Result<Self, ()> {
23 Ok(match data_type {
24 <bool as DataWrap>::MSGPCK => Self::Bool(<bool as DataWrap>::decode(rd)?),
25 <f64 as DataWrap>::MSGPCK => Self::F64(<f64 as DataWrap>::decode(rd)?),
26 <BsInt as DataWrap>::MSGPCK => Self::Int(<BsInt as DataWrap>::decode(rd)?),
27 <f32 as DataWrap>::MSGPCK => Self::F32(<f32 as DataWrap>::decode(rd)?),
28 <String as DataWrap>::MSGPCK => Self::String(<String as DataWrap>::decode(rd)?),
29 <Vec<bool> as DataWrap>::MSGPCK => {
30 Self::BoolArray(<Vec<bool> as DataWrap>::decode(rd)?)
31 }
32 <Vec<f64> as DataWrap>::MSGPCK => Self::F64Array(<Vec<f64> as DataWrap>::decode(rd)?),
33 <Vec<BsInt> as DataWrap>::MSGPCK => Self::IntArray(<Vec<BsInt> as DataWrap>::decode(rd)?),
34 <Vec<f32> as DataWrap>::MSGPCK => Self::F32Array(<Vec<f32> as DataWrap>::decode(rd)?),
35 <Vec<String> as DataWrap>::MSGPCK => {
36 Self::StringArray(<Vec<String> as DataWrap>::decode(rd)?)
37 }
38 _ => return Err(()),
39 })
40 }
41}
42
43pub trait DataWrap: Sized + Debug {
44 const MSGPCK: u8;
45 const STRING: &'static str;
46
47 fn decode<R: RmpRead>(rd: &mut R) -> Result<Self, ()>;
48 fn encode<W: RmpWrite>(wr: &mut W, val: Self) -> Result<(), ()>;
49}
50impl<T: DataType + Debug> DataWrap for T {
51 const MSGPCK: u8 = Self::DATATYPE_MSGPCK;
52 const STRING: &'static str = Self::DATATYPE_STRING;
53
54 fn decode<R: RmpRead>(rd: &mut R) -> Result<Self, ()> {
55 Self::decode(rd)
56 }
57 fn encode<W: RmpWrite>(wr: &mut W, val: Self) -> Result<(), ()> {
58 Self::encode(wr, val)
59 }
60}
61impl<T: DataType + Debug> DataWrap for Vec<T> {
62 const MSGPCK: u8 = T::ARRAYDATATYPE_MSGPCK;
63 const STRING: &'static str = T::ARRAYDATATYPE_STRING;
64
65 fn decode<R: RmpRead>(rd: &mut R) -> Result<Self, ()> {
66 T::decode_array(rd)
67 }
68 fn encode<W: RmpWrite>(wr: &mut W, val: Self) -> Result<(), ()> {
69 T::encode_array(wr, val)
70 }
71}
72
73pub trait DataType: Sized {
74 const DATATYPE_MSGPCK: u8;
75 const ARRAYDATATYPE_MSGPCK: u8;
76 const DATATYPE_STRING: &'static str;
77 const ARRAYDATATYPE_STRING: &'static str;
78
79 fn decode<R: RmpRead>(rd: &mut R) -> Result<Self, ()>;
80 fn encode<W: RmpWrite>(wr: &mut W, val: Self) -> Result<(), ()>;
81
82 fn decode_array<R: RmpRead>(rd: &mut R) -> Result<Vec<Self>, ()> {
83 let mut buf = Vec::new();
84
85 let len = rmp::decode::read_array_len(rd).map_err(|_| ())?;
86
87 for _ in 0..len {
88 buf.push(Self::decode(rd)?);
89 }
90
91 Ok(buf)
92 }
93
94 fn encode_array<W: RmpWrite>(wr: &mut W, vals: Vec<Self>) -> Result<(), ()> {
95 rmp::encode::write_array_len(wr, vals.len() as u32).map_err(|_| ())?;
96
97 for val in vals {
98 Self::encode(wr, val)?;
99 }
100
101 Ok(())
102 }
103}
104
105impl DataType for bool {
106 const DATATYPE_MSGPCK: u8 = 0;
107 const ARRAYDATATYPE_MSGPCK: u8 = 16;
108 const DATATYPE_STRING: &'static str = "boolean";
109 const ARRAYDATATYPE_STRING: &'static str = "boolean[]";
110
111 fn decode<R: RmpRead>(rd: &mut R) -> Result<Self, ()> {
112 let ret = rmp::decode::read_bool(rd).map_err(|_| ())?;
113
114 Ok(ret)
115 }
116
117 fn encode<W: RmpWrite>(wr: &mut W, val: Self) -> Result<(), ()> {
118 rmp::encode::write_bool(wr, val).map_err(|_| ())?;
119
120 Ok(())
121 }
122}
123impl DataType for f64 {
124 const DATATYPE_MSGPCK: u8 = 1;
125 const ARRAYDATATYPE_MSGPCK: u8 = 17;
126 const DATATYPE_STRING: &'static str = "double";
127 const ARRAYDATATYPE_STRING: &'static str = "double[]";
128
129 fn decode<R: RmpRead>(rd: &mut R) -> Result<Self, ()> {
130 let ret = rmp::decode::read_f64(rd).map_err(|_| ())?;
131
132 Ok(ret)
133 }
134
135 fn encode<W: RmpWrite>(wr: &mut W, val: Self) -> Result<(), ()> {
136 rmp::encode::write_f64(wr, val).map_err(|_| ())?;
137
138 Ok(())
139 }
140}
141
142
143impl DataType for f32 {
144 const DATATYPE_MSGPCK: u8 = 3;
145 const ARRAYDATATYPE_MSGPCK: u8 = 19;
146 const DATATYPE_STRING: &'static str = "float";
147 const ARRAYDATATYPE_STRING: &'static str = "float[]";
148
149 fn decode<R: RmpRead>(rd: &mut R) -> Result<Self, ()> {
150 let ret = rmp::decode::read_f32(rd).map_err(|_| ())?;
151
152 Ok(ret)
153 }
154
155 fn encode<W: RmpWrite>(wr: &mut W, val: Self) -> Result<(), ()> {
156 rmp::encode::write_f32(wr, val).map_err(|_| ())?;
157
158 Ok::<(), _>(())
159 }
160}
161
162impl DataType for String {
163 const DATATYPE_MSGPCK: u8 = 4;
164 const ARRAYDATATYPE_MSGPCK: u8 = 20;
165 const DATATYPE_STRING: &'static str = "string";
166 const ARRAYDATATYPE_STRING: &'static str = "string[]";
167
168 fn decode<R: RmpRead>(rd: &mut R) -> Result<Self, ()> {
169 let mut buf = Vec::new();
170
171 let ret = rmp::decode::read_str(rd, &mut buf).map_err(|_| ())?;
172
173 Ok(ret.to_string())
174 }
175
176 fn encode<W: RmpWrite>(wr: &mut W, val: Self) -> Result<(), ()> {
177 rmp::encode::write_str(wr, &val).map_err(|_| ())?;
178
179 Ok(())
180 }
181}