Skip to main content

buffa_types/generated/
google.protobuf.duration.rs

1// @generated by protoc-gen-buffa. 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    #[doc(hidden)]
89    pub __buffa_cached_size: ::buffa::__private::CachedSize,
90}
91impl ::core::fmt::Debug for Duration {
92    fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
93        f.debug_struct("Duration")
94            .field("seconds", &self.seconds)
95            .field("nanos", &self.nanos)
96            .finish()
97    }
98}
99impl Duration {
100    /// Protobuf type URL for this message, for use with `Any::pack` and
101    /// `Any::unpack_if`.
102    ///
103    /// Format: `type.googleapis.com/<fully.qualified.TypeName>`
104    pub const TYPE_URL: &'static str = "type.googleapis.com/google.protobuf.Duration";
105}
106unsafe impl ::buffa::DefaultInstance for Duration {
107    fn default_instance() -> &'static Self {
108        static VALUE: ::buffa::__private::OnceBox<Duration> = ::buffa::__private::OnceBox::new();
109        VALUE.get_or_init(|| ::buffa::alloc::boxed::Box::new(Self::default()))
110    }
111}
112impl ::buffa::Message for Duration {
113    /// Returns the total encoded size in bytes.
114    ///
115    /// The result is a `u32`; the protobuf specification requires all
116    /// messages to fit within 2 GiB (2,147,483,647 bytes), so a
117    /// compliant message will never overflow this type.
118    fn compute_size(&self) -> u32 {
119        #[allow(unused_imports)]
120        use ::buffa::Enumeration as _;
121        let mut size = 0u32;
122        if self.seconds != 0i64 {
123            size += 1u32 + ::buffa::types::int64_encoded_len(self.seconds) as u32;
124        }
125        if self.nanos != 0i32 {
126            size += 1u32 + ::buffa::types::int32_encoded_len(self.nanos) as u32;
127        }
128        size += self.__buffa_unknown_fields.encoded_len() as u32;
129        self.__buffa_cached_size.set(size);
130        size
131    }
132    fn write_to(&self, buf: &mut impl ::buffa::bytes::BufMut) {
133        #[allow(unused_imports)]
134        use ::buffa::Enumeration as _;
135        if self.seconds != 0i64 {
136            ::buffa::encoding::Tag::new(1u32, ::buffa::encoding::WireType::Varint)
137                .encode(buf);
138            ::buffa::types::encode_int64(self.seconds, buf);
139        }
140        if self.nanos != 0i32 {
141            ::buffa::encoding::Tag::new(2u32, ::buffa::encoding::WireType::Varint)
142                .encode(buf);
143            ::buffa::types::encode_int32(self.nanos, buf);
144        }
145        self.__buffa_unknown_fields.write_to(buf);
146    }
147    fn merge_field(
148        &mut self,
149        tag: ::buffa::encoding::Tag,
150        buf: &mut impl ::buffa::bytes::Buf,
151        depth: u32,
152    ) -> ::core::result::Result<(), ::buffa::DecodeError> {
153        #[allow(unused_imports)]
154        use ::buffa::bytes::Buf as _;
155        #[allow(unused_imports)]
156        use ::buffa::Enumeration as _;
157        match tag.field_number() {
158            1u32 => {
159                if tag.wire_type() != ::buffa::encoding::WireType::Varint {
160                    return ::core::result::Result::Err(::buffa::DecodeError::WireTypeMismatch {
161                        field_number: 1u32,
162                        expected: 0u8,
163                        actual: tag.wire_type() as u8,
164                    });
165                }
166                self.seconds = ::buffa::types::decode_int64(buf)?;
167            }
168            2u32 => {
169                if tag.wire_type() != ::buffa::encoding::WireType::Varint {
170                    return ::core::result::Result::Err(::buffa::DecodeError::WireTypeMismatch {
171                        field_number: 2u32,
172                        expected: 0u8,
173                        actual: tag.wire_type() as u8,
174                    });
175                }
176                self.nanos = ::buffa::types::decode_int32(buf)?;
177            }
178            _ => {
179                self.__buffa_unknown_fields
180                    .push(::buffa::encoding::decode_unknown_field(tag, buf, depth)?);
181            }
182        }
183        ::core::result::Result::Ok(())
184    }
185    fn cached_size(&self) -> u32 {
186        self.__buffa_cached_size.get()
187    }
188    fn clear(&mut self) {
189        self.seconds = 0i64;
190        self.nanos = 0i32;
191        self.__buffa_unknown_fields.clear();
192        self.__buffa_cached_size.set(0);
193    }
194}
195impl ::buffa::ExtensionSet for Duration {
196    const PROTO_FQN: &'static str = "google.protobuf.Duration";
197    fn unknown_fields(&self) -> &::buffa::UnknownFields {
198        &self.__buffa_unknown_fields
199    }
200    fn unknown_fields_mut(&mut self) -> &mut ::buffa::UnknownFields {
201        &mut self.__buffa_unknown_fields
202    }
203}
204impl ::buffa::text::TextFormat for Duration {
205    fn encode_text(
206        &self,
207        enc: &mut ::buffa::text::TextEncoder<'_>,
208    ) -> ::core::fmt::Result {
209        #[allow(unused_imports)]
210        use ::buffa::Enumeration as _;
211        if self.seconds != 0i64 {
212            enc.write_field_name("seconds")?;
213            enc.write_i64(self.seconds)?;
214        }
215        if self.nanos != 0i32 {
216            enc.write_field_name("nanos")?;
217            enc.write_i32(self.nanos)?;
218        }
219        enc.write_unknown_fields(&self.__buffa_unknown_fields)?;
220        ::core::result::Result::Ok(())
221    }
222    fn merge_text(
223        &mut self,
224        dec: &mut ::buffa::text::TextDecoder<'_>,
225    ) -> ::core::result::Result<(), ::buffa::text::ParseError> {
226        #[allow(unused_imports)]
227        use ::buffa::Enumeration as _;
228        while let ::core::option::Option::Some(__name) = dec.read_field_name()? {
229            match __name {
230                "seconds" => self.seconds = dec.read_i64()?,
231                "nanos" => self.nanos = dec.read_i32()?,
232                _ => dec.skip_value()?,
233            }
234        }
235        ::core::result::Result::Ok(())
236    }
237}
238#[doc(hidden)]
239pub const __DURATION_TEXT_ANY: ::buffa::type_registry::TextAnyEntry = ::buffa::type_registry::TextAnyEntry {
240    type_url: "type.googleapis.com/google.protobuf.Duration",
241    text_encode: ::buffa::type_registry::any_encode_text::<Duration>,
242    text_merge: ::buffa::type_registry::any_merge_text::<Duration>,
243};
244/// A Duration represents a signed, fixed-length span of time represented
245/// as a count of seconds and fractions of seconds at nanosecond
246/// resolution. It is independent of any calendar and concepts like "day"
247/// or "month". It is related to Timestamp in that the difference between
248/// two Timestamp values is a Duration and it can be added or subtracted
249/// from a Timestamp. Range is approximately +-10,000 years.
250///
251/// # Examples
252///
253/// Example 1: Compute Duration from two Timestamps in pseudo code.
254///
255/// ```text
256/// Timestamp start = ...;
257/// Timestamp end = ...;
258/// Duration duration = ...;
259///
260/// duration.seconds = end.seconds - start.seconds;
261/// duration.nanos = end.nanos - start.nanos;
262///
263/// if (duration.seconds < 0 && duration.nanos > 0) {
264///   duration.seconds += 1;
265///   duration.nanos -= 1000000000;
266/// } else if (duration.seconds > 0 && duration.nanos < 0) {
267///   duration.seconds -= 1;
268///   duration.nanos += 1000000000;
269/// }
270/// ```
271///
272/// Example 2: Compute Timestamp from Timestamp + Duration in pseudo code.
273///
274/// ```text
275/// Timestamp start = ...;
276/// Duration duration = ...;
277/// Timestamp end = ...;
278///
279/// end.seconds = start.seconds + duration.seconds;
280/// end.nanos = start.nanos + duration.nanos;
281///
282/// if (end.nanos < 0) {
283///   end.seconds -= 1;
284///   end.nanos += 1000000000;
285/// } else if (end.nanos >= 1000000000) {
286///   end.seconds += 1;
287///   end.nanos -= 1000000000;
288/// }
289/// ```
290///
291/// Example 3: Compute Duration from datetime.timedelta in Python.
292///
293/// ```text
294/// td = datetime.timedelta(days=3, minutes=10)
295/// duration = Duration()
296/// duration.FromTimedelta(td)
297/// ```
298///
299/// # JSON Mapping
300///
301/// In JSON format, the Duration type is encoded as a string rather than an
302/// object, where the string ends in the suffix "s" (indicating seconds) and
303/// is preceded by the number of seconds, with nanoseconds expressed as
304/// fractional seconds. For example, 3 seconds with 0 nanoseconds should be
305/// encoded in JSON format as "3s", while 3 seconds and 1 nanosecond should
306/// be expressed in JSON format as "3.000000001s", and 3 seconds and 1
307/// microsecond should be expressed in JSON format as "3.000001s".
308#[derive(Clone, Debug, Default)]
309pub struct DurationView<'a> {
310    /// Signed seconds of the span of time. Must be from -315,576,000,000
311    /// to +315,576,000,000 inclusive. Note: these bounds are computed from:
312    /// 60 sec/min * 60 min/hr * 24 hr/day * 365.25 days/year * 10000 years
313    ///
314    /// Field 1: `seconds`
315    pub seconds: i64,
316    /// Signed fractions of a second at nanosecond resolution of the span
317    /// of time. Durations less than one second are represented with a 0
318    /// `seconds` field and a positive or negative `nanos` field. For durations
319    /// of one second or more, a non-zero value for the `nanos` field must be
320    /// of the same sign as the `seconds` field. Must be from -999,999,999
321    /// to +999,999,999 inclusive.
322    ///
323    /// Field 2: `nanos`
324    pub nanos: i32,
325    pub __buffa_unknown_fields: ::buffa::UnknownFieldsView<'a>,
326}
327impl<'a> DurationView<'a> {
328    /// Decode from `buf`, enforcing a recursion depth limit for nested messages.
329    ///
330    /// Called by [`::buffa::MessageView::decode_view`] with [`::buffa::RECURSION_LIMIT`]
331    /// and by generated sub-message decode arms with `depth - 1`.
332    ///
333    /// **Not part of the public API.** Named with a leading underscore to
334    /// signal that it is for generated-code use only.
335    #[doc(hidden)]
336    pub fn _decode_depth(
337        buf: &'a [u8],
338        depth: u32,
339    ) -> ::core::result::Result<Self, ::buffa::DecodeError> {
340        let mut view = Self::default();
341        view._merge_into_view(buf, depth)?;
342        ::core::result::Result::Ok(view)
343    }
344    /// Merge fields from `buf` into this view (proto merge semantics).
345    ///
346    /// Repeated fields append; singular fields last-wins; singular
347    /// MESSAGE fields merge recursively. Used by sub-message decode
348    /// arms when the same field appears multiple times on the wire.
349    ///
350    /// **Not part of the public API.**
351    #[doc(hidden)]
352    pub fn _merge_into_view(
353        &mut self,
354        buf: &'a [u8],
355        depth: u32,
356    ) -> ::core::result::Result<(), ::buffa::DecodeError> {
357        let _ = depth;
358        #[allow(unused_variables)]
359        let view = self;
360        let mut cur: &'a [u8] = buf;
361        while !cur.is_empty() {
362            let before_tag = cur;
363            let tag = ::buffa::encoding::Tag::decode(&mut cur)?;
364            match tag.field_number() {
365                1u32 => {
366                    if tag.wire_type() != ::buffa::encoding::WireType::Varint {
367                        return ::core::result::Result::Err(::buffa::DecodeError::WireTypeMismatch {
368                            field_number: 1u32,
369                            expected: 0u8,
370                            actual: tag.wire_type() as u8,
371                        });
372                    }
373                    view.seconds = ::buffa::types::decode_int64(&mut cur)?;
374                }
375                2u32 => {
376                    if tag.wire_type() != ::buffa::encoding::WireType::Varint {
377                        return ::core::result::Result::Err(::buffa::DecodeError::WireTypeMismatch {
378                            field_number: 2u32,
379                            expected: 0u8,
380                            actual: tag.wire_type() as u8,
381                        });
382                    }
383                    view.nanos = ::buffa::types::decode_int32(&mut cur)?;
384                }
385                _ => {
386                    ::buffa::encoding::skip_field_depth(tag, &mut cur, depth)?;
387                    let span_len = before_tag.len() - cur.len();
388                    view.__buffa_unknown_fields.push_raw(&before_tag[..span_len]);
389                }
390            }
391        }
392        ::core::result::Result::Ok(())
393    }
394}
395impl<'a> ::buffa::MessageView<'a> for DurationView<'a> {
396    type Owned = Duration;
397    fn decode_view(buf: &'a [u8]) -> ::core::result::Result<Self, ::buffa::DecodeError> {
398        Self::_decode_depth(buf, ::buffa::RECURSION_LIMIT)
399    }
400    fn decode_view_with_limit(
401        buf: &'a [u8],
402        depth: u32,
403    ) -> ::core::result::Result<Self, ::buffa::DecodeError> {
404        Self::_decode_depth(buf, depth)
405    }
406    /// Convert this view to the owned message type.
407    #[allow(clippy::redundant_closure, clippy::useless_conversion)]
408    fn to_owned_message(&self) -> Duration {
409        #[allow(unused_imports)]
410        use ::buffa::alloc::string::ToString as _;
411        Duration {
412            seconds: self.seconds,
413            nanos: self.nanos,
414            __buffa_unknown_fields: self
415                .__buffa_unknown_fields
416                .to_owned()
417                .unwrap_or_default()
418                .into(),
419            ..::core::default::Default::default()
420        }
421    }
422}
423unsafe impl ::buffa::DefaultViewInstance for DurationView<'static> {
424    fn default_view_instance() -> &'static Self {
425        static VALUE: ::buffa::__private::OnceBox<DurationView<'static>> = ::buffa::__private::OnceBox::new();
426        VALUE.get_or_init(|| ::buffa::alloc::boxed::Box::new(Self::default()))
427    }
428}
429unsafe impl<'a> ::buffa::HasDefaultViewInstance for DurationView<'a> {
430    type Static = DurationView<'static>;
431}