bilrost_types/types.rs
1use alloc::collections::BTreeMap;
2use alloc::string::String;
3use alloc::vec::Vec;
4use bilrost::{Message, Oneof};
5
6/// A Duration represents a signed, fixed-length span of time represented as a count of seconds and
7/// fractions of seconds at nanosecond resolution. It is independent of any calendar and concepts
8/// like "day" or "month". It is related to Timestamp in that the difference between two Timestamp
9/// values is a Duration.
10///
11/// Values of this type are not guaranteed to only exist in their normalized form.
12///
13/// # Examples
14///
15/// Example 1: Compute Duration from two Timestamps in pseudo code.
16///
17/// ```text
18/// Timestamp start = ...;
19/// Timestamp end = ...;
20/// Duration duration = ...;
21///
22/// duration.seconds = end.seconds - start.seconds;
23/// duration.nanos = end.nanos - start.nanos;
24///
25/// if (duration.seconds < 0 && duration.nanos > 0) {
26/// duration.seconds += 1;
27/// duration.nanos -= 1000000000;
28/// } else if (duration.seconds > 0 && duration.nanos < 0) {
29/// duration.seconds -= 1;
30/// duration.nanos += 1000000000;
31/// }
32/// ```
33///
34/// Example 2: Compute Timestamp from Timestamp + Duration in pseudo code.
35///
36/// ```text
37/// Timestamp start = ...;
38/// Duration duration = ...;
39/// Timestamp end = ...;
40///
41/// end.seconds = start.seconds + duration.seconds;
42/// end.nanos = start.nanos + duration.nanos;
43///
44/// if (end.nanos < 0) {
45/// end.seconds -= 1;
46/// end.nanos += 1000000000;
47/// } else if (end.nanos >= 1000000000) {
48/// end.seconds += 1;
49/// end.nanos -= 1000000000;
50/// }
51/// ```
52#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Message)]
53#[bilrost(distinguished)]
54pub struct Duration {
55 /// Signed seconds of the span of time. Must be from -315,576,000,000 to +315,576,000,000
56 /// inclusive. Note: these bounds are computed from: 60 sec/min * 60 min/hr * 24 hr/day *
57 /// 365.25 days/year * 10000 years
58 #[bilrost(1)]
59 pub seconds: i64,
60 /// Signed fractions of a second at nanosecond resolution of the span of time. Durations less
61 /// than one second are represented with a 0 `seconds` field and a positive or negative `nanos`
62 /// field. For durations of one second or more, a non-zero value for the `nanos` field must be
63 /// of the same sign as the `seconds` field. Must be from -999,999,999 to +999,999,999
64 /// inclusive.
65 #[bilrost(tag = 2, encoding = "fixed")]
66 pub nanos: i32,
67}
68
69/// A Timestamp represents a point in time independent of any time zone or local calendar, encoded
70/// as a count of seconds and fractions of seconds at nanosecond resolution. The count is relative
71/// to an epoch at UTC midnight on January 1, 1970, in the proleptic Gregorian calendar which
72/// extends the Gregorian calendar backwards indefinitely.
73///
74/// All minutes are 60 seconds long. Leap seconds are "smeared" so that no leap second table is
75/// needed for interpretation, using a [24-hour linear smear](
76/// <https://developers.google.com/time/smear>).
77///
78/// The range from 0001-01-01T00:00:00Z to 9999-12-31T23:59:59.999999999Z may be converted to and
79/// from [RFC 3339](<https://www.ietf.org/rfc/rfc3339.txt>) date strings via the `Display` and
80/// `FromStr` traits. Dates before and after those years are extended further, still in the
81/// proleptic Gregorian calendar, in negative years or in positive years with more than 4 digits.
82///
83/// Values of this type are not guaranteed to only exist in their normalized form.
84#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Message)]
85#[bilrost(distinguished)]
86pub struct Timestamp {
87 /// Represents seconds of UTC time since Unix epoch 1970-01-01T00:00:00Z.
88 #[bilrost(1)]
89 pub seconds: i64,
90 /// Non-negative fractions of a second at nanosecond resolution. Negative second values with
91 /// fractions must still have non-negative nanos values that count forward in time. Must be from
92 /// 0 to 999,999,999 inclusive.
93 #[bilrost(tag = 2, encoding = "fixed")]
94 pub nanos: i32,
95}
96
97impl Timestamp {
98 pub const MIN: Self = Timestamp {
99 seconds: i64::MIN,
100 nanos: 0,
101 };
102 pub const MAX: Self = Timestamp {
103 seconds: i64::MAX,
104 nanos: 999999999,
105 };
106}
107
108/// `Value` represents a dynamically typed JSON value which can be either null, a number (signed,
109/// unsigned, or floating point in 64 bits), a string, a boolean, a string-keyed associative map of
110/// other values, or a list of values.
111#[derive(Clone, Debug, PartialEq, Oneof, Message)]
112pub enum Value {
113 /// Represents a JSON null value.
114 Null,
115 #[bilrost(1)]
116 Float(f64),
117 #[bilrost(2)]
118 Signed(i64),
119 #[bilrost(3)]
120 Unsigned(u64),
121 #[bilrost(4)]
122 String(String),
123 #[bilrost(5)]
124 Bool(bool),
125 /// Represents a structured value.
126 #[bilrost(6)]
127 Struct(StructValue),
128 /// Represents a repeated `Value`.
129 #[bilrost(7)]
130 List(ListValue),
131}
132
133/// `StructValue` represents a structured data value analogous to a JSON object value.
134#[derive(Clone, Debug, PartialEq, Message)]
135pub struct StructValue {
136 /// Unordered map of dynamically typed values.
137 #[bilrost(tag = 1, recurses)]
138 pub fields: BTreeMap<String, Value>,
139}
140
141/// `ListValue` is a wrapper around a repeated list of values, analogous to a JSON list value.
142#[derive(Clone, Debug, PartialEq, Message)]
143pub struct ListValue {
144 /// Repeated field of dynamically typed values.
145 #[bilrost(tag = 1, encoding = "packed", recurses)]
146 pub values: Vec<Value>,
147}