Skip to main content

krabka_protocol/opt/rustwide/workdir/generated/
RequestHeader.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_i16, get_i32, put_i16, put_i32},
8        string_bytes::{
9            compact_nullable_string_len, nullable_string_len, put_compact_nullable_string,
10            put_nullable_string,
11        },
12        string_bytes_borrowed::{
13            get_compact_nullable_string_borrowed, get_nullable_string_borrowed,
14        },
15    },
16    tagged_fields::{WriteTaggedFields, read_tagged_fields, tagged_fields_len},
17};
18pub const MIN_VERSION: i16 = 1;
19pub const MAX_VERSION: i16 = 2;
20pub const FLEXIBLE_MIN: i16 = 2;
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 RequestHeader<'a> {
28    pub request_api_key: i16,
29    pub request_api_version: i16,
30    pub correlation_id: i32,
31    pub client_id: Option<&'a str>,
32    pub unknown_tagged_fields: UnknownTaggedFields,
33}
34impl Default for RequestHeader<'_> {
35    fn default() -> Self {
36        Self {
37            request_api_key: 0i16,
38            request_api_version: 0i16,
39            correlation_id: 0i32,
40            client_id: Some(""),
41            unknown_tagged_fields: UnknownTaggedFields::default(),
42        }
43    }
44}
45impl RequestHeader<'_> {
46    /// # Panics
47    ///
48    /// Panics if a records field contains an invalid encoded record batch.
49    #[must_use]
50    pub fn to_owned(&self) -> crate::owned::request_header::RequestHeader {
51        crate::owned::request_header::RequestHeader {
52            request_api_key: (self.request_api_key),
53            request_api_version: (self.request_api_version),
54            correlation_id: (self.correlation_id),
55            client_id: (self.client_id).map(std::string::ToString::to_string),
56            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
57        }
58    }
59}
60impl Encode for RequestHeader<'_> {
61    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
62        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
63            return Err(ProtocolError::SchemaMismatch(
64                "RequestHeader version out of range",
65            ));
66        }
67        let flex = is_flexible(version);
68        if version >= 0 {
69            put_i16(buf, self.request_api_key);
70        }
71        if version >= 0 {
72            put_i16(buf, self.request_api_version);
73        }
74        if version >= 0 {
75            put_i32(buf, self.correlation_id);
76        }
77        if version >= 1 {
78            {
79                let flex = false;
80                if flex {
81                    let () = put_compact_nullable_string(buf, self.client_id);
82                } else {
83                    let () = put_nullable_string(buf, self.client_id);
84                }
85            };
86        }
87        if flex {
88            let tagged = WriteTaggedFields::new();
89            tagged.write(buf, &self.unknown_tagged_fields);
90        }
91        Ok(())
92    }
93    fn encoded_len(&self, version: i16) -> usize {
94        let flex = is_flexible(version);
95        let mut n: usize = 0;
96        if version >= 0 {
97            n += 2;
98        }
99        if version >= 0 {
100            n += 2;
101        }
102        if version >= 0 {
103            n += 4;
104        }
105        if version >= 1 {
106            n += {
107                let flex = false;
108                if flex {
109                    compact_nullable_string_len(self.client_id)
110                } else {
111                    nullable_string_len(self.client_id)
112                }
113            };
114        }
115        if flex {
116            let known_pairs: Vec<(u32, usize)> = Vec::new();
117            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
118        }
119        n
120    }
121}
122impl<'de> DecodeBorrow<'de> for RequestHeader<'de> {
123    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
124        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
125            return Err(ProtocolError::SchemaMismatch(
126                "RequestHeader version out of range",
127            ));
128        }
129        let flex = is_flexible(version);
130        let mut out = Self::default();
131        if version >= 0 {
132            out.request_api_key = get_i16(buf)?;
133        }
134        if version >= 0 {
135            out.request_api_version = get_i16(buf)?;
136        }
137        if version >= 0 {
138            out.correlation_id = get_i32(buf)?;
139        }
140        if version >= 1 {
141            out.client_id = {
142                let flex = false;
143                if flex {
144                    get_compact_nullable_string_borrowed(buf)?
145                } else {
146                    get_nullable_string_borrowed(buf)?
147                }
148            };
149        }
150        if flex {
151            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
152        }
153        Ok(out)
154    }
155}
156#[cfg(test)]
157impl RequestHeader<'_> {
158    #[must_use]
159    pub fn populated(version: i16) -> Self {
160        let mut m = Self::default();
161        if version >= 0 {
162            m.request_api_key = 1i16;
163        }
164        if version >= 0 {
165            m.request_api_version = 1i16;
166        }
167        if version >= 0 {
168            m.correlation_id = 1i32;
169        }
170        if version >= 1 {
171            m.client_id = Some("x");
172        }
173        m
174    }
175}