use bytes::{Bytes, BufMut};
use crate::primitives::fixed::{get_i16, get_i32, get_i64, put_i16, put_i32, put_i64};
use crate::primitives::string_bytes::{
compact_string_len, put_compact_string, put_string, string_len,
};
use crate::primitives::string_bytes_borrowed::{
get_compact_string_borrowed, get_string_borrowed,
};
use crate::primitives::string_bytes::{put_bytes, put_compact_bytes};
use crate::primitives::string_bytes_borrowed::{get_bytes_borrowed, get_compact_bytes_borrowed};
use crate::tagged_fields::{read_tagged_fields, tagged_fields_len, WriteTaggedFields};
use crate::{DecodeBorrow, Encode, ProtocolError, UnknownTaggedFields};
pub const API_KEY: i16 = 38;
pub const MIN_VERSION: i16 = 1;
pub const MAX_VERSION: i16 = 3;
pub const FLEXIBLE_MIN: i16 = 2;
#[inline]
fn is_flexible(version: i16) -> bool { version >= FLEXIBLE_MIN }
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CreateDelegationTokenResponse<'a> {
pub error_code: i16,
pub principal_type: &'a str,
pub principal_name: &'a str,
pub token_requester_principal_type: &'a str,
pub token_requester_principal_name: &'a str,
pub issue_timestamp_ms: i64,
pub expiry_timestamp_ms: i64,
pub max_timestamp_ms: i64,
pub token_id: &'a str,
pub hmac: &'a [u8],
pub throttle_time_ms: i32,
pub unknown_tagged_fields: UnknownTaggedFields,
}
impl<'a> Default for CreateDelegationTokenResponse<'a> {
fn default() -> Self {
Self {
error_code: 0i16,
principal_type: "",
principal_name: "",
token_requester_principal_type: "",
token_requester_principal_name: "",
issue_timestamp_ms: 0i64,
expiry_timestamp_ms: 0i64,
max_timestamp_ms: 0i64,
token_id: "",
hmac: &[],
throttle_time_ms: 0i32,
unknown_tagged_fields: Default::default(),
}
}
}
impl<'a> CreateDelegationTokenResponse<'a> {
pub fn to_owned(&self) -> crate::owned::create_delegation_token_response::CreateDelegationTokenResponse {
crate::owned::create_delegation_token_response::CreateDelegationTokenResponse {
error_code: (self.error_code),
principal_type: (self.principal_type).to_string(),
principal_name: (self.principal_name).to_string(),
token_requester_principal_type: (self.token_requester_principal_type).to_string(),
token_requester_principal_name: (self.token_requester_principal_name).to_string(),
issue_timestamp_ms: (self.issue_timestamp_ms),
expiry_timestamp_ms: (self.expiry_timestamp_ms),
max_timestamp_ms: (self.max_timestamp_ms),
token_id: (self.token_id).to_string(),
hmac: Bytes::copy_from_slice(self.hmac),
throttle_time_ms: (self.throttle_time_ms),
unknown_tagged_fields: self.unknown_tagged_fields.clone(),
}
}
}
impl<'a> Encode for CreateDelegationTokenResponse<'a> {
fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
return Err(ProtocolError::UnsupportedVersion { api_key: API_KEY, version });
}
let flex = is_flexible(version);
if version >= 0 { put_i16(buf, self.error_code) }
if version >= 0 { if flex { put_compact_string(buf, self.principal_type) } else { put_string(buf, self.principal_type) } }
if version >= 0 { if flex { put_compact_string(buf, self.principal_name) } else { put_string(buf, self.principal_name) } }
if version >= 3 { if flex { put_compact_string(buf, self.token_requester_principal_type) } else { put_string(buf, self.token_requester_principal_type) } }
if version >= 3 { if flex { put_compact_string(buf, self.token_requester_principal_name) } else { put_string(buf, self.token_requester_principal_name) } }
if version >= 0 { put_i64(buf, self.issue_timestamp_ms) }
if version >= 0 { put_i64(buf, self.expiry_timestamp_ms) }
if version >= 0 { put_i64(buf, self.max_timestamp_ms) }
if version >= 0 { if flex { put_compact_string(buf, self.token_id) } else { put_string(buf, self.token_id) } }
if version >= 0 { if flex { put_compact_bytes(buf, self.hmac) } else { put_bytes(buf, self.hmac) } }
if version >= 0 { put_i32(buf, self.throttle_time_ms) }
if flex {
let tagged = WriteTaggedFields::new();
tagged.write(buf, &self.unknown_tagged_fields);
}
Ok(())
}
fn encoded_len(&self, version: i16) -> usize {
let flex = is_flexible(version);
let mut n: usize = 0;
if version >= 0 { n += 2; }
if version >= 0 { n += if flex { compact_string_len(self.principal_type) } else { string_len(self.principal_type) }; }
if version >= 0 { n += if flex { compact_string_len(self.principal_name) } else { string_len(self.principal_name) }; }
if version >= 3 { n += if flex { compact_string_len(self.token_requester_principal_type) } else { string_len(self.token_requester_principal_type) }; }
if version >= 3 { n += if flex { compact_string_len(self.token_requester_principal_name) } else { string_len(self.token_requester_principal_name) }; }
if version >= 0 { n += 8; }
if version >= 0 { n += 8; }
if version >= 0 { n += 8; }
if version >= 0 { n += if flex { compact_string_len(self.token_id) } else { string_len(self.token_id) }; }
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() }; }
if version >= 0 { n += 4; }
if flex {
let known_pairs: Vec<(u32, usize)> = Vec::new();
n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
}
n
}
}
impl<'de> DecodeBorrow<'de> for CreateDelegationTokenResponse<'de> {
fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
return Err(ProtocolError::UnsupportedVersion { api_key: API_KEY, version });
}
let flex = is_flexible(version);
let mut out = Self::default();
if version >= 0 { out.error_code = get_i16(buf)?; }
if version >= 0 { out.principal_type = if flex { get_compact_string_borrowed(buf)? } else { get_string_borrowed(buf)? }; }
if version >= 0 { out.principal_name = if flex { get_compact_string_borrowed(buf)? } else { get_string_borrowed(buf)? }; }
if version >= 3 { out.token_requester_principal_type = if flex { get_compact_string_borrowed(buf)? } else { get_string_borrowed(buf)? }; }
if version >= 3 { out.token_requester_principal_name = if flex { get_compact_string_borrowed(buf)? } else { get_string_borrowed(buf)? }; }
if version >= 0 { out.issue_timestamp_ms = get_i64(buf)?; }
if version >= 0 { out.expiry_timestamp_ms = get_i64(buf)?; }
if version >= 0 { out.max_timestamp_ms = get_i64(buf)?; }
if version >= 0 { out.token_id = if flex { get_compact_string_borrowed(buf)? } else { get_string_borrowed(buf)? }; }
if version >= 0 { out.hmac = if flex { get_compact_bytes_borrowed(buf)? } else { get_bytes_borrowed(buf)? }; }
if version >= 0 { out.throttle_time_ms = get_i32(buf)?; }
if flex {
out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
Ok(false)
})?;
}
Ok(out)
}
}