crabka_protocol/opt/rustwide/workdir/generated/
ProduceRequest.owned.rs1use bytes::{Buf, 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, get_compact_nullable_string_owned,
8 get_compact_string_owned, get_nullable_string_owned, get_string_owned, nullable_string_len,
9 put_compact_nullable_string, put_compact_string, put_nullable_string, put_string,
10 string_len,
11};
12use crate::primitives::string_bytes::{get_compact_nullable_bytes_owned, get_nullable_bytes_owned, put_bytes, put_compact_bytes, put_compact_nullable_bytes, put_nullable_bytes};
13use crate::tagged_fields::{read_tagged_fields, tagged_fields_len, WriteTaggedFields};
14use crate::{Decode, Encode, ProtocolError, UnknownTaggedFields};
15
16pub const API_KEY: i16 = 0;
17pub const MIN_VERSION: i16 = 3;
18pub const MAX_VERSION: i16 = 13;
19pub const FLEXIBLE_MIN: i16 = 9;
20
21#[inline]
22fn is_flexible(version: i16) -> bool { version >= FLEXIBLE_MIN }
23
24#[derive(Debug, Clone, PartialEq, Eq, Default)]
25pub struct ProduceRequest {
26 pub transactional_id: Option<String>,
27 pub acks: i16,
28 pub timeout_ms: i32,
29 pub topic_data: Vec<TopicProduceData>,
30 pub unknown_tagged_fields: UnknownTaggedFields,
31}
32
33impl Encode for ProduceRequest {
34 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
35 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
36 return Err(ProtocolError::UnsupportedVersion { api_key: API_KEY, version });
37 }
38 let flex = is_flexible(version);
39 if version >= 3 { if flex { put_compact_nullable_string(buf, self.transactional_id.as_deref()) } else { put_nullable_string(buf, self.transactional_id.as_deref()) } }
40 if version >= 0 { put_i16(buf, self.acks) }
41 if version >= 0 { put_i32(buf, self.timeout_ms) }
42 if version >= 0 { { crate::primitives::array::put_array_len(buf, (self.topic_data).len(), flex); for it in &self.topic_data { it.encode(buf, version)?; } } }
43 if flex {
44 let tagged = WriteTaggedFields::new();
45 tagged.write(buf, &self.unknown_tagged_fields);
46 }
47 Ok(())
48 }
49 fn encoded_len(&self, version: i16) -> usize {
50 let flex = is_flexible(version);
51 let mut n: usize = 0;
52 if version >= 3 { n += if flex { compact_nullable_string_len(self.transactional_id.as_deref()) } else { nullable_string_len(self.transactional_id.as_deref()) }; }
53 if version >= 0 { n += 2; }
54 if version >= 0 { n += 4; }
55 if version >= 0 { n += { let prefix = crate::primitives::array::array_len_prefix_len((self.topic_data).len(), flex); let body: usize = (self.topic_data).iter().map(|it| it.encoded_len(version)).sum(); prefix + body }; }
56 if flex {
57 let known_pairs: Vec<(u32, usize)> = Vec::new();
58 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
59 }
60 n
61 }
62}
63
64impl<'de> Decode<'de> for ProduceRequest {
65 fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, 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 let mut out = Self::default();
71 if version >= 3 { out.transactional_id = if flex { get_compact_nullable_string_owned(buf)? } else { get_nullable_string_owned(buf)? }; }
72 if version >= 0 { out.acks = get_i16(buf)?; }
73 if version >= 0 { out.timeout_ms = get_i32(buf)?; }
74 if version >= 0 { out.topic_data = { let n = crate::primitives::array::get_array_len(buf, flex)?; let mut v = Vec::with_capacity(n); for _ in 0..n { v.push(TopicProduceData::decode(buf, version)?); } v }; }
75 if flex {
76 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
77 Ok(false)
78 })?;
79 }
80 Ok(out)
81 }
82}
83
84#[derive(Debug, Clone, PartialEq, Eq, Default)]
85pub struct TopicProduceData {
86 pub name: String,
87 pub topic_id: crate::primitives::uuid::Uuid,
88 pub partition_data: Vec<PartitionProduceData>,
89 pub unknown_tagged_fields: UnknownTaggedFields,
90}
91
92impl Encode for TopicProduceData {
93 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
94 let flex = version >= 9;
95 if version >= 0 && version <= 12 { if flex { put_compact_string(buf, &self.name) } else { put_string(buf, &self.name) } }
96 if version >= 13 { crate::primitives::uuid::put_uuid(buf, self.topic_id) }
97 if version >= 0 { { crate::primitives::array::put_array_len(buf, (self.partition_data).len(), flex); for it in &self.partition_data { it.encode(buf, version)?; } } }
98 if flex {
99 let tagged = WriteTaggedFields::new();
100 tagged.write(buf, &self.unknown_tagged_fields);
101 }
102 Ok(())
103 }
104 fn encoded_len(&self, version: i16) -> usize {
105 let flex = version >= 9;
106 let mut n: usize = 0;
107 if version >= 0 && version <= 12 { n += if flex { compact_string_len(&self.name) } else { string_len(&self.name) }; }
108 if version >= 13 { n += 16; }
109 if version >= 0 { n += { let prefix = crate::primitives::array::array_len_prefix_len((self.partition_data).len(), flex); let body: usize = (self.partition_data).iter().map(|it| it.encoded_len(version)).sum(); prefix + body }; }
110 if flex {
111 let known_pairs: Vec<(u32, usize)> = Vec::new();
112 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
113 }
114 n
115 }
116}
117
118impl<'de> Decode<'de> for TopicProduceData {
119 fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
120 let flex = version >= 9;
121 let mut out = Self::default();
122 if version >= 0 && version <= 12 { out.name = if flex { get_compact_string_owned(buf)? } else { get_string_owned(buf)? }; }
123 if version >= 13 { out.topic_id = crate::primitives::uuid::get_uuid(buf)?; }
124 if version >= 0 { out.partition_data = { let n = crate::primitives::array::get_array_len(buf, flex)?; let mut v = Vec::with_capacity(n); for _ in 0..n { v.push(PartitionProduceData::decode(buf, version)?); } v }; }
125 if flex {
126 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
127 Ok(false)
128 })?;
129 }
130 Ok(out)
131 }
132}
133
134#[derive(Debug, Clone, PartialEq, Eq, Default)]
135pub struct PartitionProduceData {
136 pub index: i32,
137 pub records: Option<crate::records::RecordsPayload>,
138 pub unknown_tagged_fields: UnknownTaggedFields,
139}
140
141impl Encode for PartitionProduceData {
142 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
143 let flex = version >= 9;
144 if version >= 0 { put_i32(buf, self.index) }
145 if version >= 0 { match &self.records { None => if flex { put_compact_nullable_bytes(buf, None) } else { put_nullable_bytes(buf, None) }, Some(__rb) => { let mut __rb_buf = bytes::BytesMut::new(); <crate::records::RecordsPayload as crate::Encode>::encode(__rb, &mut __rb_buf, version)?; if flex { put_compact_bytes(buf, &__rb_buf) } else { put_bytes(buf, &__rb_buf) } } } }
146 if flex {
147 let tagged = WriteTaggedFields::new();
148 tagged.write(buf, &self.unknown_tagged_fields);
149 }
150 Ok(())
151 }
152 fn encoded_len(&self, version: i16) -> usize {
153 let flex = version >= 9;
154 let mut n: usize = 0;
155 if version >= 0 { n += 4; }
156 if version >= 0 { n += match &self.records { None => if flex { crate::primitives::varint::uvarint_len(0) } else { 4 }, Some(__rb) => { let __rb_len = <crate::records::RecordsPayload as crate::Encode>::encoded_len(__rb, version); if flex { crate::primitives::string_bytes::compact_bytes_len_from_size(__rb_len) } else { 4 + __rb_len } } }; }
157 if flex {
158 let known_pairs: Vec<(u32, usize)> = Vec::new();
159 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
160 }
161 n
162 }
163}
164
165impl<'de> Decode<'de> for PartitionProduceData {
166 fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
167 let flex = version >= 9;
168 let mut out = Self::default();
169 if version >= 0 { out.index = get_i32(buf)?; }
170 if version >= 0 { out.records = { let __rb_opt = if flex { get_compact_nullable_bytes_owned(buf)? } else { get_nullable_bytes_owned(buf)? }; match __rb_opt { None => None, Some(__rb_bytes) => { let mut __rb_cur: &[u8] = &__rb_bytes; Some(<crate::records::RecordsPayload as crate::Decode>::decode(&mut __rb_cur, version)?) } } }; }
171 if flex {
172 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
173 Ok(false)
174 })?;
175 }
176 Ok(out)
177 }
178}
179
180#[must_use]
183#[allow(unused_comparisons)]
184pub fn default_json(version: i16) -> ::serde_json::Value {
185 let mut obj = ::serde_json::Map::new();
186 if version >= 3 {
187 obj.insert("transactionalId".to_string(), ::serde_json::Value::Null);
188 }
189 obj.insert("acks".to_string(), ::serde_json::json!(0));
190 obj.insert("timeoutMs".to_string(), ::serde_json::json!(0));
191 obj.insert("topicData".to_string(), ::serde_json::Value::Array(vec![]));
192 ::serde_json::Value::Object(obj)
193}
194
195impl crate::ProtocolRequest for ProduceRequest {
196 const API_KEY: i16 = API_KEY;
197 const MIN_VERSION: i16 = MIN_VERSION;
198 const MAX_VERSION: i16 = MAX_VERSION;
199 const FLEXIBLE_MIN: i16 = FLEXIBLE_MIN;
200 type Response = super::produce_response::ProduceResponse;
201}