xds_api/
value.rs

1use crate::generated::google::protobuf;
2
3macro_rules! wrapper_type {
4    ($proto_ty:ty, $primitive_ty:ty) => {
5        impl From<$primitive_ty> for $proto_ty {
6            fn from(value: $primitive_ty) -> Self {
7                Self { value }
8            }
9        }
10
11        impl From<$proto_ty> for $primitive_ty {
12            fn from(value: $proto_ty) -> Self {
13                value.value
14            }
15        }
16    };
17}
18
19wrapper_type!(protobuf::UInt64Value, u64);
20wrapper_type!(protobuf::UInt32Value, u32);
21wrapper_type!(protobuf::Int32Value, i32);
22wrapper_type!(protobuf::Int64Value, i64);
23wrapper_type!(protobuf::FloatValue, f32);
24wrapper_type!(protobuf::DoubleValue, f64);
25wrapper_type!(protobuf::BytesValue, Vec<u8>);
26wrapper_type!(protobuf::BoolValue, bool);
27wrapper_type!(protobuf::StringValue, String);
28
29impl TryFrom<std::time::Duration> for protobuf::Duration {
30    type Error = std::num::TryFromIntError;
31
32    fn try_from(value: std::time::Duration) -> Result<Self, Self::Error> {
33        let seconds: i64 = value.as_secs().try_into()?;
34        let nanos: i32 = value.subsec_nanos().try_into()?;
35        Ok(protobuf::Duration { seconds, nanos })
36    }
37}
38
39impl TryFrom<protobuf::Duration> for std::time::Duration {
40    type Error = std::num::TryFromIntError;
41
42    fn try_from(value: protobuf::Duration) -> Result<Self, Self::Error> {
43        let seconds: u64 = value.seconds.try_into()?;
44        let nanos: u32 = value.nanos.try_into()?;
45        Ok(std::time::Duration::new(seconds, nanos))
46    }
47}