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
//! to value
//!

use crate::proto::transport::{Value, Value_ValueType};
/// ToValue is implemented for all rust types that can be converted to protobuf values
pub trait ToValue {
    /// outputs the appropriate protobuf record value
    fn to_value(self) -> Value;
}

macro_rules! impl_value {
    ($ty:ident, $field:ident, $value_type:ident) => {
        impl ToValue for $ty {
            fn to_value(self) -> Value {
                let mut v = Value::new();
                v.$field(self);
                v.set_value_type(Value_ValueType::$value_type);
                v
            }
        }
    };
}

// special case
impl ToValue for usize {
    fn to_value(self) -> Value {
        let mut v = Value::new();
        v.int64 = self as i64;
        v.set_value_type(Value_ValueType::INT64);
        v
    }
}

impl ToValue for Vec<u8> {
    fn to_value(self) -> Value {
        let mut v = Value::new();
        v.bytes = self;
        v.set_value_type(Value_ValueType::BYTES);
        v
    }
}

impl ToValue for &str {
    fn to_value(self) -> Value {
        let mut v = Value::new();
        v.string = self.to_string();
        v.set_value_type(Value_ValueType::STRING);
        v
    }
}

impl_value!(f64, set_double, DOUBLE);
impl_value!(f32, set_float, FLOAT);
impl_value!(i32, set_int32, INT32);
impl_value!(bool, set_bool, BOOL);
impl_value!(String, set_string, STRING);