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