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