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