Skip to main content

krabka_protocol/opt/rustwide/workdir/generated/
RequestHeader.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    primitives::{
8        fixed::{get_i16, get_i32, put_i16, put_i32},
9        string_bytes::{
10            compact_nullable_string_len, get_compact_nullable_string_owned,
11            get_nullable_string_owned, nullable_string_len, put_compact_nullable_string,
12            put_nullable_string,
13        },
14    },
15    tagged_fields::{WriteTaggedFields, read_tagged_fields, tagged_fields_len},
16};
17pub const MIN_VERSION: i16 = 1;
18pub const MAX_VERSION: i16 = 2;
19pub const FLEXIBLE_MIN: i16 = 2;
20#[inline]
21#[must_use]
22pub fn is_flexible(version: i16) -> bool {
23    version >= FLEXIBLE_MIN
24}
25#[derive(Debug, Clone, PartialEq, Eq)]
26pub struct RequestHeader {
27    pub request_api_key: i16,
28    pub request_api_version: i16,
29    pub correlation_id: i32,
30    pub client_id: Option<String>,
31    pub unknown_tagged_fields: UnknownTaggedFields,
32}
33impl Default for RequestHeader {
34    fn default() -> Self {
35        Self {
36            request_api_key: 0i16,
37            request_api_version: 0i16,
38            correlation_id: 0i32,
39            client_id: Some(String::new()),
40            unknown_tagged_fields: UnknownTaggedFields::default(),
41        }
42    }
43}
44impl Encode for RequestHeader {
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::SchemaMismatch(
48                "RequestHeader version out of range",
49            ));
50        }
51        let flex = is_flexible(version);
52        if version >= 0 {
53            put_i16(buf, self.request_api_key);
54        }
55        if version >= 0 {
56            put_i16(buf, self.request_api_version);
57        }
58        if version >= 0 {
59            put_i32(buf, self.correlation_id);
60        }
61        if version >= 1 {
62            {
63                let flex = false;
64                if flex {
65                    let () = put_compact_nullable_string(buf, self.client_id.as_deref());
66                } else {
67                    let () = put_nullable_string(buf, self.client_id.as_deref());
68                }
69            };
70        }
71        if flex {
72            let tagged = WriteTaggedFields::new();
73            tagged.write(buf, &self.unknown_tagged_fields);
74        }
75        Ok(())
76    }
77    fn encoded_len(&self, version: i16) -> usize {
78        let flex = is_flexible(version);
79        let mut n: usize = 0;
80        if version >= 0 {
81            n += 2;
82        }
83        if version >= 0 {
84            n += 2;
85        }
86        if version >= 0 {
87            n += 4;
88        }
89        if version >= 1 {
90            n += {
91                let flex = false;
92                if flex {
93                    compact_nullable_string_len(self.client_id.as_deref())
94                } else {
95                    nullable_string_len(self.client_id.as_deref())
96                }
97            };
98        }
99        if flex {
100            let known_pairs: Vec<(u32, usize)> = Vec::new();
101            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
102        }
103        n
104    }
105}
106impl Decode<'_> for RequestHeader {
107    fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
108        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
109            return Err(ProtocolError::SchemaMismatch(
110                "RequestHeader version out of range",
111            ));
112        }
113        let flex = is_flexible(version);
114        let mut out = Self::default();
115        if version >= 0 {
116            out.request_api_key = get_i16(buf)?;
117        }
118        if version >= 0 {
119            out.request_api_version = get_i16(buf)?;
120        }
121        if version >= 0 {
122            out.correlation_id = get_i32(buf)?;
123        }
124        if version >= 1 {
125            out.client_id = {
126                let flex = false;
127                if flex {
128                    get_compact_nullable_string_owned(buf)?
129                } else {
130                    get_nullable_string_owned(buf)?
131                }
132            };
133        }
134        if flex {
135            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
136        }
137        Ok(out)
138    }
139}
140#[cfg(test)]
141impl RequestHeader {
142    #[must_use]
143    pub fn populated(version: i16) -> Self {
144        let mut m = Self::default();
145        if version >= 0 {
146            m.request_api_key = 1i16;
147        }
148        if version >= 0 {
149            m.request_api_version = 1i16;
150        }
151        if version >= 0 {
152            m.correlation_id = 1i32;
153        }
154        if version >= 1 {
155            m.client_id = Some("x".to_string());
156        }
157        m
158    }
159}
160/// Default JSON payload matching `Self::default()` for JVM oracle differential testing.
161/// Only includes fields valid for the given version.
162#[must_use]
163#[allow(unused_comparisons)]
164pub fn default_json(version: i16) -> ::serde_json::Value {
165    let mut obj = ::serde_json::Map::new();
166    obj.insert("requestApiKey".to_string(), ::serde_json::json!(0));
167    obj.insert("requestApiVersion".to_string(), ::serde_json::json!(0));
168    obj.insert("correlationId".to_string(), ::serde_json::json!(0));
169    if version >= 1 {
170        obj.insert(
171            "clientId".to_string(),
172            ::serde_json::Value::String(String::new()),
173        );
174    }
175    ::serde_json::Value::Object(obj)
176}