Skip to main content

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

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