Skip to main content

crabka_protocol/opt/rustwide/workdir/generated/
PushTelemetryRequest.borrowed.rs

1// AUTO-GENERATED by crabka-protocol-codegen against a9ce3221537b8653448750697915607dc7936cf3. Do not edit.
2
3use bytes::{Bytes, BufMut};
4
5use crate::primitives::fixed::{get_bool, get_i32, get_i8, put_bool, put_i32, put_i8};
6use crate::primitives::string_bytes::{put_bytes, put_compact_bytes};
7use crate::primitives::string_bytes_borrowed::{get_bytes_borrowed, get_compact_bytes_borrowed};
8use crate::tagged_fields::{read_tagged_fields, tagged_fields_len, WriteTaggedFields};
9use crate::{DecodeBorrow, Encode, ProtocolError, UnknownTaggedFields};
10
11pub const API_KEY: i16 = 72;
12pub const MIN_VERSION: i16 = 0;
13pub const MAX_VERSION: i16 = 0;
14pub const FLEXIBLE_MIN: i16 = 0;
15
16#[inline]
17fn is_flexible(version: i16) -> bool { version >= FLEXIBLE_MIN }
18
19#[derive(Debug, Clone, PartialEq, Eq)]
20pub struct PushTelemetryRequest<'a> {
21    pub client_instance_id: crate::primitives::uuid::Uuid,
22    pub subscription_id: i32,
23    pub terminating: bool,
24    pub compression_type: i8,
25    pub metrics: &'a [u8],
26    pub unknown_tagged_fields: UnknownTaggedFields,
27}
28
29impl<'a> Default for PushTelemetryRequest<'a> {
30    fn default() -> Self {
31        Self {
32            client_instance_id: Default::default(),
33            subscription_id: 0i32,
34            terminating: false,
35            compression_type: 0i8,
36            metrics: &[],
37            unknown_tagged_fields: Default::default(),
38        }
39    }
40}
41
42impl<'a> PushTelemetryRequest<'a> {
43    pub fn to_owned(&self) -> crate::owned::push_telemetry_request::PushTelemetryRequest {
44        crate::owned::push_telemetry_request::PushTelemetryRequest {
45            client_instance_id: (self.client_instance_id),
46            subscription_id: (self.subscription_id),
47            terminating: (self.terminating),
48            compression_type: (self.compression_type),
49            metrics: Bytes::copy_from_slice(self.metrics),
50            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
51        }
52    }
53}
54
55impl<'a> Encode for PushTelemetryRequest<'a> {
56    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
57        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
58            return Err(ProtocolError::UnsupportedVersion { api_key: API_KEY, version });
59        }
60        let flex = is_flexible(version);
61        if version >= 0 { crate::primitives::uuid::put_uuid(buf, self.client_instance_id) }
62        if version >= 0 { put_i32(buf, self.subscription_id) }
63        if version >= 0 { put_bool(buf, self.terminating) }
64        if version >= 0 { put_i8(buf, self.compression_type) }
65        if version >= 0 { if flex { put_compact_bytes(buf, self.metrics) } else { put_bytes(buf, self.metrics) } }
66        if flex {
67            let tagged = WriteTaggedFields::new();
68            tagged.write(buf, &self.unknown_tagged_fields);
69        }
70        Ok(())
71    }
72    fn encoded_len(&self, version: i16) -> usize {
73        let flex = is_flexible(version);
74        let mut n: usize = 0;
75        if version >= 0 { n += 16; }
76        if version >= 0 { n += 4; }
77        if version >= 0 { n += 1; }
78        if version >= 0 { n += 1; }
79        if version >= 0 { n += if flex { crate::primitives::varint::uvarint_len(u32::try_from((self.metrics).len() + 1).unwrap()) + (self.metrics).len() } else { 4 + (self.metrics).len() }; }
80        if flex {
81            let known_pairs: Vec<(u32, usize)> = Vec::new();
82            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
83        }
84        n
85    }
86}
87
88impl<'de> DecodeBorrow<'de> for PushTelemetryRequest<'de> {
89    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
90        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
91            return Err(ProtocolError::UnsupportedVersion { api_key: API_KEY, version });
92        }
93        let flex = is_flexible(version);
94        let mut out = Self::default();
95        if version >= 0 { out.client_instance_id = crate::primitives::uuid::get_uuid(buf)?; }
96        if version >= 0 { out.subscription_id = get_i32(buf)?; }
97        if version >= 0 { out.terminating = get_bool(buf)?; }
98        if version >= 0 { out.compression_type = get_i8(buf)?; }
99        if version >= 0 { out.metrics = if flex { get_compact_bytes_borrowed(buf)? } else { get_bytes_borrowed(buf)? }; }
100        if flex {
101            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
102                Ok(false)
103            })?;
104        }
105        Ok(out)
106    }
107}