Skip to main content

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

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