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
50
51
52
53
54
55
56
57
use chrono::{DateTime, TimeZone};

pub trait ToMsgPackValue {
    fn to_msgpack_value(self) -> rmpv::Value;
}

impl<T> ToMsgPackValue for Option<T> where T: ToMsgPackValue {
    fn to_msgpack_value(self) -> rmpv::Value {
        match self {
            Some(v) => v.to_msgpack_value(),
            None => rmpv::Value::Nil,
        }
    }
}

impl<Tz> ToMsgPackValue for DateTime<Tz> where Tz: TimeZone {
    fn to_msgpack_value(self) -> rmpv::Value {
        self.timestamp().into()
    }
}

impl ToMsgPackValue for String {
    fn to_msgpack_value(self) -> rmpv::Value {
        rmpv::Value::String(self.into())
    }
}

impl ToMsgPackValue for &str {
    fn to_msgpack_value(self) -> rmpv::Value {
        rmpv::Value::String(self.into())
    }
}

macro_rules! to_msgpack_value_for_integer {
    ($type: ty) => {
        impl ToMsgPackValue for $type {
            fn to_msgpack_value(self) -> rmpv::Value {
                rmpv::Value::Integer(self.into())
            }
        }
        impl ToMsgPackValue for &$type {
            fn to_msgpack_value(self) -> rmpv::Value {
                rmpv::Value::Integer((*self).into())
            }
        }
    };
}

to_msgpack_value_for_integer!(u8);
to_msgpack_value_for_integer!(u16);
to_msgpack_value_for_integer!(u32);
to_msgpack_value_for_integer!(u64);

to_msgpack_value_for_integer!(i8);
to_msgpack_value_for_integer!(i16);
to_msgpack_value_for_integer!(i32);
to_msgpack_value_for_integer!(i64);