1pub use prost::{self, DecodeError, EncodeError, Message};
2pub use prost_types::{Any, Timestamp};
3
4use super::{Error, NamedMessage};
5
6pub trait ProstTimestampExt {
7 fn to_chrono_datetime(&self) -> chrono::DateTime<chrono::Utc>;
8
9 fn to_timestamp_nanos(&self) -> u64 {
10 self.to_chrono_datetime()
11 .timestamp_nanos_opt()
12 .unwrap_or_default() as u64
13 }
14}
15
16impl ProstTimestampExt for Timestamp {
17 fn to_chrono_datetime(&self) -> chrono::DateTime<chrono::Utc> {
18 crate::time::timestamp_parts_to_datetime(self.seconds, self.nanos)
19 }
20}
21
22pub trait ProstDateTimeExt {
23 fn to_proto_timestamp(&self) -> Timestamp;
24}
25
26impl<Tz: chrono::TimeZone> ProstDateTimeExt for chrono::DateTime<Tz> {
27 fn to_proto_timestamp(&self) -> Timestamp {
28 Timestamp {
29 seconds: self.timestamp(),
30 nanos: self.timestamp_subsec_nanos() as i32,
31 }
32 }
33}
34
35pub trait ProstAnyPackMessageExt {
36 fn pack_to_any(&self) -> Result<Any, Error>;
37
38 fn pack_to_stepan_any(&self) -> Result<protobuf::well_known_types::any::Any, Error>;
39}
40
41impl<M> ProstAnyPackMessageExt for M
42where
43 M: Message + NamedMessage,
44{
45 fn pack_to_any(&self) -> Result<Any, Error> {
46 let mut buf = Vec::new();
47 self.encode(&mut buf)?;
48
49 Ok(Any {
50 type_url: format!("type.googleapis.com/{}", M::full_name()),
51 value: buf,
52 })
53 }
54
55 fn pack_to_stepan_any(&self) -> Result<protobuf::well_known_types::any::Any, Error> {
56 let any = self.pack_to_any()?;
57
58 Ok(protobuf::well_known_types::any::Any {
59 type_url: any.type_url,
60 value: any.value,
61 ..Default::default()
62 })
63 }
64}