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::{Bytes, BufMut};
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::{read_tagged_fields, tagged_fields_len, WriteTaggedFields};
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 { version >= FLEXIBLE_MIN }
18
19#[derive(Debug, Clone, PartialEq, Eq)]
20pub struct RenewDelegationTokenRequest<'a> {
21    pub hmac: &'a [u8],
22    pub renew_period_ms: i64,
23    pub unknown_tagged_fields: UnknownTaggedFields,
24}
25
26impl<'a> Default for RenewDelegationTokenRequest<'a> {
27    fn default() -> Self {
28        Self {
29            hmac: &[],
30            renew_period_ms: 0i64,
31            unknown_tagged_fields: Default::default(),
32        }
33    }
34}
35
36impl<'a> RenewDelegationTokenRequest<'a> {
37    pub fn to_owned(&self) -> crate::owned::renew_delegation_token_request::RenewDelegationTokenRequest {
38        crate::owned::renew_delegation_token_request::RenewDelegationTokenRequest {
39            hmac: Bytes::copy_from_slice(self.hmac),
40            renew_period_ms: (self.renew_period_ms),
41            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
42        }
43    }
44}
45
46impl<'a> Encode for RenewDelegationTokenRequest<'a> {
47    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
48        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
49            return Err(ProtocolError::UnsupportedVersion { api_key: API_KEY, version });
50        }
51        let flex = is_flexible(version);
52        if version >= 0 { if flex { put_compact_bytes(buf, self.hmac) } else { put_bytes(buf, self.hmac) } }
53        if version >= 0 { put_i64(buf, self.renew_period_ms) }
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 { n += if flex { crate::primitives::varint::uvarint_len(u32::try_from((self.hmac).len() + 1).unwrap()) + (self.hmac).len() } else { 4 + (self.hmac).len() }; }
64        if version >= 0 { n += 8; }
65        if flex {
66            let known_pairs: Vec<(u32, usize)> = Vec::new();
67            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
68        }
69        n
70    }
71}
72
73impl<'de> DecodeBorrow<'de> for RenewDelegationTokenRequest<'de> {
74    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
75        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
76            return Err(ProtocolError::UnsupportedVersion { api_key: API_KEY, version });
77        }
78        let flex = is_flexible(version);
79        let mut out = Self::default();
80        if version >= 0 { out.hmac = if flex { get_compact_bytes_borrowed(buf)? } else { get_bytes_borrowed(buf)? }; }
81        if version >= 0 { out.renew_period_ms = get_i64(buf)?; }
82        if flex {
83            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
84                Ok(false)
85            })?;
86        }
87        Ok(out)
88    }
89}