Skip to main content

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

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