Skip to main content

krabka_protocol/opt/rustwide/workdir/generated/
OffsetCommitValue.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    primitives::{
7        fixed::{get_i32, get_i64, put_i32, put_i64},
8        string_bytes::{compact_string_len, put_compact_string, put_string, string_len},
9        string_bytes_borrowed::{get_compact_string_borrowed, get_string_borrowed},
10    },
11    tagged_fields::{WriteTaggedFields, encode_to_bytes, read_tagged_fields, tagged_fields_len},
12};
13pub const MIN_VERSION: i16 = 0;
14pub const MAX_VERSION: i16 = 4;
15pub const FLEXIBLE_MIN: i16 = 4;
16#[inline]
17#[must_use]
18pub fn is_flexible(version: i16) -> bool {
19    version >= FLEXIBLE_MIN
20}
21#[derive(Debug, Clone, PartialEq, Eq)]
22pub struct OffsetCommitValue<'a> {
23    pub offset: i64,
24    pub leader_epoch: i32,
25    pub metadata: &'a str,
26    pub commit_timestamp: i64,
27    pub expire_timestamp: i64,
28    pub topic_id: crate::primitives::uuid::Uuid,
29    pub unknown_tagged_fields: UnknownTaggedFields,
30}
31impl Default for OffsetCommitValue<'_> {
32    fn default() -> Self {
33        Self {
34            offset: 0i64,
35            leader_epoch: -1i32,
36            metadata: "",
37            commit_timestamp: 0i64,
38            expire_timestamp: -1i64,
39            topic_id: crate::primitives::uuid::Uuid::default(),
40            unknown_tagged_fields: UnknownTaggedFields::default(),
41        }
42    }
43}
44impl OffsetCommitValue<'_> {
45    /// # Panics
46    ///
47    /// Panics if a records field contains an invalid encoded record batch.
48    #[must_use]
49    pub fn to_owned(&self) -> crate::owned::offset_commit_value::OffsetCommitValue {
50        crate::owned::offset_commit_value::OffsetCommitValue {
51            offset: (self.offset),
52            leader_epoch: (self.leader_epoch),
53            metadata: (self.metadata).to_string(),
54            commit_timestamp: (self.commit_timestamp),
55            expire_timestamp: (self.expire_timestamp),
56            topic_id: (self.topic_id),
57            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
58        }
59    }
60}
61impl Encode for OffsetCommitValue<'_> {
62    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
63        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
64            return Err(ProtocolError::SchemaMismatch(
65                "OffsetCommitValue version out of range",
66            ));
67        }
68        let flex = is_flexible(version);
69        if version >= 0 {
70            put_i64(buf, self.offset);
71        }
72        if version >= 3 {
73            put_i32(buf, self.leader_epoch);
74        }
75        if version >= 0 {
76            if flex {
77                let () = put_compact_string(buf, self.metadata);
78            } else {
79                let () = put_string(buf, self.metadata);
80            }
81        }
82        if version >= 0 {
83            put_i64(buf, self.commit_timestamp);
84        }
85        if version == 1 {
86            put_i64(buf, self.expire_timestamp);
87        }
88        if flex {
89            let mut tagged = WriteTaggedFields::new();
90            if !(crate::codegen_helpers::is_default(&self.topic_id)) {
91                let payload = encode_to_bytes(16, |b| {
92                    crate::primitives::uuid::put_uuid(b, self.topic_id);
93                    Ok(())
94                });
95                tagged.add(0, payload);
96            }
97            tagged.write(buf, &self.unknown_tagged_fields);
98        }
99        Ok(())
100    }
101    fn encoded_len(&self, version: i16) -> usize {
102        let flex = is_flexible(version);
103        let mut n: usize = 0;
104        if version >= 0 {
105            n += 8;
106        }
107        if version >= 3 {
108            n += 4;
109        }
110        if version >= 0 {
111            n += if flex {
112                compact_string_len(self.metadata)
113            } else {
114                string_len(self.metadata)
115            };
116        }
117        if version >= 0 {
118            n += 8;
119        }
120        if version == 1 {
121            n += 8;
122        }
123        if flex {
124            let mut known_pairs: Vec<(u32, usize)> = Vec::new();
125            if !(crate::codegen_helpers::is_default(&self.topic_id)) {
126                known_pairs.push((0, 16));
127            }
128            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
129        }
130        n
131    }
132}
133impl<'de> DecodeBorrow<'de> for OffsetCommitValue<'de> {
134    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
135        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
136            return Err(ProtocolError::SchemaMismatch(
137                "OffsetCommitValue version out of range",
138            ));
139        }
140        let flex = is_flexible(version);
141        let mut out = Self::default();
142        if version >= 0 {
143            out.offset = get_i64(buf)?;
144        }
145        if version >= 3 {
146            out.leader_epoch = get_i32(buf)?;
147        }
148        if version >= 0 {
149            out.metadata = if flex {
150                get_compact_string_borrowed(buf)?
151            } else {
152                get_string_borrowed(buf)?
153            };
154        }
155        if version >= 0 {
156            out.commit_timestamp = get_i64(buf)?;
157        }
158        if version == 1 {
159            out.expire_timestamp = get_i64(buf)?;
160        }
161        if flex {
162            let mut tag_topic_id = None;
163            out.unknown_tagged_fields = read_tagged_fields(buf, |tag, payload| match tag {
164                0 => {
165                    tag_topic_id = Some({
166                        let b: &mut &[u8] = payload;
167                        crate::primitives::uuid::get_uuid(b)?
168                    });
169                    Ok(true)
170                }
171                _ => Ok(false),
172            })?;
173            if let Some(v) = tag_topic_id {
174                out.topic_id = v;
175            }
176        }
177        Ok(out)
178    }
179}
180#[cfg(test)]
181impl OffsetCommitValue<'_> {
182    #[must_use]
183    pub fn populated(version: i16) -> Self {
184        let mut m = Self::default();
185        if version >= 0 {
186            m.offset = 1i64;
187        }
188        if version >= 3 {
189            m.leader_epoch = 1i32;
190        }
191        if version >= 0 {
192            m.metadata = "x";
193        }
194        if version >= 0 {
195            m.commit_timestamp = 1i64;
196        }
197        if version == 1 {
198            m.expire_timestamp = 1i64;
199        }
200        if version >= 4 {
201            m.topic_id = crate::primitives::uuid::Uuid([1u8; 16]);
202        }
203        m
204    }
205}