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 bytes::{Buf, BufMut};
4
5use crate::primitives::fixed::{get_bool, get_i16, get_i32, get_i64, put_bool, put_i16, put_i32, put_i64};
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::tagged_fields::{read_tagged_fields, tagged_fields_len, WriteTaggedFields};
13use crate::{Decode, Encode, ProtocolError, UnknownTaggedFields};
14
15pub const API_KEY: i16 = 22;
16pub const MIN_VERSION: i16 = 0;
17pub const MAX_VERSION: i16 = 6;
18pub const FLEXIBLE_MIN: i16 = 2;
19
20#[inline]
21fn is_flexible(version: i16) -> bool { version >= FLEXIBLE_MIN }
22
23#[derive(Debug, Clone, PartialEq, Eq)]
24pub struct InitProducerIdRequest {
25    pub transactional_id: Option<String>,
26    pub transaction_timeout_ms: i32,
27    pub producer_id: i64,
28    pub producer_epoch: i16,
29    pub enable2_pc: bool,
30    pub keep_prepared_txn: bool,
31    pub unknown_tagged_fields: UnknownTaggedFields,
32}
33
34impl Default for InitProducerIdRequest {
35    fn default() -> Self {
36        Self {
37            transactional_id: None,
38            transaction_timeout_ms: 0i32,
39            producer_id: -1i64,
40            producer_epoch: -1i16,
41            enable2_pc: false,
42            keep_prepared_txn: false,
43            unknown_tagged_fields: Default::default(),
44        }
45    }
46}
47
48impl Encode for InitProducerIdRequest {
49    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
50        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
51            return Err(ProtocolError::UnsupportedVersion { api_key: API_KEY, version });
52        }
53        let flex = is_flexible(version);
54        if version >= 0 { if flex { put_compact_nullable_string(buf, self.transactional_id.as_deref()) } else { put_nullable_string(buf, self.transactional_id.as_deref()) } }
55        if version >= 0 { put_i32(buf, self.transaction_timeout_ms) }
56        if version >= 3 { put_i64(buf, self.producer_id) }
57        if version >= 3 { put_i16(buf, self.producer_epoch) }
58        if version >= 6 { put_bool(buf, self.enable2_pc) }
59        if version >= 6 { put_bool(buf, self.keep_prepared_txn) }
60        if flex {
61            let tagged = WriteTaggedFields::new();
62            tagged.write(buf, &self.unknown_tagged_fields);
63        }
64        Ok(())
65    }
66    fn encoded_len(&self, version: i16) -> usize {
67        let flex = is_flexible(version);
68        let mut n: usize = 0;
69        if version >= 0 { n += if flex { compact_nullable_string_len(self.transactional_id.as_deref()) } else { nullable_string_len(self.transactional_id.as_deref()) }; }
70        if version >= 0 { n += 4; }
71        if version >= 3 { n += 8; }
72        if version >= 3 { n += 2; }
73        if version >= 6 { n += 1; }
74        if version >= 6 { n += 1; }
75        if flex {
76            let known_pairs: Vec<(u32, usize)> = Vec::new();
77            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
78        }
79        n
80    }
81}
82
83impl<'de> Decode<'de> for InitProducerIdRequest {
84    fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
85        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
86            return Err(ProtocolError::UnsupportedVersion { api_key: API_KEY, version });
87        }
88        let flex = is_flexible(version);
89        let mut out = Self::default();
90        if version >= 0 { out.transactional_id = if flex { get_compact_nullable_string_owned(buf)? } else { get_nullable_string_owned(buf)? }; }
91        if version >= 0 { out.transaction_timeout_ms = get_i32(buf)?; }
92        if version >= 3 { out.producer_id = get_i64(buf)?; }
93        if version >= 3 { out.producer_epoch = get_i16(buf)?; }
94        if version >= 6 { out.enable2_pc = get_bool(buf)?; }
95        if version >= 6 { out.keep_prepared_txn = get_bool(buf)?; }
96        if flex {
97            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
98                Ok(false)
99            })?;
100        }
101        Ok(out)
102    }
103}
104
105/// Default JSON payload matching `Self::default()` for JVM oracle differential testing.
106/// Only includes fields valid for the given version.
107#[must_use]
108#[allow(unused_comparisons)]
109pub fn default_json(version: i16) -> ::serde_json::Value {
110    let mut obj = ::serde_json::Map::new();
111    obj.insert("transactionalId".to_string(), ::serde_json::Value::Null);
112    obj.insert("transactionTimeoutMs".to_string(), ::serde_json::json!(0));
113    if version >= 3 {
114        obj.insert("producerId".to_string(), ::serde_json::json!(-1));
115    }
116    if version >= 3 {
117        obj.insert("producerEpoch".to_string(), ::serde_json::json!(-1));
118    }
119    if version >= 6 {
120        obj.insert("enable2Pc".to_string(), ::serde_json::Value::Bool(false));
121    }
122    if version >= 6 {
123        obj.insert("keepPreparedTxn".to_string(), ::serde_json::Value::Bool(false));
124    }
125    ::serde_json::Value::Object(obj)
126}
127
128impl crate::ProtocolRequest for InitProducerIdRequest {
129    const API_KEY: i16 = API_KEY;
130    const MIN_VERSION: i16 = MIN_VERSION;
131    const MAX_VERSION: i16 = MAX_VERSION;
132    const FLEXIBLE_MIN: i16 = FLEXIBLE_MIN;
133    type Response = super::init_producer_id_response::InitProducerIdResponse;
134}