Skip to main content

krabka_protocol/opt/rustwide/workdir/generated/
OffsetCommitValue.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    codegen_helpers::is_default,
8    primitives::{
9        fixed::{get_i32, get_i64, put_i32, put_i64},
10        string_bytes::{
11            compact_string_len, get_compact_string_owned, get_string_owned, put_compact_string,
12            put_string, string_len,
13        },
14        uuid::{Uuid, get_uuid, put_uuid},
15    },
16    tagged_fields::{WriteTaggedFields, encode_to_bytes, read_tagged_fields, tagged_fields_len},
17};
18pub const MIN_VERSION: i16 = 0;
19pub const MAX_VERSION: i16 = 4;
20pub const FLEXIBLE_MIN: i16 = 4;
21#[inline]
22#[must_use]
23pub fn is_flexible(version: i16) -> bool {
24    version >= FLEXIBLE_MIN
25}
26#[derive(Debug, Clone, PartialEq, Eq)]
27pub struct OffsetCommitValue {
28    pub offset: i64,
29    pub leader_epoch: i32,
30    pub metadata: String,
31    pub commit_timestamp: i64,
32    pub expire_timestamp: i64,
33    pub topic_id: Uuid,
34    pub unknown_tagged_fields: UnknownTaggedFields,
35}
36impl Default for OffsetCommitValue {
37    fn default() -> Self {
38        Self {
39            offset: 0i64,
40            leader_epoch: -1i32,
41            metadata: String::new(),
42            commit_timestamp: 0i64,
43            expire_timestamp: -1i64,
44            topic_id: Uuid::default(),
45            unknown_tagged_fields: UnknownTaggedFields::default(),
46        }
47    }
48}
49impl Encode for OffsetCommitValue {
50    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
51        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
52            return Err(ProtocolError::SchemaMismatch(
53                "OffsetCommitValue version out of range",
54            ));
55        }
56        let flex = is_flexible(version);
57        if version >= 0 {
58            put_i64(buf, self.offset);
59        }
60        if version >= 3 {
61            put_i32(buf, self.leader_epoch);
62        }
63        if version >= 0 {
64            if flex {
65                put_compact_string(buf, &self.metadata)?;
66            } else {
67                put_string(buf, &self.metadata)?;
68            }
69        }
70        if version >= 0 {
71            put_i64(buf, self.commit_timestamp);
72        }
73        if version == 1 {
74            put_i64(buf, self.expire_timestamp);
75        }
76        if flex {
77            let mut tagged = WriteTaggedFields::new();
78            if !(is_default(&self.topic_id)) {
79                let payload = encode_to_bytes(16, |b| {
80                    put_uuid(b, self.topic_id);
81                    Ok(())
82                })?;
83                tagged.add(0, payload);
84            }
85            tagged.write(buf, &self.unknown_tagged_fields)?;
86        }
87        Ok(())
88    }
89    fn encoded_len(&self, version: i16) -> usize {
90        let flex = is_flexible(version);
91        let mut n: usize = 0;
92        if version >= 0 {
93            n += 8;
94        }
95        if version >= 3 {
96            n += 4;
97        }
98        if version >= 0 {
99            n += if flex {
100                compact_string_len(&self.metadata)
101            } else {
102                string_len(&self.metadata)
103            };
104        }
105        if version >= 0 {
106            n += 8;
107        }
108        if version == 1 {
109            n += 8;
110        }
111        if flex {
112            let mut known_pairs: Vec<(u32, usize)> = Vec::new();
113            if !(is_default(&self.topic_id)) {
114                known_pairs.push((0, 16));
115            }
116            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
117        }
118        n
119    }
120}
121impl Decode<'_> for OffsetCommitValue {
122    fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
123        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
124            return Err(ProtocolError::SchemaMismatch(
125                "OffsetCommitValue version out of range",
126            ));
127        }
128        let flex = is_flexible(version);
129        let mut out = Self::default();
130        if version >= 0 {
131            out.offset = get_i64(buf)?;
132        }
133        if version >= 3 {
134            out.leader_epoch = get_i32(buf)?;
135        }
136        if version >= 0 {
137            out.metadata = if flex {
138                get_compact_string_owned(buf)?
139            } else {
140                get_string_owned(buf)?
141            };
142        }
143        if version >= 0 {
144            out.commit_timestamp = get_i64(buf)?;
145        }
146        if version == 1 {
147            out.expire_timestamp = get_i64(buf)?;
148        }
149        if flex {
150            let mut tag_topic_id = None;
151            out.unknown_tagged_fields = read_tagged_fields(buf, |tag, payload| match tag {
152                0 => {
153                    tag_topic_id = Some({
154                        let b: &mut &[u8] = payload;
155                        get_uuid(b)?
156                    });
157                    Ok(true)
158                }
159                _ => Ok(false),
160            })?;
161            if let Some(v) = tag_topic_id {
162                out.topic_id = v;
163            }
164        }
165        Ok(out)
166    }
167}
168#[cfg(test)]
169impl OffsetCommitValue {
170    #[must_use]
171    pub fn populated(version: i16) -> Self {
172        let mut m = Self::default();
173        if version >= 0 {
174            m.offset = 1i64;
175        }
176        if version >= 3 {
177            m.leader_epoch = 1i32;
178        }
179        if version >= 0 {
180            m.metadata = "x".to_owned();
181        }
182        if version >= 0 {
183            m.commit_timestamp = 1i64;
184        }
185        if version == 1 {
186            m.expire_timestamp = 1i64;
187        }
188        if version >= 4 {
189            m.topic_id = Uuid([1u8; 16]);
190        }
191        m
192    }
193}
194/// Default JSON payload matching `Self::default()` for JVM oracle differential testing.
195/// Only includes fields valid for the given version.
196#[must_use]
197#[allow(unused_comparisons)]
198pub fn default_json(version: i16) -> ::serde_json::Value {
199    let mut obj = ::serde_json::Map::new();
200    obj.insert("offset".to_owned(), ::serde_json::json!(0));
201    if version >= 3 {
202        obj.insert("leaderEpoch".to_owned(), ::serde_json::json!(-1));
203    }
204    obj.insert(
205        "metadata".to_owned(),
206        ::serde_json::Value::String(String::new()),
207    );
208    obj.insert("commitTimestamp".to_owned(), ::serde_json::json!(0));
209    if version == 1 {
210        obj.insert("expireTimestamp".to_owned(), ::serde_json::json!(-1));
211    }
212    if version >= 4 {
213        obj.insert(
214            "topicId".to_owned(),
215            ::serde_json::Value::String("AAAAAAAAAAAAAAAAAAAAAA".to_owned()),
216        );
217    }
218    ::serde_json::Value::Object(obj)
219}
220/// A message with a non-default value in every tagged field, at every depth, for
221/// the JVM oracle differential sweep. The value is the same at every version.
222#[must_use]
223pub fn tagged_fixture() -> OffsetCommitValue {
224    OffsetCommitValue {
225        topic_id: Uuid([1u8; 16]),
226        ..OffsetCommitValue::default()
227    }
228}
229/// The JSON form of `tagged_fixture()` that Kafka's JSON converter reads at
230/// `version`. It holds only the fields that the schema declares at that version.
231#[must_use]
232pub fn tagged_fixture_json(version: i16) -> ::serde_json::Value {
233    let mut m = ::serde_json::Map::new();
234    m.insert("offset".to_owned(), ::serde_json::json!(0));
235    if version >= 3 {
236        m.insert("leaderEpoch".to_owned(), ::serde_json::json!(-1));
237    }
238    m.insert(
239        "metadata".to_owned(),
240        ::serde_json::Value::String(String::new()),
241    );
242    m.insert("commitTimestamp".to_owned(), ::serde_json::json!(0));
243    if version == 1 {
244        m.insert("expireTimestamp".to_owned(), ::serde_json::json!(-1));
245    }
246    if version == 4 {
247        m.insert(
248            "topicId".to_owned(),
249            ::serde_json::Value::String("AQEBAQEBAQEBAQEBAQEBAQ".to_owned()),
250        );
251    }
252    ::serde_json::Value::Object(m)
253}