crabka_protocol/opt/rustwide/workdir/generated/
BrokerRegistrationChangeRecord.owned.rs1use bytes::{Buf, BufMut};
4
5use crate::primitives::fixed::{get_i8, get_i32, get_i64, put_i8, put_i32, put_i64};
6use crate::tagged_fields::{
7 WriteTaggedFields, encode_to_bytes, read_tagged_fields, tagged_fields_len,
8};
9use crate::{Decode, Encode, ProtocolError, UnknownTaggedFields};
10pub const MIN_VERSION: i16 = 0;
11pub const MAX_VERSION: i16 = 3;
12pub const FLEXIBLE_MIN: i16 = 0;
13
14#[inline]
15fn is_flexible(version: i16) -> bool {
16 version >= FLEXIBLE_MIN
17}
18
19#[derive(Debug, Clone, PartialEq, Eq, Default)]
20pub struct BrokerRegistrationChangeRecord {
21 pub broker_id: i32,
22 pub broker_epoch: i64,
23 pub fenced: i8,
24 pub in_controlled_shutdown: i8,
25 pub log_dirs: Vec<crate::primitives::uuid::Uuid>,
26 pub cordoned_log_dirs: Option<Vec<crate::primitives::uuid::Uuid>>,
27 pub unknown_tagged_fields: UnknownTaggedFields,
28}
29impl Encode for BrokerRegistrationChangeRecord {
30 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
31 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
32 return Err(ProtocolError::SchemaMismatch(
33 "BrokerRegistrationChangeRecord version out of range",
34 ));
35 }
36 let flex = is_flexible(version);
37 if version >= 0 {
38 put_i32(buf, self.broker_id);
39 }
40 if version >= 0 {
41 put_i64(buf, self.broker_epoch);
42 }
43 if flex {
44 let mut tagged = WriteTaggedFields::new();
45 if !(crate::codegen_helpers::is_default(&self.fenced)) {
46 let payload = encode_to_bytes(1, |b| {
47 put_i8(b, self.fenced);
48 Ok(())
49 });
50 tagged.add(0, payload);
51 }
52 if !(crate::codegen_helpers::is_default(&self.in_controlled_shutdown)) {
53 let payload = encode_to_bytes(1, |b| {
54 put_i8(b, self.in_controlled_shutdown);
55 Ok(())
56 });
57 tagged.add(1, payload);
58 }
59 if !(crate::codegen_helpers::is_default(&self.log_dirs)) {
60 let payload = encode_to_bytes(
61 {
62 let prefix = crate::primitives::array::array_len_prefix_len(
63 (self.log_dirs).len(),
64 flex,
65 );
66 let body: usize = (self.log_dirs).iter().map(|_| 16).sum();
67 prefix + body
68 },
69 |b| {
70 {
71 crate::primitives::array::put_array_len(b, (self.log_dirs).len(), flex);
72 for it in &self.log_dirs {
73 crate::primitives::uuid::put_uuid(b, *it);
74 }
75 };
76 Ok(())
77 },
78 );
79 tagged.add(2, payload);
80 }
81 if !(self.cordoned_log_dirs.is_none()) {
82 let payload = encode_to_bytes(
83 {
84 let opt: Option<&Vec<_>> = (self.cordoned_log_dirs).as_ref();
85 let prefix = crate::primitives::array::nullable_array_len_prefix_len(
86 opt.map(std::vec::Vec::len),
87 flex,
88 );
89 let body: usize = opt.map_or(0, |v| v.iter().map(|_| 16).sum());
90 prefix + body
91 },
92 |b| {
93 {
94 let len = (self.cordoned_log_dirs).as_ref().map(Vec::len);
95 crate::primitives::array::put_nullable_array_len(b, len, flex);
96 if let Some(v) = &self.cordoned_log_dirs {
97 for it in v {
98 crate::primitives::uuid::put_uuid(b, *it);
99 }
100 }
101 };
102 Ok(())
103 },
104 );
105 tagged.add(3, payload);
106 }
107 tagged.write(buf, &self.unknown_tagged_fields);
108 }
109 Ok(())
110 }
111 fn encoded_len(&self, version: i16) -> usize {
112 let flex = is_flexible(version);
113 let mut n: usize = 0;
114 if version >= 0 {
115 n += 4;
116 }
117 if version >= 0 {
118 n += 8;
119 }
120 if flex {
121 let mut known_pairs: Vec<(u32, usize)> = Vec::new();
122 if !(crate::codegen_helpers::is_default(&self.fenced)) {
123 known_pairs.push((0, 1));
124 }
125 if !(crate::codegen_helpers::is_default(&self.in_controlled_shutdown)) {
126 known_pairs.push((1, 1));
127 }
128 if !(crate::codegen_helpers::is_default(&self.log_dirs)) {
129 known_pairs.push((2, {
130 let prefix =
131 crate::primitives::array::array_len_prefix_len((self.log_dirs).len(), flex);
132 let body: usize = (self.log_dirs).iter().map(|_| 16).sum();
133 prefix + body
134 }));
135 }
136 if !(self.cordoned_log_dirs.is_none()) {
137 known_pairs.push((3, {
138 let opt: Option<&Vec<_>> = (self.cordoned_log_dirs).as_ref();
139 let prefix = crate::primitives::array::nullable_array_len_prefix_len(
140 opt.map(std::vec::Vec::len),
141 flex,
142 );
143 let body: usize = opt.map_or(0, |v| v.iter().map(|_| 16).sum());
144 prefix + body
145 }));
146 }
147 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
148 }
149 n
150 }
151}
152impl Decode<'_> for BrokerRegistrationChangeRecord {
153 fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
154 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
155 return Err(ProtocolError::SchemaMismatch(
156 "BrokerRegistrationChangeRecord version out of range",
157 ));
158 }
159 let flex = is_flexible(version);
160 let mut out = Self::default();
161 if version >= 0 {
162 out.broker_id = get_i32(buf)?;
163 }
164 if version >= 0 {
165 out.broker_epoch = get_i64(buf)?;
166 }
167 if flex {
168 let mut tag_fenced = None;
169 let mut tag_in_controlled_shutdown = None;
170 let mut tag_log_dirs = None;
171 let mut tag_cordoned_log_dirs = None;
172 out.unknown_tagged_fields = read_tagged_fields(buf, |tag, payload| match tag {
173 0 => {
174 tag_fenced = Some({
175 let b: &mut &[u8] = payload;
176 get_i8(b)?
177 });
178 Ok(true)
179 }
180 1 => {
181 tag_in_controlled_shutdown = Some({
182 let b: &mut &[u8] = payload;
183 get_i8(b)?
184 });
185 Ok(true)
186 }
187 2 => {
188 tag_log_dirs = Some({
189 let b: &mut &[u8] = payload;
190 {
191 let n = crate::primitives::array::get_array_len(b, flex)?;
192 let mut v = Vec::with_capacity(n);
193 for _ in 0..n {
194 v.push(crate::primitives::uuid::get_uuid(b)?);
195 }
196 v
197 }
198 });
199 Ok(true)
200 }
201 3 => {
202 tag_cordoned_log_dirs = Some({
203 let b: &mut &[u8] = payload;
204 {
205 let opt = crate::primitives::array::get_nullable_array_len(b, flex)?;
206 match opt {
207 None => None,
208 Some(n) => {
209 let mut v = Vec::with_capacity(n);
210 for _ in 0..n {
211 v.push(crate::primitives::uuid::get_uuid(b)?);
212 }
213 Some(v)
214 }
215 }
216 }
217 });
218 Ok(true)
219 }
220 _ => Ok(false),
221 })?;
222 if let Some(v) = tag_fenced {
223 out.fenced = v;
224 }
225 if let Some(v) = tag_in_controlled_shutdown {
226 out.in_controlled_shutdown = v;
227 }
228 if let Some(v) = tag_log_dirs {
229 out.log_dirs = v;
230 }
231 if let Some(v) = tag_cordoned_log_dirs {
232 out.cordoned_log_dirs = v;
233 }
234 }
235 Ok(out)
236 }
237}
238#[cfg(test)]
239impl BrokerRegistrationChangeRecord {
240 #[must_use]
241 pub fn populated(version: i16) -> Self {
242 let mut m = Self::default();
243 if version >= 0 {
244 m.broker_id = 1i32;
245 }
246 if version >= 0 {
247 m.broker_epoch = 1i64;
248 }
249 if version >= 0 {
250 m.fenced = 1i8;
251 }
252 if version >= 1 {
253 m.in_controlled_shutdown = 1i8;
254 }
255 if version >= 2 {
256 m.log_dirs = vec![crate::primitives::uuid::Uuid([1u8; 16])];
257 }
258 if version >= 3 {
259 m.cordoned_log_dirs = Some(vec![crate::primitives::uuid::Uuid([1u8; 16])]);
260 }
261 m
262 }
263}
264
265#[must_use]
268#[allow(unused_comparisons)]
269pub fn default_json(version: i16) -> ::serde_json::Value {
270 let mut obj = ::serde_json::Map::new();
271 obj.insert("brokerId".to_string(), ::serde_json::json!(0));
272 obj.insert("brokerEpoch".to_string(), ::serde_json::json!(0));
273 obj.insert("fenced".to_string(), ::serde_json::json!(0));
274 if version >= 1 {
275 obj.insert("inControlledShutdown".to_string(), ::serde_json::json!(0));
276 }
277 if version >= 2 {
278 obj.insert("logDirs".to_string(), ::serde_json::Value::Array(vec![]));
279 }
280 if version >= 3 {
281 obj.insert("cordonedLogDirs".to_string(), ::serde_json::Value::Null);
282 }
283 ::serde_json::Value::Object(obj)
284}