Skip to main content

krabka_protocol/opt/rustwide/workdir/generated/
SyncGroupResponse.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::sync_group_response,
7    primitives::{
8        fixed::{get_i16, get_i32, put_i16, put_i32},
9        string_bytes::{
10            bytes_len, compact_bytes_len, compact_nullable_string_len, nullable_string_len,
11            put_bytes, put_compact_bytes, put_compact_nullable_string, put_nullable_string,
12        },
13        string_bytes_borrowed::{
14            get_bytes_borrowed, get_compact_bytes_borrowed, get_compact_nullable_string_borrowed,
15            get_nullable_string_borrowed,
16        },
17    },
18    tagged_fields::{WriteTaggedFields, read_tagged_fields, tagged_fields_len},
19};
20pub const API_KEY: i16 = 14;
21pub const MIN_VERSION: i16 = 0;
22pub const MAX_VERSION: i16 = 5;
23pub const FLEXIBLE_MIN: i16 = 4;
24#[inline]
25#[must_use]
26pub fn is_flexible(version: i16) -> bool {
27    version >= FLEXIBLE_MIN
28}
29#[derive(Debug, Clone, PartialEq, Eq, Default)]
30pub struct SyncGroupResponse<'a> {
31    pub throttle_time_ms: i32,
32    pub error_code: i16,
33    pub protocol_type: Option<&'a str>,
34    pub protocol_name: Option<&'a str>,
35    pub assignment: &'a [u8],
36    pub unknown_tagged_fields: UnknownTaggedFields,
37}
38impl SyncGroupResponse<'_> {
39    #[must_use]
40    pub fn to_owned(&self) -> sync_group_response::SyncGroupResponse {
41        sync_group_response::SyncGroupResponse {
42            throttle_time_ms: (self.throttle_time_ms),
43            error_code: (self.error_code),
44            protocol_type: (self.protocol_type).map(str::to_owned),
45            protocol_name: (self.protocol_name).map(str::to_owned),
46            assignment: Bytes::copy_from_slice(self.assignment),
47            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
48        }
49    }
50}
51impl Encode for SyncGroupResponse<'_> {
52    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
53        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
54            return Err(ProtocolError::UnsupportedVersion {
55                api_key: API_KEY,
56                version,
57            });
58        }
59        let flex = is_flexible(version);
60        if version >= 1 {
61            put_i32(buf, self.throttle_time_ms);
62        }
63        if version >= 0 {
64            put_i16(buf, self.error_code);
65        }
66        if version >= 5 {
67            if flex {
68                put_compact_nullable_string(buf, self.protocol_type)?;
69            } else {
70                put_nullable_string(buf, self.protocol_type)?;
71            }
72        }
73        if version >= 5 {
74            if flex {
75                put_compact_nullable_string(buf, self.protocol_name)?;
76            } else {
77                put_nullable_string(buf, self.protocol_name)?;
78            }
79        }
80        if version >= 0 {
81            if flex {
82                put_compact_bytes(buf, self.assignment)?;
83            } else {
84                put_bytes(buf, self.assignment)?;
85            }
86        }
87        if flex {
88            let tagged = WriteTaggedFields::new();
89            tagged.write(buf, &self.unknown_tagged_fields)?;
90        }
91        Ok(())
92    }
93    fn encoded_len(&self, version: i16) -> usize {
94        let flex = is_flexible(version);
95        let mut n: usize = 0;
96        if version >= 1 {
97            n += 4;
98        }
99        if version >= 0 {
100            n += 2;
101        }
102        if version >= 5 {
103            n += if flex {
104                compact_nullable_string_len(self.protocol_type)
105            } else {
106                nullable_string_len(self.protocol_type)
107            };
108        }
109        if version >= 5 {
110            n += if flex {
111                compact_nullable_string_len(self.protocol_name)
112            } else {
113                nullable_string_len(self.protocol_name)
114            };
115        }
116        if version >= 0 {
117            n += if flex {
118                compact_bytes_len(self.assignment)
119            } else {
120                bytes_len(self.assignment)
121            };
122        }
123        if flex {
124            let known_pairs: Vec<(u32, usize)> = Vec::new();
125            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
126        }
127        n
128    }
129}
130impl<'de> DecodeBorrow<'de> for SyncGroupResponse<'de> {
131    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
132        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
133            return Err(ProtocolError::UnsupportedVersion {
134                api_key: API_KEY,
135                version,
136            });
137        }
138        let flex = is_flexible(version);
139        let mut out = Self::default();
140        if version >= 1 {
141            out.throttle_time_ms = get_i32(buf)?;
142        }
143        if version >= 0 {
144            out.error_code = get_i16(buf)?;
145        }
146        if version >= 5 {
147            out.protocol_type = if flex {
148                get_compact_nullable_string_borrowed(buf)?
149            } else {
150                get_nullable_string_borrowed(buf)?
151            };
152        }
153        if version >= 5 {
154            out.protocol_name = if flex {
155                get_compact_nullable_string_borrowed(buf)?
156            } else {
157                get_nullable_string_borrowed(buf)?
158            };
159        }
160        if version >= 0 {
161            out.assignment = if flex {
162                get_compact_bytes_borrowed(buf)?
163            } else {
164                get_bytes_borrowed(buf)?
165            };
166        }
167        if flex {
168            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
169        }
170        Ok(out)
171    }
172}
173#[cfg(test)]
174impl SyncGroupResponse<'_> {
175    #[must_use]
176    pub fn populated(version: i16) -> Self {
177        let mut m = Self::default();
178        if version >= 1 {
179            m.throttle_time_ms = 1i32;
180        }
181        if version >= 0 {
182            m.error_code = 1i16;
183        }
184        if version >= 5 {
185            m.protocol_type = Some("x");
186        }
187        if version >= 5 {
188            m.protocol_name = Some("x");
189        }
190        if version >= 0 {
191            m.assignment = &b"x"[..];
192        }
193        m
194    }
195}