Skip to main content

buffa_types/generated/
google.protobuf.duration.rs

1// @generated by buffa-codegen. DO NOT EDIT.
2// source: google/protobuf/duration.proto
3
4/// A Duration represents a signed, fixed-length span of time represented
5/// as a count of seconds and fractions of seconds at nanosecond
6/// resolution. It is independent of any calendar and concepts like "day"
7/// or "month". It is related to Timestamp in that the difference between
8/// two Timestamp values is a Duration and it can be added or subtracted
9/// from a Timestamp. Range is approximately +-10,000 years.
10///
11/// # Examples
12///
13/// Example 1: Compute Duration from two Timestamps in pseudo code.
14///
15/// ```text
16/// Timestamp start = ...;
17/// Timestamp end = ...;
18/// Duration duration = ...;
19///
20/// duration.seconds = end.seconds - start.seconds;
21/// duration.nanos = end.nanos - start.nanos;
22///
23/// if (duration.seconds < 0 && duration.nanos > 0) {
24///   duration.seconds += 1;
25///   duration.nanos -= 1000000000;
26/// } else if (duration.seconds > 0 && duration.nanos < 0) {
27///   duration.seconds -= 1;
28///   duration.nanos += 1000000000;
29/// }
30/// ```
31///
32/// Example 2: Compute Timestamp from Timestamp + Duration in pseudo code.
33///
34/// ```text
35/// Timestamp start = ...;
36/// Duration duration = ...;
37/// Timestamp end = ...;
38///
39/// end.seconds = start.seconds + duration.seconds;
40/// end.nanos = start.nanos + duration.nanos;
41///
42/// if (end.nanos < 0) {
43///   end.seconds -= 1;
44///   end.nanos += 1000000000;
45/// } else if (end.nanos >= 1000000000) {
46///   end.seconds += 1;
47///   end.nanos -= 1000000000;
48/// }
49/// ```
50///
51/// Example 3: Compute Duration from datetime.timedelta in Python.
52///
53/// ```text
54/// td = datetime.timedelta(days=3, minutes=10)
55/// duration = Duration()
56/// duration.FromTimedelta(td)
57/// ```
58///
59/// # JSON Mapping
60///
61/// In JSON format, the Duration type is encoded as a string rather than an
62/// object, where the string ends in the suffix "s" (indicating seconds) and
63/// is preceded by the number of seconds, with nanoseconds expressed as
64/// fractional seconds. For example, 3 seconds with 0 nanoseconds should be
65/// encoded in JSON format as "3s", while 3 seconds and 1 nanosecond should
66/// be expressed in JSON format as "3.000000001s", and 3 seconds and 1
67/// microsecond should be expressed in JSON format as "3.000001s".
68#[derive(Clone, PartialEq, Default)]
69#[cfg_attr(feature = "arbitrary", derive(::arbitrary::Arbitrary))]
70pub struct Duration {
71    /// Signed seconds of the span of time. Must be from -315,576,000,000
72    /// to +315,576,000,000 inclusive. Note: these bounds are computed from:
73    /// 60 sec/min * 60 min/hr * 24 hr/day * 365.25 days/year * 10000 years
74    ///
75    /// Field 1: `seconds`
76    pub seconds: i64,
77    /// Signed fractions of a second at nanosecond resolution of the span
78    /// of time. Durations less than one second are represented with a 0
79    /// `seconds` field and a positive or negative `nanos` field. For durations
80    /// of one second or more, a non-zero value for the `nanos` field must be
81    /// of the same sign as the `seconds` field. Must be from -999,999,999
82    /// to +999,999,999 inclusive.
83    ///
84    /// Field 2: `nanos`
85    pub nanos: i32,
86    #[doc(hidden)]
87    pub __buffa_unknown_fields: ::buffa::UnknownFields,
88}
89impl ::core::fmt::Debug for Duration {
90    fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
91        f.debug_struct("Duration")
92            .field("seconds", &self.seconds)
93            .field("nanos", &self.nanos)
94            .finish()
95    }
96}
97impl Duration {
98    /// Protobuf type URL for this message, for use with `Any::pack` and
99    /// `Any::unpack_if`.
100    ///
101    /// Format: `type.googleapis.com/<fully.qualified.TypeName>`
102    pub const TYPE_URL: &'static str = "type.googleapis.com/google.protobuf.Duration";
103}
104impl ::buffa::DefaultInstance for Duration {
105    fn default_instance() -> &'static Self {
106        static VALUE: ::buffa::__private::OnceBox<Duration> = ::buffa::__private::OnceBox::new();
107        VALUE.get_or_init(|| ::buffa::alloc::boxed::Box::new(Self::default()))
108    }
109}
110impl ::buffa::Message for Duration {
111    /// Returns the total encoded size in bytes.
112    ///
113    /// The result is a `u32`; the protobuf specification requires all
114    /// messages to fit within 2 GiB (2,147,483,647 bytes), so a
115    /// compliant message will never overflow this type.
116    #[allow(clippy::let_and_return)]
117    fn compute_size(&self, _cache: &mut ::buffa::SizeCache) -> u32 {
118        #[allow(unused_imports)]
119        use ::buffa::Enumeration as _;
120        let mut size = 0u32;
121        if self.seconds != 0i64 {
122            size += 1u32 + ::buffa::types::int64_encoded_len(self.seconds) as u32;
123        }
124        if self.nanos != 0i32 {
125            size += 1u32 + ::buffa::types::int32_encoded_len(self.nanos) as u32;
126        }
127        size += self.__buffa_unknown_fields.encoded_len() as u32;
128        size
129    }
130    fn write_to(
131        &self,
132        _cache: &mut ::buffa::SizeCache,
133        buf: &mut impl ::buffa::bytes::BufMut,
134    ) {
135        #[allow(unused_imports)]
136        use ::buffa::Enumeration as _;
137        if self.seconds != 0i64 {
138            ::buffa::encoding::Tag::new(1u32, ::buffa::encoding::WireType::Varint)
139                .encode(buf);
140            ::buffa::types::encode_int64(self.seconds, buf);
141        }
142        if self.nanos != 0i32 {
143            ::buffa::encoding::Tag::new(2u32, ::buffa::encoding::WireType::Varint)
144                .encode(buf);
145            ::buffa::types::encode_int32(self.nanos, buf);
146        }
147        self.__buffa_unknown_fields.write_to(buf);
148    }
149    fn merge_field(
150        &mut self,
151        tag: ::buffa::encoding::Tag,
152        buf: &mut impl ::buffa::bytes::Buf,
153        depth: u32,
154    ) -> ::core::result::Result<(), ::buffa::DecodeError> {
155        #[allow(unused_imports)]
156        use ::buffa::bytes::Buf as _;
157        #[allow(unused_imports)]
158        use ::buffa::Enumeration as _;
159        match tag.field_number() {
160            1u32 => {
161                if tag.wire_type() != ::buffa::encoding::WireType::Varint {
162                    return ::core::result::Result::Err(::buffa::DecodeError::WireTypeMismatch {
163                        field_number: 1u32,
164                        expected: 0u8,
165                        actual: tag.wire_type() as u8,
166                    });
167                }
168                self.seconds = ::buffa::types::decode_int64(buf)?;
169            }
170            2u32 => {
171                if tag.wire_type() != ::buffa::encoding::WireType::Varint {
172                    return ::core::result::Result::Err(::buffa::DecodeError::WireTypeMismatch {
173                        field_number: 2u32,
174                        expected: 0u8,
175                        actual: tag.wire_type() as u8,
176                    });
177                }
178                self.nanos = ::buffa::types::decode_int32(buf)?;
179            }
180            _ => {
181                self.__buffa_unknown_fields
182                    .push(::buffa::encoding::decode_unknown_field(tag, buf, depth)?);
183            }
184        }
185        ::core::result::Result::Ok(())
186    }
187    fn clear(&mut self) {
188        self.seconds = 0i64;
189        self.nanos = 0i32;
190        self.__buffa_unknown_fields.clear();
191    }
192}
193impl ::buffa::ExtensionSet for Duration {
194    const PROTO_FQN: &'static str = "google.protobuf.Duration";
195    fn unknown_fields(&self) -> &::buffa::UnknownFields {
196        &self.__buffa_unknown_fields
197    }
198    fn unknown_fields_mut(&mut self) -> &mut ::buffa::UnknownFields {
199        &mut self.__buffa_unknown_fields
200    }
201}
202impl ::buffa::text::TextFormat for Duration {
203    fn encode_text(
204        &self,
205        enc: &mut ::buffa::text::TextEncoder<'_>,
206    ) -> ::core::fmt::Result {
207        #[allow(unused_imports)]
208        use ::buffa::Enumeration as _;
209        if self.seconds != 0i64 {
210            enc.write_field_name("seconds")?;
211            enc.write_i64(self.seconds)?;
212        }
213        if self.nanos != 0i32 {
214            enc.write_field_name("nanos")?;
215            enc.write_i32(self.nanos)?;
216        }
217        enc.write_unknown_fields(&self.__buffa_unknown_fields)?;
218        ::core::result::Result::Ok(())
219    }
220    fn merge_text(
221        &mut self,
222        dec: &mut ::buffa::text::TextDecoder<'_>,
223    ) -> ::core::result::Result<(), ::buffa::text::ParseError> {
224        #[allow(unused_imports)]
225        use ::buffa::Enumeration as _;
226        while let ::core::option::Option::Some(__name) = dec.read_field_name()? {
227            match __name {
228                "seconds" => self.seconds = dec.read_i64()?,
229                "nanos" => self.nanos = dec.read_i32()?,
230                _ => dec.skip_value()?,
231            }
232        }
233        ::core::result::Result::Ok(())
234    }
235}
236#[doc(hidden)]
237pub const __DURATION_TEXT_ANY: ::buffa::type_registry::TextAnyEntry = ::buffa::type_registry::TextAnyEntry {
238    type_url: "type.googleapis.com/google.protobuf.Duration",
239    text_encode: ::buffa::type_registry::any_encode_text::<Duration>,
240    text_merge: ::buffa::type_registry::any_merge_text::<Duration>,
241};