Skip to main content

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

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