Skip to main content

crabka_protocol/opt/rustwide/workdir/generated/
DescribeDelegationTokenRequest.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 = 41;
12pub const MIN_VERSION: i16 = 1;
13pub const MAX_VERSION: i16 = 3;
14pub const FLEXIBLE_MIN: i16 = 2;
15
16#[inline]
17fn is_flexible(version: i16) -> bool {
18    version >= FLEXIBLE_MIN
19}
20
21#[derive(Debug, Clone, PartialEq, Eq, Default)]
22pub struct DescribeDelegationTokenRequest<'a> {
23    pub owners: Option<Vec<DescribeDelegationTokenOwner<'a>>>,
24    pub unknown_tagged_fields: UnknownTaggedFields,
25}
26impl DescribeDelegationTokenRequest<'_> {
27    pub fn to_owned(
28        &self,
29    ) -> crate::owned::describe_delegation_token_request::DescribeDelegationTokenRequest {
30        crate::owned::describe_delegation_token_request::DescribeDelegationTokenRequest {
31            owners: (self.owners).as_ref().map(|v| {
32                v.iter()
33                    .map(DescribeDelegationTokenOwner::to_owned)
34                    .collect()
35            }),
36            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
37        }
38    }
39}
40impl Encode for DescribeDelegationTokenRequest<'_> {
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 >= 0 {
50            {
51                let len = (self.owners).as_ref().map(Vec::len);
52                crate::primitives::array::put_nullable_array_len(buf, len, flex);
53                if let Some(v) = &self.owners {
54                    for it in v {
55                        it.encode(buf, version)?;
56                    }
57                }
58            }
59        }
60        if flex {
61            let tagged = WriteTaggedFields::new();
62            tagged.write(buf, &self.unknown_tagged_fields);
63        }
64        Ok(())
65    }
66    fn encoded_len(&self, version: i16) -> usize {
67        let flex = is_flexible(version);
68        let mut n: usize = 0;
69        if version >= 0 {
70            n += {
71                let opt: Option<&Vec<_>> = (self.owners).as_ref();
72                let prefix = crate::primitives::array::nullable_array_len_prefix_len(
73                    opt.map(std::vec::Vec::len),
74                    flex,
75                );
76                let body: usize =
77                    opt.map_or(0, |v| v.iter().map(|it| it.encoded_len(version)).sum());
78                prefix + body
79            };
80        }
81        if flex {
82            let known_pairs: Vec<(u32, usize)> = Vec::new();
83            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
84        }
85        n
86    }
87}
88impl<'de> DecodeBorrow<'de> for DescribeDelegationTokenRequest<'de> {
89    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
90        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
91            return Err(ProtocolError::UnsupportedVersion {
92                api_key: API_KEY,
93                version,
94            });
95        }
96        let flex = is_flexible(version);
97        let mut out = Self::default();
98        if version >= 0 {
99            out.owners = {
100                let opt = crate::primitives::array::get_nullable_array_len(buf, flex)?;
101                match opt {
102                    None => None,
103                    Some(n) => {
104                        let mut v = Vec::with_capacity(n);
105                        for _ in 0..n {
106                            v.push(DescribeDelegationTokenOwner::decode_borrow(buf, version)?);
107                        }
108                        Some(v)
109                    }
110                }
111            };
112        }
113        if flex {
114            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
115        }
116        Ok(out)
117    }
118}
119#[cfg(test)]
120impl DescribeDelegationTokenRequest<'_> {
121    #[must_use]
122    pub fn populated(version: i16) -> Self {
123        let mut m = Self::default();
124        if version >= 0 {
125            m.owners = Some(vec![DescribeDelegationTokenOwner::populated(version)]);
126        }
127        m
128    }
129}
130#[derive(Debug, Clone, PartialEq, Eq, Default)]
131pub struct DescribeDelegationTokenOwner<'a> {
132    pub principal_type: &'a str,
133    pub principal_name: &'a str,
134    pub unknown_tagged_fields: UnknownTaggedFields,
135}
136impl DescribeDelegationTokenOwner<'_> {
137    pub fn to_owned(
138        &self,
139    ) -> crate::owned::describe_delegation_token_request::DescribeDelegationTokenOwner {
140        crate::owned::describe_delegation_token_request::DescribeDelegationTokenOwner {
141            principal_type: (self.principal_type).to_string(),
142            principal_name: (self.principal_name).to_string(),
143            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
144        }
145    }
146}
147impl Encode for DescribeDelegationTokenOwner<'_> {
148    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
149        let flex = version >= 2;
150        if version >= 0 {
151            if flex {
152                put_compact_string(buf, self.principal_type);
153            } else {
154                put_string(buf, self.principal_type);
155            }
156        }
157        if version >= 0 {
158            if flex {
159                put_compact_string(buf, self.principal_name);
160            } else {
161                put_string(buf, self.principal_name);
162            }
163        }
164        if flex {
165            let tagged = WriteTaggedFields::new();
166            tagged.write(buf, &self.unknown_tagged_fields);
167        }
168        Ok(())
169    }
170    fn encoded_len(&self, version: i16) -> usize {
171        let flex = version >= 2;
172        let mut n: usize = 0;
173        if version >= 0 {
174            n += if flex {
175                compact_string_len(self.principal_type)
176            } else {
177                string_len(self.principal_type)
178            };
179        }
180        if version >= 0 {
181            n += if flex {
182                compact_string_len(self.principal_name)
183            } else {
184                string_len(self.principal_name)
185            };
186        }
187        if flex {
188            let known_pairs: Vec<(u32, usize)> = Vec::new();
189            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
190        }
191        n
192    }
193}
194impl<'de> DecodeBorrow<'de> for DescribeDelegationTokenOwner<'de> {
195    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
196        let flex = version >= 2;
197        let mut out = Self::default();
198        if version >= 0 {
199            out.principal_type = if flex {
200                get_compact_string_borrowed(buf)?
201            } else {
202                get_string_borrowed(buf)?
203            };
204        }
205        if version >= 0 {
206            out.principal_name = if flex {
207                get_compact_string_borrowed(buf)?
208            } else {
209                get_string_borrowed(buf)?
210            };
211        }
212        if flex {
213            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
214        }
215        Ok(out)
216    }
217}
218#[cfg(test)]
219impl DescribeDelegationTokenOwner<'_> {
220    #[must_use]
221    pub fn populated(version: i16) -> Self {
222        let mut m = Self::default();
223        if version >= 0 {
224            m.principal_type = "x";
225        }
226        if version >= 0 {
227            m.principal_name = "x";
228        }
229        m
230    }
231}