crabka_protocol/opt/rustwide/workdir/generated/
SyncGroupResponse.borrowed.rs1use bytes::{Bytes, BufMut};
4
5use crate::primitives::fixed::{get_i16, get_i32, put_i16, put_i32};
6use crate::primitives::string_bytes::{
7 compact_nullable_string_len, compact_string_len, nullable_string_len,
8 put_compact_nullable_string, put_compact_string, put_nullable_string, put_string,
9 string_len,
10};
11use crate::primitives::string_bytes_borrowed::{
12 get_compact_nullable_string_borrowed, get_compact_string_borrowed,
13 get_nullable_string_borrowed, get_string_borrowed,
14};
15use crate::primitives::string_bytes::{put_bytes, put_compact_bytes};
16use crate::primitives::string_bytes_borrowed::{get_bytes_borrowed, get_compact_bytes_borrowed};
17use crate::tagged_fields::{read_tagged_fields, tagged_fields_len, WriteTaggedFields};
18use crate::{DecodeBorrow, Encode, ProtocolError, UnknownTaggedFields};
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
25#[inline]
26fn is_flexible(version: i16) -> bool { version >= FLEXIBLE_MIN }
27
28#[derive(Debug, Clone, PartialEq, Eq)]
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}
37
38impl<'a> Default for SyncGroupResponse<'a> {
39 fn default() -> Self {
40 Self {
41 throttle_time_ms: 0i32,
42 error_code: 0i16,
43 protocol_type: None,
44 protocol_name: None,
45 assignment: &[],
46 unknown_tagged_fields: Default::default(),
47 }
48 }
49}
50
51impl<'a> SyncGroupResponse<'a> {
52 pub fn to_owned(&self) -> crate::owned::sync_group_response::SyncGroupResponse {
53 crate::owned::sync_group_response::SyncGroupResponse {
54 throttle_time_ms: (self.throttle_time_ms),
55 error_code: (self.error_code),
56 protocol_type: (self.protocol_type).map(|s| s.to_string()),
57 protocol_name: (self.protocol_name).map(|s| s.to_string()),
58 assignment: Bytes::copy_from_slice(self.assignment),
59 unknown_tagged_fields: self.unknown_tagged_fields.clone(),
60 }
61 }
62}
63
64impl<'a> Encode for SyncGroupResponse<'a> {
65 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
66 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
67 return Err(ProtocolError::UnsupportedVersion { api_key: API_KEY, version });
68 }
69 let flex = is_flexible(version);
70 if version >= 1 { put_i32(buf, self.throttle_time_ms) }
71 if version >= 0 { put_i16(buf, self.error_code) }
72 if version >= 5 { if flex { put_compact_nullable_string(buf, self.protocol_type) } else { put_nullable_string(buf, self.protocol_type) } }
73 if version >= 5 { if flex { put_compact_nullable_string(buf, self.protocol_name) } else { put_nullable_string(buf, self.protocol_name) } }
74 if version >= 0 { if flex { put_compact_bytes(buf, self.assignment) } else { put_bytes(buf, self.assignment) } }
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 >= 1 { n += 4; }
85 if version >= 0 { n += 2; }
86 if version >= 5 { n += if flex { compact_nullable_string_len(self.protocol_type) } else { nullable_string_len(self.protocol_type) }; }
87 if version >= 5 { n += if flex { compact_nullable_string_len(self.protocol_name) } else { nullable_string_len(self.protocol_name) }; }
88 if version >= 0 { n += if flex { crate::primitives::varint::uvarint_len(u32::try_from((self.assignment).len() + 1).unwrap()) + (self.assignment).len() } else { 4 + (self.assignment).len() }; }
89 if flex {
90 let known_pairs: Vec<(u32, usize)> = Vec::new();
91 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
92 }
93 n
94 }
95}
96
97impl<'de> DecodeBorrow<'de> for SyncGroupResponse<'de> {
98 fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
99 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
100 return Err(ProtocolError::UnsupportedVersion { api_key: API_KEY, version });
101 }
102 let flex = is_flexible(version);
103 let mut out = Self::default();
104 if version >= 1 { out.throttle_time_ms = get_i32(buf)?; }
105 if version >= 0 { out.error_code = get_i16(buf)?; }
106 if version >= 5 { out.protocol_type = if flex { get_compact_nullable_string_borrowed(buf)? } else { get_nullable_string_borrowed(buf)? }; }
107 if version >= 5 { out.protocol_name = if flex { get_compact_nullable_string_borrowed(buf)? } else { get_nullable_string_borrowed(buf)? }; }
108 if version >= 0 { out.assignment = if flex { get_compact_bytes_borrowed(buf)? } else { get_bytes_borrowed(buf)? }; }
109 if flex {
110 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
111 Ok(false)
112 })?;
113 }
114 Ok(out)
115 }
116}