Skip to main content

crabka_protocol/opt/rustwide/workdir/generated/
ListGroupsRequest.borrowed.rs

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