Skip to main content

krabka_protocol/opt/rustwide/workdir/generated/
ListGroupsRequest.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        string_bytes::{compact_string_len, put_compact_string, put_string, string_len},
8        string_bytes_borrowed::{get_compact_string_borrowed, get_string_borrowed},
9    },
10    tagged_fields::{WriteTaggedFields, read_tagged_fields, tagged_fields_len},
11};
12pub const API_KEY: i16 = 16;
13pub const MIN_VERSION: i16 = 0;
14pub const MAX_VERSION: i16 = 5;
15pub const LATEST_STABLE_VERSION: i16 = 5;
16pub const FLEXIBLE_MIN: i16 = 3;
17#[inline]
18#[must_use]
19pub fn is_flexible(version: i16) -> bool {
20    version >= FLEXIBLE_MIN
21}
22#[derive(Debug, Clone, PartialEq, Eq, Default)]
23pub struct ListGroupsRequest<'a> {
24    pub states_filter: Vec<&'a str>,
25    pub types_filter: Vec<&'a str>,
26    pub unknown_tagged_fields: UnknownTaggedFields,
27}
28impl ListGroupsRequest<'_> {
29    /// # Panics
30    ///
31    /// Panics if a records field contains an invalid encoded record batch.
32    #[must_use]
33    pub fn to_owned(&self) -> crate::owned::list_groups_request::ListGroupsRequest {
34        crate::owned::list_groups_request::ListGroupsRequest {
35            states_filter: (self.states_filter)
36                .iter()
37                .map(std::string::ToString::to_string)
38                .collect(),
39            types_filter: (self.types_filter)
40                .iter()
41                .map(std::string::ToString::to_string)
42                .collect(),
43            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
44        }
45    }
46}
47impl Encode for ListGroupsRequest<'_> {
48    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
49        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
50            return Err(ProtocolError::UnsupportedVersion {
51                api_key: API_KEY,
52                version,
53            });
54        }
55        let flex = is_flexible(version);
56        if version >= 4 {
57            {
58                crate::primitives::array::put_array_len(buf, (self.states_filter).len(), flex);
59                for it in &self.states_filter {
60                    if flex {
61                        let () = put_compact_string(buf, it);
62                    } else {
63                        let () = put_string(buf, it);
64                    }
65                }
66            };
67        }
68        if version >= 5 {
69            {
70                crate::primitives::array::put_array_len(buf, (self.types_filter).len(), flex);
71                for it in &self.types_filter {
72                    if flex {
73                        let () = put_compact_string(buf, it);
74                    } else {
75                        let () = put_string(buf, it);
76                    }
77                }
78            };
79        }
80        if flex {
81            let tagged = WriteTaggedFields::new();
82            tagged.write(buf, &self.unknown_tagged_fields);
83        }
84        Ok(())
85    }
86    fn encoded_len(&self, version: i16) -> usize {
87        let flex = is_flexible(version);
88        let mut n: usize = 0;
89        if version >= 4 {
90            n += {
91                let prefix = crate::primitives::array::array_len_prefix_len(
92                    (self.states_filter).len(),
93                    flex,
94                );
95                let body: usize = (self.states_filter)
96                    .iter()
97                    .map(|it| {
98                        if flex {
99                            compact_string_len(it)
100                        } else {
101                            string_len(it)
102                        }
103                    })
104                    .sum();
105                prefix + body
106            };
107        }
108        if version >= 5 {
109            n += {
110                let prefix =
111                    crate::primitives::array::array_len_prefix_len((self.types_filter).len(), flex);
112                let body: usize = (self.types_filter)
113                    .iter()
114                    .map(|it| {
115                        if flex {
116                            compact_string_len(it)
117                        } else {
118                            string_len(it)
119                        }
120                    })
121                    .sum();
122                prefix + body
123            };
124        }
125        if flex {
126            let known_pairs: Vec<(u32, usize)> = Vec::new();
127            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
128        }
129        n
130    }
131}
132impl<'de> DecodeBorrow<'de> for ListGroupsRequest<'de> {
133    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
134        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
135            return Err(ProtocolError::UnsupportedVersion {
136                api_key: API_KEY,
137                version,
138            });
139        }
140        let flex = is_flexible(version);
141        let mut out = Self::default();
142        if version >= 4 {
143            out.states_filter = {
144                let n = crate::primitives::array::get_array_len(buf, flex)?;
145                let mut v = Vec::with_capacity(n);
146                for _ in 0..n {
147                    v.push(if flex {
148                        get_compact_string_borrowed(buf)?
149                    } else {
150                        get_string_borrowed(buf)?
151                    });
152                }
153                v
154            };
155        }
156        if version >= 5 {
157            out.types_filter = {
158                let n = crate::primitives::array::get_array_len(buf, flex)?;
159                let mut v = Vec::with_capacity(n);
160                for _ in 0..n {
161                    v.push(if flex {
162                        get_compact_string_borrowed(buf)?
163                    } else {
164                        get_string_borrowed(buf)?
165                    });
166                }
167                v
168            };
169        }
170        if flex {
171            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
172        }
173        Ok(out)
174    }
175}
176#[cfg(test)]
177impl ListGroupsRequest<'_> {
178    #[must_use]
179    pub fn populated(version: i16) -> Self {
180        let mut m = Self::default();
181        if version >= 4 {
182            m.states_filter = vec!["x"];
183        }
184        if version >= 5 {
185            m.types_filter = vec!["x"];
186        }
187        m
188    }
189}