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