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.
2use crate::primitives::fixed::{get_i64, put_i64};
3use crate::primitives::string_bytes::{put_bytes, put_compact_bytes};
4use crate::primitives::string_bytes_borrowed::{get_bytes_borrowed, get_compact_bytes_borrowed};
5use crate::tagged_fields::{WriteTaggedFields, read_tagged_fields, tagged_fields_len};
6use crate::{DecodeBorrow, Encode, ProtocolError, UnknownTaggedFields};
7use bytes::{BufMut, Bytes};
8pub const API_KEY: i16 = 39;
9pub const MIN_VERSION: i16 = 1;
10pub const MAX_VERSION: i16 = 2;
11pub const FLEXIBLE_MIN: i16 = 2;
12#[inline]
13fn is_flexible(version: i16) -> bool {
14    version >= FLEXIBLE_MIN
15}
16#[derive(Debug, Clone, PartialEq, Eq, Default)]
17pub struct RenewDelegationTokenRequest<'a> {
18    pub hmac: &'a [u8],
19    pub renew_period_ms: i64,
20    pub unknown_tagged_fields: UnknownTaggedFields,
21}
22impl RenewDelegationTokenRequest<'_> {
23    pub fn to_owned(
24        &self,
25    ) -> crate::owned::renew_delegation_token_request::RenewDelegationTokenRequest {
26        crate::owned::renew_delegation_token_request::RenewDelegationTokenRequest {
27            hmac: Bytes::copy_from_slice(self.hmac),
28            renew_period_ms: (self.renew_period_ms),
29            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
30        }
31    }
32}
33impl Encode for RenewDelegationTokenRequest<'_> {
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            if flex {
44                put_compact_bytes(buf, self.hmac);
45            } else {
46                put_bytes(buf, self.hmac);
47            }
48        }
49        if version >= 0 {
50            put_i64(buf, self.renew_period_ms);
51        }
52        if flex {
53            let tagged = WriteTaggedFields::new();
54            tagged.write(buf, &self.unknown_tagged_fields);
55        }
56        Ok(())
57    }
58    fn encoded_len(&self, version: i16) -> usize {
59        let flex = is_flexible(version);
60        let mut n: usize = 0;
61        if version >= 0 {
62            n += if flex {
63                crate::primitives::varint::uvarint_len(
64                    u32::try_from((self.hmac).len() + 1).unwrap(),
65                ) + (self.hmac).len()
66            } else {
67                4 + (self.hmac).len()
68            };
69        }
70        if version >= 0 {
71            n += 8;
72        }
73        if flex {
74            let known_pairs: Vec<(u32, usize)> = Vec::new();
75            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
76        }
77        n
78    }
79}
80impl<'de> DecodeBorrow<'de> for RenewDelegationTokenRequest<'de> {
81    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
82        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
83            return Err(ProtocolError::UnsupportedVersion {
84                api_key: API_KEY,
85                version,
86            });
87        }
88        let flex = is_flexible(version);
89        let mut out = Self::default();
90        if version >= 0 {
91            out.hmac = if flex {
92                get_compact_bytes_borrowed(buf)?
93            } else {
94                get_bytes_borrowed(buf)?
95            };
96        }
97        if version >= 0 {
98            out.renew_period_ms = get_i64(buf)?;
99        }
100        if flex {
101            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
102        }
103        Ok(out)
104    }
105}
106#[cfg(test)]
107impl RenewDelegationTokenRequest<'_> {
108    #[must_use]
109    pub fn populated(version: i16) -> Self {
110        let mut m = Self::default();
111        if version >= 0 {
112            m.hmac = &b"x"[..];
113        }
114        if version >= 0 {
115            m.renew_period_ms = 1i64;
116        }
117        m
118    }
119}