krabka_protocol/opt/rustwide/workdir/generated/
OffsetCommitValue.owned.rs1use bytes::{Buf, BufMut};
4
5use crate::{
6 Decode, Encode, ProtocolError, UnknownTaggedFields,
7 primitives::{
8 fixed::{get_i32, get_i64, put_i32, put_i64},
9 string_bytes::{
10 compact_string_len, get_compact_string_owned, get_string_owned, put_compact_string,
11 put_string, string_len,
12 },
13 },
14 tagged_fields::{WriteTaggedFields, encode_to_bytes, read_tagged_fields, tagged_fields_len},
15};
16pub const MIN_VERSION: i16 = 0;
17pub const MAX_VERSION: i16 = 4;
18pub const FLEXIBLE_MIN: i16 = 4;
19#[inline]
20#[must_use]
21pub fn is_flexible(version: i16) -> bool {
22 version >= FLEXIBLE_MIN
23}
24#[derive(Debug, Clone, PartialEq, Eq)]
25pub struct OffsetCommitValue {
26 pub offset: i64,
27 pub leader_epoch: i32,
28 pub metadata: String,
29 pub commit_timestamp: i64,
30 pub expire_timestamp: i64,
31 pub topic_id: crate::primitives::uuid::Uuid,
32 pub unknown_tagged_fields: UnknownTaggedFields,
33}
34impl Default for OffsetCommitValue {
35 fn default() -> Self {
36 Self {
37 offset: 0i64,
38 leader_epoch: -1i32,
39 metadata: String::new(),
40 commit_timestamp: 0i64,
41 expire_timestamp: -1i64,
42 topic_id: crate::primitives::uuid::Uuid::default(),
43 unknown_tagged_fields: UnknownTaggedFields::default(),
44 }
45 }
46}
47impl Encode for OffsetCommitValue {
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::SchemaMismatch(
51 "OffsetCommitValue version out of range",
52 ));
53 }
54 let flex = is_flexible(version);
55 if version >= 0 {
56 put_i64(buf, self.offset);
57 }
58 if version >= 3 {
59 put_i32(buf, self.leader_epoch);
60 }
61 if version >= 0 {
62 if flex {
63 let () = put_compact_string(buf, &self.metadata);
64 } else {
65 let () = put_string(buf, &self.metadata);
66 }
67 }
68 if version >= 0 {
69 put_i64(buf, self.commit_timestamp);
70 }
71 if version == 1 {
72 put_i64(buf, self.expire_timestamp);
73 }
74 if flex {
75 let mut tagged = WriteTaggedFields::new();
76 if !(crate::codegen_helpers::is_default(&self.topic_id)) {
77 let payload = encode_to_bytes(16, |b| {
78 crate::primitives::uuid::put_uuid(b, self.topic_id);
79 Ok(())
80 });
81 tagged.add(0, payload);
82 }
83 tagged.write(buf, &self.unknown_tagged_fields);
84 }
85 Ok(())
86 }
87 fn encoded_len(&self, version: i16) -> usize {
88 let flex = is_flexible(version);
89 let mut n: usize = 0;
90 if version >= 0 {
91 n += 8;
92 }
93 if version >= 3 {
94 n += 4;
95 }
96 if version >= 0 {
97 n += if flex {
98 compact_string_len(&self.metadata)
99 } else {
100 string_len(&self.metadata)
101 };
102 }
103 if version >= 0 {
104 n += 8;
105 }
106 if version == 1 {
107 n += 8;
108 }
109 if flex {
110 let mut known_pairs: Vec<(u32, usize)> = Vec::new();
111 if !(crate::codegen_helpers::is_default(&self.topic_id)) {
112 known_pairs.push((0, 16));
113 }
114 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
115 }
116 n
117 }
118}
119impl Decode<'_> for OffsetCommitValue {
120 fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
121 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
122 return Err(ProtocolError::SchemaMismatch(
123 "OffsetCommitValue version out of range",
124 ));
125 }
126 let flex = is_flexible(version);
127 let mut out = Self::default();
128 if version >= 0 {
129 out.offset = get_i64(buf)?;
130 }
131 if version >= 3 {
132 out.leader_epoch = get_i32(buf)?;
133 }
134 if version >= 0 {
135 out.metadata = if flex {
136 get_compact_string_owned(buf)?
137 } else {
138 get_string_owned(buf)?
139 };
140 }
141 if version >= 0 {
142 out.commit_timestamp = get_i64(buf)?;
143 }
144 if version == 1 {
145 out.expire_timestamp = get_i64(buf)?;
146 }
147 if flex {
148 let mut tag_topic_id = None;
149 out.unknown_tagged_fields = read_tagged_fields(buf, |tag, payload| match tag {
150 0 => {
151 tag_topic_id = Some({
152 let b: &mut &[u8] = payload;
153 crate::primitives::uuid::get_uuid(b)?
154 });
155 Ok(true)
156 }
157 _ => Ok(false),
158 })?;
159 if let Some(v) = tag_topic_id {
160 out.topic_id = v;
161 }
162 }
163 Ok(out)
164 }
165}
166#[cfg(test)]
167impl OffsetCommitValue {
168 #[must_use]
169 pub fn populated(version: i16) -> Self {
170 let mut m = Self::default();
171 if version >= 0 {
172 m.offset = 1i64;
173 }
174 if version >= 3 {
175 m.leader_epoch = 1i32;
176 }
177 if version >= 0 {
178 m.metadata = "x".to_string();
179 }
180 if version >= 0 {
181 m.commit_timestamp = 1i64;
182 }
183 if version == 1 {
184 m.expire_timestamp = 1i64;
185 }
186 if version >= 4 {
187 m.topic_id = crate::primitives::uuid::Uuid([1u8; 16]);
188 }
189 m
190 }
191}
192#[must_use]
195#[allow(unused_comparisons)]
196pub fn default_json(version: i16) -> ::serde_json::Value {
197 let mut obj = ::serde_json::Map::new();
198 obj.insert("offset".to_string(), ::serde_json::json!(0));
199 if version >= 3 {
200 obj.insert("leaderEpoch".to_string(), ::serde_json::json!(-1));
201 }
202 obj.insert(
203 "metadata".to_string(),
204 ::serde_json::Value::String(String::new()),
205 );
206 obj.insert("commitTimestamp".to_string(), ::serde_json::json!(0));
207 if version == 1 {
208 obj.insert("expireTimestamp".to_string(), ::serde_json::json!(-1));
209 }
210 if version >= 4 {
211 obj.insert(
212 "topicId".to_string(),
213 ::serde_json::Value::String("AAAAAAAAAAAAAAAAAAAAAA".to_string()),
214 );
215 }
216 ::serde_json::Value::Object(obj)
217}
218#[must_use]
221pub fn tagged_fixture() -> OffsetCommitValue {
222 OffsetCommitValue {
223 topic_id: crate::primitives::uuid::Uuid([1u8; 16]),
224 ..OffsetCommitValue::default()
225 }
226}
227#[must_use]
230pub fn tagged_fixture_json(version: i16) -> ::serde_json::Value {
231 let mut m = ::serde_json::Map::new();
232 m.insert("offset".to_string(), ::serde_json::json!(0));
233 if version >= 3 {
234 m.insert("leaderEpoch".to_string(), ::serde_json::json!(-1));
235 }
236 m.insert(
237 "metadata".to_string(),
238 ::serde_json::Value::String(String::new()),
239 );
240 m.insert("commitTimestamp".to_string(), ::serde_json::json!(0));
241 if version == 1 {
242 m.insert("expireTimestamp".to_string(), ::serde_json::json!(-1));
243 }
244 if version == 4 {
245 m.insert(
246 "topicId".to_string(),
247 ::serde_json::Value::String("AQEBAQEBAQEBAQEBAQEBAQ".to_string()),
248 );
249 }
250 ::serde_json::Value::Object(m)
251}