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