Skip to main content

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

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