tenderdash_proto/protobuf.rs
1// Google protobuf Timestamp and Duration types reimplemented because their
2// comments are turned into invalid documentation texts and doctest chokes on them. See https://github.com/danburkert/prost/issues/374
3// Prost does not seem to have a way yet to remove documentations defined in
4// protobuf files. These structs are defined in gogoproto v1.3.1 at https://github.com/gogo/protobuf/tree/v1.3.1/protobuf/google/protobuf
5
6#[cfg(not(feature = "std"))]
7use core::fmt;
8#[cfg(feature = "std")]
9use std::fmt;
10
11/// A Timestamp represents a point in time independent of any time zone or local
12/// calendar, encoded as a count of seconds and fractions of seconds at
13/// nanosecond resolution. The count is relative to an epoch at UTC midnight on
14/// January 1, 1970, in the proleptic Gregorian calendar which extends the
15/// Gregorian calendar backwards to year one.
16///
17/// All minutes are 60 seconds long. Leap seconds are "smeared" so that no leap
18/// second table is needed for interpretation, using a [24-hour linear
19/// smear](https://developers.google.com/time/smear).
20///
21/// The range is from 0001-01-01T00:00:00Z to 9999-12-31T23:59:59.999999999Z. By
22/// restricting to that range, we ensure that we can convert to and from [RFC
23/// 3339](https://www.ietf.org/rfc/rfc3339.txt) date strings.
24#[derive(Clone, Copy, PartialEq, Eq, Hash, ::prost::Message)]
25#[cfg_attr(feature = "serde", derive(::serde::Serialize, ::serde::Deserialize))]
26#[cfg_attr(
27 feature = "serde",
28 serde(
29 from = "crate::serializers::timestamp::Rfc3339",
30 into = "crate::serializers::timestamp::Rfc3339"
31 )
32)]
33pub struct Timestamp {
34 /// Represents seconds of UTC time since Unix epoch
35 /// 1970-01-01T00:00:00Z. Must be from 0001-01-01T00:00:00Z to
36 /// 9999-12-31T23:59:59Z inclusive.
37 #[prost(int64, tag = "1")]
38 pub seconds: i64,
39 /// Non-negative fractions of a second at nanosecond resolution. Negative
40 /// second values with fractions must still have non-negative nanos values
41 /// that count forward in time. Must be from 0 to 999,999,999
42 /// inclusive.
43 #[prost(int32, tag = "2")]
44 pub nanos: i32,
45}
46
47/// A Duration represents a signed, fixed-length span of time represented
48/// as a count of seconds and fractions of seconds at nanosecond
49/// resolution. It is independent of any calendar and concepts like "day"
50/// or "month". It is related to Timestamp in that the difference between
51/// two Timestamp values is a Duration and it can be added or subtracted
52/// from a Timestamp. Range is approximately +-10,000 years.
53#[derive(Clone, Copy, PartialEq, Eq, Hash, ::prost::Message)]
54pub struct Duration {
55 /// Signed seconds of the span of time. Must be from -315,576,000,000
56 /// to +315,576,000,000 inclusive. Note: these bounds are computed from:
57 /// 60 sec/min * 60 min/hr * 24 hr/day * 365.25 days/year * 10000 years
58 #[prost(int64, tag = "1")]
59 pub seconds: i64,
60 /// Signed fractions of a second at nanosecond resolution of the span
61 /// of time. Durations less than one second are represented with a 0
62 /// `seconds` field and a positive or negative `nanos` field. For durations
63 /// of one second or more, a non-zero value for the `nanos` field must be
64 /// of the same sign as the `seconds` field. Must be from -999,999,999
65 /// to +999,999,999 inclusive.
66 #[prost(int32, tag = "2")]
67 pub nanos: i32,
68}
69#[cfg(feature = "serde")]
70impl serde::Serialize for Duration {
71 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
72 where
73 S: serde::ser::Serializer,
74 {
75 let total_nanos = self.seconds * 1_000_000_000 + self.nanos as i64;
76 serializer.serialize_i64(total_nanos)
77 }
78}
79#[cfg(feature = "serde")]
80struct DurationVisitor;
81#[cfg(feature = "serde")]
82impl<'de> serde::de::Visitor<'de> for DurationVisitor {
83 type Value = Duration;
84
85 fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
86 formatter.write_str("a nanosecond representation of a duration")
87 }
88
89 fn visit_i128<E>(self, value: i128) -> Result<Duration, E>
90 where
91 E: serde::de::Error,
92 {
93 let seconds = (value / 1_000_000_000) as i64;
94 let nanos = (value % 1_000_000_000) as i32;
95 Ok(Duration { seconds, nanos })
96 }
97
98 fn visit_str<E>(self, value: &str) -> Result<Duration, E>
99 where
100 E: serde::de::Error,
101 {
102 let value = value.parse::<i128>().map_err(E::custom)?;
103 self.visit_i128(value)
104 }
105}
106#[cfg(feature = "serde")]
107impl<'de> serde::Deserialize<'de> for Duration {
108 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
109 where
110 D: serde::de::Deserializer<'de>,
111 {
112 deserializer.deserialize_str(DurationVisitor)
113 }
114}