Skip to main content

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

1// AUTO-GENERATED by krabka-protocol-codegen against a9ce3221537b8653448750697915607dc7936cf3. Do not edit.
2use bytes::{BufMut, Bytes};
3
4use crate::{
5    DecodeBorrow, Encode, ProtocolError, UnknownTaggedFields,
6    primitives::{
7        fixed::{get_bool, get_i8, get_i32, put_bool, put_i8, put_i32},
8        string_bytes::{put_bytes, put_compact_bytes},
9        string_bytes_borrowed::{get_bytes_borrowed, get_compact_bytes_borrowed},
10    },
11    tagged_fields::{WriteTaggedFields, read_tagged_fields, tagged_fields_len},
12};
13pub const API_KEY: i16 = 72;
14pub const MIN_VERSION: i16 = 0;
15pub const MAX_VERSION: i16 = 0;
16pub const LATEST_STABLE_VERSION: i16 = 0;
17pub const FLEXIBLE_MIN: i16 = 0;
18#[inline]
19#[must_use]
20pub fn is_flexible(version: i16) -> bool {
21    version >= FLEXIBLE_MIN
22}
23#[derive(Debug, Clone, PartialEq, Eq, Default)]
24pub struct PushTelemetryRequest<'a> {
25    pub client_instance_id: crate::primitives::uuid::Uuid,
26    pub subscription_id: i32,
27    pub terminating: bool,
28    pub compression_type: i8,
29    pub metrics: &'a [u8],
30    pub unknown_tagged_fields: UnknownTaggedFields,
31}
32impl PushTelemetryRequest<'_> {
33    /// # Panics
34    ///
35    /// Panics if a records field contains an invalid encoded record batch.
36    #[must_use]
37    pub fn to_owned(&self) -> crate::owned::push_telemetry_request::PushTelemetryRequest {
38        crate::owned::push_telemetry_request::PushTelemetryRequest {
39            client_instance_id: (self.client_instance_id),
40            subscription_id: (self.subscription_id),
41            terminating: (self.terminating),
42            compression_type: (self.compression_type),
43            metrics: Bytes::copy_from_slice(self.metrics),
44            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
45        }
46    }
47}
48impl Encode for PushTelemetryRequest<'_> {
49    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
50        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
51            return Err(ProtocolError::UnsupportedVersion {
52                api_key: API_KEY,
53                version,
54            });
55        }
56        let flex = is_flexible(version);
57        if version >= 0 {
58            crate::primitives::uuid::put_uuid(buf, self.client_instance_id);
59        }
60        if version >= 0 {
61            put_i32(buf, self.subscription_id);
62        }
63        if version >= 0 {
64            put_bool(buf, self.terminating);
65        }
66        if version >= 0 {
67            put_i8(buf, self.compression_type);
68        }
69        if version >= 0 {
70            if flex {
71                let () = put_compact_bytes(buf, self.metrics);
72            } else {
73                let () = put_bytes(buf, self.metrics);
74            }
75        }
76        if flex {
77            let tagged = WriteTaggedFields::new();
78            tagged.write(buf, &self.unknown_tagged_fields);
79        }
80        Ok(())
81    }
82    fn encoded_len(&self, version: i16) -> usize {
83        let flex = is_flexible(version);
84        let mut n: usize = 0;
85        if version >= 0 {
86            n += 16;
87        }
88        if version >= 0 {
89            n += 4;
90        }
91        if version >= 0 {
92            n += 1;
93        }
94        if version >= 0 {
95            n += 1;
96        }
97        if version >= 0 {
98            n += if flex {
99                crate::primitives::varint::uvarint_len(
100                    u32::try_from((self.metrics).len() + 1).unwrap(),
101                ) + (self.metrics).len()
102            } else {
103                4 + (self.metrics).len()
104            };
105        }
106        if flex {
107            let known_pairs: Vec<(u32, usize)> = Vec::new();
108            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
109        }
110        n
111    }
112}
113impl<'de> DecodeBorrow<'de> for PushTelemetryRequest<'de> {
114    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
115        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
116            return Err(ProtocolError::UnsupportedVersion {
117                api_key: API_KEY,
118                version,
119            });
120        }
121        let flex = is_flexible(version);
122        let mut out = Self::default();
123        if version >= 0 {
124            out.client_instance_id = crate::primitives::uuid::get_uuid(buf)?;
125        }
126        if version >= 0 {
127            out.subscription_id = get_i32(buf)?;
128        }
129        if version >= 0 {
130            out.terminating = get_bool(buf)?;
131        }
132        if version >= 0 {
133            out.compression_type = get_i8(buf)?;
134        }
135        if version >= 0 {
136            out.metrics = if flex {
137                get_compact_bytes_borrowed(buf)?
138            } else {
139                get_bytes_borrowed(buf)?
140            };
141        }
142        if flex {
143            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
144        }
145        Ok(out)
146    }
147}
148#[cfg(test)]
149impl PushTelemetryRequest<'_> {
150    #[must_use]
151    pub fn populated(version: i16) -> Self {
152        let mut m = Self::default();
153        if version >= 0 {
154            m.client_instance_id = crate::primitives::uuid::Uuid([1u8; 16]);
155        }
156        if version >= 0 {
157            m.subscription_id = 1i32;
158        }
159        if version >= 0 {
160            m.terminating = true;
161        }
162        if version >= 0 {
163            m.compression_type = 1i8;
164        }
165        if version >= 0 {
166            m.metrics = &b"x"[..];
167        }
168        m
169    }
170}