Skip to main content

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

1// AUTO-GENERATED by krabka-protocol-codegen against a9ce3221537b8653448750697915607dc7936cf3. Do not edit.
2use bytes::{BufMut, Bytes};
3
4use crate::{
5    DecodeBorrow, Encode, ProtocolError, UnknownTaggedFields,
6    owned::renew_delegation_token_request,
7    primitives::{
8        fixed::{get_i64, put_i64},
9        string_bytes::{bytes_len, compact_bytes_len, put_bytes, put_compact_bytes},
10        string_bytes_borrowed::{get_bytes_borrowed, get_compact_bytes_borrowed},
11    },
12    tagged_fields::{WriteTaggedFields, read_tagged_fields, tagged_fields_len},
13};
14pub const API_KEY: i16 = 39;
15pub const MIN_VERSION: i16 = 1;
16pub const MAX_VERSION: i16 = 2;
17pub const LATEST_STABLE_VERSION: i16 = 2;
18pub const FLEXIBLE_MIN: i16 = 2;
19#[inline]
20#[must_use]
21pub fn is_flexible(version: i16) -> bool {
22    version >= FLEXIBLE_MIN
23}
24#[derive(Debug, Clone, PartialEq, Eq, Default)]
25pub struct RenewDelegationTokenRequest<'a> {
26    pub hmac: &'a [u8],
27    pub renew_period_ms: i64,
28    pub unknown_tagged_fields: UnknownTaggedFields,
29}
30impl RenewDelegationTokenRequest<'_> {
31    #[must_use]
32    pub fn to_owned(&self) -> renew_delegation_token_request::RenewDelegationTokenRequest {
33        renew_delegation_token_request::RenewDelegationTokenRequest {
34            hmac: Bytes::copy_from_slice(self.hmac),
35            renew_period_ms: (self.renew_period_ms),
36            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
37        }
38    }
39}
40impl Encode for RenewDelegationTokenRequest<'_> {
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            if flex {
51                put_compact_bytes(buf, self.hmac)?;
52            } else {
53                put_bytes(buf, self.hmac)?;
54            }
55        }
56        if version >= 0 {
57            put_i64(buf, self.renew_period_ms);
58        }
59        if flex {
60            let tagged = WriteTaggedFields::new();
61            tagged.write(buf, &self.unknown_tagged_fields)?;
62        }
63        Ok(())
64    }
65    fn encoded_len(&self, version: i16) -> usize {
66        let flex = is_flexible(version);
67        let mut n: usize = 0;
68        if version >= 0 {
69            n += if flex {
70                compact_bytes_len(self.hmac)
71            } else {
72                bytes_len(self.hmac)
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}