Skip to main content

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

1// AUTO-GENERATED by crabka-protocol-codegen against a9ce3221537b8653448750697915607dc7936cf3. Do not edit.
2
3use bytes::{BufMut, Bytes};
4
5use crate::primitives::fixed::{get_i64, put_i64};
6use crate::primitives::string_bytes::{put_bytes, put_compact_bytes};
7use crate::primitives::string_bytes_borrowed::{get_bytes_borrowed, get_compact_bytes_borrowed};
8use crate::tagged_fields::{WriteTaggedFields, read_tagged_fields, tagged_fields_len};
9use crate::{DecodeBorrow, Encode, ProtocolError, UnknownTaggedFields};
10
11pub const API_KEY: i16 = 39;
12pub const MIN_VERSION: i16 = 1;
13pub const MAX_VERSION: i16 = 2;
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 RenewDelegationTokenRequest<'a> {
23    pub hmac: &'a [u8],
24    pub renew_period_ms: i64,
25    pub unknown_tagged_fields: UnknownTaggedFields,
26}
27impl RenewDelegationTokenRequest<'_> {
28    pub fn to_owned(
29        &self,
30    ) -> crate::owned::renew_delegation_token_request::RenewDelegationTokenRequest {
31        crate::owned::renew_delegation_token_request::RenewDelegationTokenRequest {
32            hmac: Bytes::copy_from_slice(self.hmac),
33            renew_period_ms: (self.renew_period_ms),
34            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
35        }
36    }
37}
38impl Encode for RenewDelegationTokenRequest<'_> {
39    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
40        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
41            return Err(ProtocolError::UnsupportedVersion {
42                api_key: API_KEY,
43                version,
44            });
45        }
46        let flex = is_flexible(version);
47        if version >= 0 {
48            if flex {
49                put_compact_bytes(buf, self.hmac);
50            } else {
51                put_bytes(buf, self.hmac);
52            }
53        }
54        if version >= 0 {
55            put_i64(buf, self.renew_period_ms);
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 >= 0 {
67            n += if flex {
68                crate::primitives::varint::uvarint_len(
69                    u32::try_from((self.hmac).len() + 1).unwrap(),
70                ) + (self.hmac).len()
71            } else {
72                4 + (self.hmac).len()
73            };
74        }
75        if version >= 0 {
76            n += 8;
77        }
78        if flex {
79            let known_pairs: Vec<(u32, usize)> = Vec::new();
80            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
81        }
82        n
83    }
84}
85impl<'de> DecodeBorrow<'de> for RenewDelegationTokenRequest<'de> {
86    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
87        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
88            return Err(ProtocolError::UnsupportedVersion {
89                api_key: API_KEY,
90                version,
91            });
92        }
93        let flex = is_flexible(version);
94        let mut out = Self::default();
95        if version >= 0 {
96            out.hmac = if flex {
97                get_compact_bytes_borrowed(buf)?
98            } else {
99                get_bytes_borrowed(buf)?
100            };
101        }
102        if version >= 0 {
103            out.renew_period_ms = get_i64(buf)?;
104        }
105        if flex {
106            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
107        }
108        Ok(out)
109    }
110}
111#[cfg(test)]
112impl RenewDelegationTokenRequest<'_> {
113    #[must_use]
114    pub fn populated(version: i16) -> Self {
115        let mut m = Self::default();
116        if version >= 0 {
117            m.hmac = &b"x"[..];
118        }
119        if version >= 0 {
120            m.renew_period_ms = 1i64;
121        }
122        m
123    }
124}