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