flow_record_common/
to_msgpack_value.rs1use chrono::{DateTime, TimeZone};
2
3use crate::FieldType;
4
5pub trait ToMsgPackValue {
6 fn to_msgpack_value(self) -> rmpv::Value;
7 fn field_type() -> FieldType;
8}
9
10impl ToMsgPackValue for bool {
11 fn to_msgpack_value(self) -> rmpv::Value {
12 rmpv::Value::Boolean(self)
13 }
14
15 fn field_type() -> FieldType {
16 FieldType::Boolean
17 }
18}
19
20impl<T> ToMsgPackValue for Option<T> where T: ToMsgPackValue {
21 fn to_msgpack_value(self) -> rmpv::Value {
22 match self {
23 Some(v) => v.to_msgpack_value(),
24 None => rmpv::Value::Nil,
25 }
26 }
27
28 fn field_type() -> FieldType {
29 T::field_type()
30 }
31}
32
33impl<Tz> ToMsgPackValue for DateTime<Tz> where Tz: TimeZone {
34 fn to_msgpack_value(self) -> rmpv::Value {
35 self.timestamp().into()
36 }
37
38 fn field_type() -> FieldType {
39 FieldType::Datetime
40 }
41}
42
43impl<Tz> ToMsgPackValue for &DateTime<Tz> where Tz: TimeZone {
44 fn to_msgpack_value(self) -> rmpv::Value {
45 self.timestamp().into()
46 }
47
48 fn field_type() -> FieldType {
49 FieldType::Datetime
50 }
51}
52
53impl ToMsgPackValue for String {
54 fn to_msgpack_value(self) -> rmpv::Value {
55 rmpv::Value::String(self.into())
56 }
57
58 fn field_type() -> FieldType {
59 FieldType::String
60 }
61}
62
63impl ToMsgPackValue for &str {
64 fn to_msgpack_value(self) -> rmpv::Value {
65 rmpv::Value::String(self.into())
66 }
67
68 fn field_type() -> FieldType {
69 FieldType::String
70 }
71}
72
73impl ToMsgPackValue for Vec<u8> {
74 fn to_msgpack_value(self) -> rmpv::Value {
75 rmpv::Value::Binary(self)
76 }
77
78 fn field_type() -> FieldType {
79 FieldType::Bin
80 }
81}
82
83macro_rules! to_msgpack_value_for {
84 ($dst: expr, $type: ty, $field_type: expr) => {
85 impl ToMsgPackValue for $type {
86 fn to_msgpack_value(self) -> rmpv::Value {
87 $dst(self.into())
88 }
89
90 fn field_type() -> FieldType {
91 $field_type
92 }
93 }
94 impl ToMsgPackValue for &$type {
95 fn to_msgpack_value(self) -> rmpv::Value {
96 $dst((*self).into())
97 }
98
99 fn field_type() -> FieldType {
100 $field_type
101 }
102 }
103 };
104}
105
106macro_rules! to_msgpack_value_for_integer {
107 ($type: ty, $field_type: expr) => {to_msgpack_value_for!(rmpv::Value::Integer, $type, $field_type);}
108}
109
110to_msgpack_value_for_integer!(u8, FieldType::UInt16);
111to_msgpack_value_for_integer!(u16, FieldType::UInt16);
112to_msgpack_value_for_integer!(u32, FieldType::UInt32);
113to_msgpack_value_for_integer!(u64, FieldType::VarInt);
114to_msgpack_value_for_integer!(usize, FieldType::VarInt);
115
116to_msgpack_value_for_integer!(i8, FieldType::VarInt);
117to_msgpack_value_for_integer!(i16, FieldType::VarInt);
118to_msgpack_value_for_integer!(i32, FieldType::VarInt);
119to_msgpack_value_for_integer!(i64, FieldType::VarInt);
120
121to_msgpack_value_for!(rmpv::Value::F32, f32, FieldType::Float);
122to_msgpack_value_for!(rmpv::Value::F64, f64, FieldType::Float);