Skip to main content

krabka_protocol/opt/rustwide/workdir/generated/
EndTxnRequest.owned.rs

1// AUTO-GENERATED by krabka-protocol-codegen against a9ce3221537b8653448750697915607dc7936cf3. Do not edit.
2
3use bytes::{Buf, BufMut};
4
5use crate::{
6    Decode, Encode, ProtocolError, UnknownTaggedFields,
7    primitives::{
8        fixed::{get_bool, get_i16, get_i64, put_bool, put_i16, 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, read_tagged_fields, tagged_fields_len},
15};
16pub const API_KEY: i16 = 26;
17pub const MIN_VERSION: i16 = 0;
18pub const MAX_VERSION: i16 = 5;
19pub const LATEST_STABLE_VERSION: i16 = 5;
20pub const FLEXIBLE_MIN: i16 = 3;
21#[inline]
22#[must_use]
23pub fn is_flexible(version: i16) -> bool {
24    version >= FLEXIBLE_MIN
25}
26#[derive(Debug, Clone, PartialEq, Eq, Default)]
27pub struct EndTxnRequest {
28    pub transactional_id: String,
29    pub producer_id: i64,
30    pub producer_epoch: i16,
31    pub committed: bool,
32    pub unknown_tagged_fields: UnknownTaggedFields,
33}
34impl Encode for EndTxnRequest {
35    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
36        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
37            return Err(ProtocolError::UnsupportedVersion {
38                api_key: API_KEY,
39                version,
40            });
41        }
42        let flex = is_flexible(version);
43        if version >= 0 {
44            if flex {
45                let () = put_compact_string(buf, &self.transactional_id);
46            } else {
47                let () = put_string(buf, &self.transactional_id);
48            }
49        }
50        if version >= 0 {
51            put_i64(buf, self.producer_id);
52        }
53        if version >= 0 {
54            put_i16(buf, self.producer_epoch);
55        }
56        if version >= 0 {
57            put_bool(buf, self.committed);
58        }
59        if flex {
60            let tagged = WriteTaggedFields::new();
61            tagged.write(buf, &self.unknown_tagged_fields);
62        }
63        Ok(())
64    }
65    fn encoded_len(&self, version: i16) -> usize {
66        let flex = is_flexible(version);
67        let mut n: usize = 0;
68        if version >= 0 {
69            n += if flex {
70                compact_string_len(&self.transactional_id)
71            } else {
72                string_len(&self.transactional_id)
73            };
74        }
75        if version >= 0 {
76            n += 8;
77        }
78        if version >= 0 {
79            n += 2;
80        }
81        if version >= 0 {
82            n += 1;
83        }
84        if flex {
85            let known_pairs: Vec<(u32, usize)> = Vec::new();
86            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
87        }
88        n
89    }
90}
91impl Decode<'_> for EndTxnRequest {
92    fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
93        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
94            return Err(ProtocolError::UnsupportedVersion {
95                api_key: API_KEY,
96                version,
97            });
98        }
99        let flex = is_flexible(version);
100        let mut out = Self::default();
101        if version >= 0 {
102            out.transactional_id = if flex {
103                get_compact_string_owned(buf)?
104            } else {
105                get_string_owned(buf)?
106            };
107        }
108        if version >= 0 {
109            out.producer_id = get_i64(buf)?;
110        }
111        if version >= 0 {
112            out.producer_epoch = get_i16(buf)?;
113        }
114        if version >= 0 {
115            out.committed = get_bool(buf)?;
116        }
117        if flex {
118            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
119        }
120        Ok(out)
121    }
122}
123#[cfg(test)]
124impl EndTxnRequest {
125    #[must_use]
126    pub fn populated(version: i16) -> Self {
127        let mut m = Self::default();
128        if version >= 0 {
129            m.transactional_id = "x".to_string();
130        }
131        if version >= 0 {
132            m.producer_id = 1i64;
133        }
134        if version >= 0 {
135            m.producer_epoch = 1i16;
136        }
137        if version >= 0 {
138            m.committed = true;
139        }
140        m
141    }
142}
143/// Default JSON payload matching `Self::default()` for JVM oracle differential testing.
144/// Only includes fields valid for the given version.
145#[must_use]
146#[allow(unused_comparisons)]
147pub fn default_json(_version: i16) -> ::serde_json::Value {
148    let mut obj = ::serde_json::Map::new();
149    obj.insert(
150        "transactionalId".to_string(),
151        ::serde_json::Value::String(String::new()),
152    );
153    obj.insert("producerId".to_string(), ::serde_json::json!(0));
154    obj.insert("producerEpoch".to_string(), ::serde_json::json!(0));
155    obj.insert("committed".to_string(), ::serde_json::Value::Bool(false));
156    ::serde_json::Value::Object(obj)
157}
158impl crate::ProtocolRequest for EndTxnRequest {
159    const API_KEY: i16 = API_KEY;
160    const MIN_VERSION: i16 = MIN_VERSION;
161    const MAX_VERSION: i16 = MAX_VERSION;
162    const LATEST_STABLE_VERSION: i16 = LATEST_STABLE_VERSION;
163    const FLEXIBLE_MIN: i16 = FLEXIBLE_MIN;
164    type Response = super::end_txn_response::EndTxnResponse;
165}