Skip to main content

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