Skip to main content

crabka_protocol/opt/rustwide/workdir/generated/
InitProducerIdRequest.owned.rs

1// AUTO-GENERATED by crabka-protocol-codegen against a9ce3221537b8653448750697915607dc7936cf3. Do not edit.
2
3use crate::primitives::fixed::{
4    get_bool, get_i16, get_i32, get_i64, put_bool, put_i16, put_i32, put_i64,
5};
6use crate::primitives::string_bytes::{
7    compact_nullable_string_len, get_compact_nullable_string_owned, get_nullable_string_owned,
8    nullable_string_len, put_compact_nullable_string, put_nullable_string,
9};
10use crate::tagged_fields::{WriteTaggedFields, read_tagged_fields, tagged_fields_len};
11use crate::{Decode, Encode, ProtocolError, UnknownTaggedFields};
12use bytes::{Buf, BufMut};
13pub const API_KEY: i16 = 22;
14pub const MIN_VERSION: i16 = 0;
15pub const MAX_VERSION: i16 = 6;
16pub const FLEXIBLE_MIN: i16 = 2;
17#[inline]
18fn is_flexible(version: i16) -> bool {
19    version >= FLEXIBLE_MIN
20}
21#[derive(Debug, Clone, PartialEq, Eq)]
22pub struct InitProducerIdRequest {
23    pub transactional_id: Option<String>,
24    pub transaction_timeout_ms: i32,
25    pub producer_id: i64,
26    pub producer_epoch: i16,
27    pub enable2_pc: bool,
28    pub keep_prepared_txn: bool,
29    pub unknown_tagged_fields: UnknownTaggedFields,
30}
31impl Default for InitProducerIdRequest {
32    fn default() -> Self {
33        Self {
34            transactional_id: None,
35            transaction_timeout_ms: 0i32,
36            producer_id: -1i64,
37            producer_epoch: -1i16,
38            enable2_pc: false,
39            keep_prepared_txn: false,
40            unknown_tagged_fields: Default::default(),
41        }
42    }
43}
44impl Encode for InitProducerIdRequest {
45    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
46        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
47            return Err(ProtocolError::UnsupportedVersion {
48                api_key: API_KEY,
49                version,
50            });
51        }
52        let flex = is_flexible(version);
53        if version >= 0 {
54            if flex {
55                put_compact_nullable_string(buf, self.transactional_id.as_deref());
56            } else {
57                put_nullable_string(buf, self.transactional_id.as_deref());
58            }
59        }
60        if version >= 0 {
61            put_i32(buf, self.transaction_timeout_ms);
62        }
63        if version >= 3 {
64            put_i64(buf, self.producer_id);
65        }
66        if version >= 3 {
67            put_i16(buf, self.producer_epoch);
68        }
69        if version >= 6 {
70            put_bool(buf, self.enable2_pc);
71        }
72        if version >= 6 {
73            put_bool(buf, self.keep_prepared_txn);
74        }
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 >= 0 {
85            n += if flex {
86                compact_nullable_string_len(self.transactional_id.as_deref())
87            } else {
88                nullable_string_len(self.transactional_id.as_deref())
89            };
90        }
91        if version >= 0 {
92            n += 4;
93        }
94        if version >= 3 {
95            n += 8;
96        }
97        if version >= 3 {
98            n += 2;
99        }
100        if version >= 6 {
101            n += 1;
102        }
103        if version >= 6 {
104            n += 1;
105        }
106        if flex {
107            let known_pairs: Vec<(u32, usize)> = Vec::new();
108            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
109        }
110        n
111    }
112}
113impl Decode<'_> for InitProducerIdRequest {
114    fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
115        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
116            return Err(ProtocolError::UnsupportedVersion {
117                api_key: API_KEY,
118                version,
119            });
120        }
121        let flex = is_flexible(version);
122        let mut out = Self::default();
123        if version >= 0 {
124            out.transactional_id = if flex {
125                get_compact_nullable_string_owned(buf)?
126            } else {
127                get_nullable_string_owned(buf)?
128            };
129        }
130        if version >= 0 {
131            out.transaction_timeout_ms = get_i32(buf)?;
132        }
133        if version >= 3 {
134            out.producer_id = get_i64(buf)?;
135        }
136        if version >= 3 {
137            out.producer_epoch = get_i16(buf)?;
138        }
139        if version >= 6 {
140            out.enable2_pc = get_bool(buf)?;
141        }
142        if version >= 6 {
143            out.keep_prepared_txn = get_bool(buf)?;
144        }
145        if flex {
146            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
147        }
148        Ok(out)
149    }
150}
151#[cfg(test)]
152impl InitProducerIdRequest {
153    #[must_use]
154    pub fn populated(version: i16) -> Self {
155        let mut m = Self::default();
156        if version >= 0 {
157            m.transactional_id = Some("x".to_string());
158        }
159        if version >= 0 {
160            m.transaction_timeout_ms = 1i32;
161        }
162        if version >= 3 {
163            m.producer_id = 1i64;
164        }
165        if version >= 3 {
166            m.producer_epoch = 1i16;
167        }
168        if version >= 6 {
169            m.enable2_pc = true;
170        }
171        if version >= 6 {
172            m.keep_prepared_txn = true;
173        }
174        m
175    }
176}
177/// Default JSON payload matching `Self::default()` for JVM oracle differential testing.
178/// Only includes fields valid for the given version.
179#[must_use]
180#[allow(unused_comparisons)]
181pub fn default_json(version: i16) -> ::serde_json::Value {
182    let mut obj = ::serde_json::Map::new();
183    obj.insert("transactionalId".to_string(), ::serde_json::Value::Null);
184    obj.insert("transactionTimeoutMs".to_string(), ::serde_json::json!(0));
185    if version >= 3 {
186        obj.insert("producerId".to_string(), ::serde_json::json!(-1));
187    }
188    if version >= 3 {
189        obj.insert("producerEpoch".to_string(), ::serde_json::json!(-1));
190    }
191    if version >= 6 {
192        obj.insert("enable2Pc".to_string(), ::serde_json::Value::Bool(false));
193    }
194    if version >= 6 {
195        obj.insert(
196            "keepPreparedTxn".to_string(),
197            ::serde_json::Value::Bool(false),
198        );
199    }
200    ::serde_json::Value::Object(obj)
201}
202impl crate::ProtocolRequest for InitProducerIdRequest {
203    const API_KEY: i16 = API_KEY;
204    const MIN_VERSION: i16 = MIN_VERSION;
205    const MAX_VERSION: i16 = MAX_VERSION;
206    const FLEXIBLE_MIN: i16 = FLEXIBLE_MIN;
207    type Response = super::init_producer_id_response::InitProducerIdResponse;
208}