use bytes::{Buf, BufMut};
use crate::primitives::fixed::{get_i64, put_i64};
use crate::primitives::string_bytes::{
compact_nullable_string_len, compact_string_len, get_compact_nullable_string_owned,
get_compact_string_owned, get_nullable_string_owned, get_string_owned, nullable_string_len,
put_compact_nullable_string, put_compact_string, put_nullable_string, put_string, string_len,
};
use crate::tagged_fields::{WriteTaggedFields, read_tagged_fields, tagged_fields_len};
use crate::{Decode, 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, Default)]
pub struct CreateDelegationTokenRequest {
pub owner_principal_type: Option<String>,
pub owner_principal_name: Option<String>,
pub renewers: Vec<CreatableRenewers>,
pub max_lifetime_ms: i64,
pub unknown_tagged_fields: UnknownTaggedFields,
}
impl Encode for CreateDelegationTokenRequest {
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 >= 3 {
if flex {
put_compact_nullable_string(buf, self.owner_principal_type.as_deref());
} else {
put_nullable_string(buf, self.owner_principal_type.as_deref());
}
}
if version >= 3 {
if flex {
put_compact_nullable_string(buf, self.owner_principal_name.as_deref());
} else {
put_nullable_string(buf, self.owner_principal_name.as_deref());
}
}
if version >= 0 {
{
crate::primitives::array::put_array_len(buf, (self.renewers).len(), flex);
for it in &self.renewers {
it.encode(buf, version)?;
}
}
}
if version >= 0 {
put_i64(buf, self.max_lifetime_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 >= 3 {
n += if flex {
compact_nullable_string_len(self.owner_principal_type.as_deref())
} else {
nullable_string_len(self.owner_principal_type.as_deref())
};
}
if version >= 3 {
n += if flex {
compact_nullable_string_len(self.owner_principal_name.as_deref())
} else {
nullable_string_len(self.owner_principal_name.as_deref())
};
}
if version >= 0 {
n += {
let prefix =
crate::primitives::array::array_len_prefix_len((self.renewers).len(), flex);
let body: usize = (self.renewers)
.iter()
.map(|it| it.encoded_len(version))
.sum();
prefix + body
};
}
if version >= 0 {
n += 8;
}
if flex {
let known_pairs: Vec<(u32, usize)> = Vec::new();
n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
}
n
}
}
impl Decode<'_> for CreateDelegationTokenRequest {
fn decode<B: Buf>(buf: &mut B, 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 >= 3 {
out.owner_principal_type = if flex {
get_compact_nullable_string_owned(buf)?
} else {
get_nullable_string_owned(buf)?
};
}
if version >= 3 {
out.owner_principal_name = if flex {
get_compact_nullable_string_owned(buf)?
} else {
get_nullable_string_owned(buf)?
};
}
if version >= 0 {
out.renewers = {
let n = crate::primitives::array::get_array_len(buf, flex)?;
let mut v = Vec::with_capacity(n);
for _ in 0..n {
v.push(CreatableRenewers::decode(buf, version)?);
}
v
};
}
if version >= 0 {
out.max_lifetime_ms = get_i64(buf)?;
}
if flex {
out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
}
Ok(out)
}
}
#[cfg(test)]
impl CreateDelegationTokenRequest {
#[must_use]
pub fn populated(version: i16) -> Self {
let mut m = Self::default();
if version >= 3 {
m.owner_principal_type = Some("x".to_string());
}
if version >= 3 {
m.owner_principal_name = Some("x".to_string());
}
if version >= 0 {
m.renewers = vec![CreatableRenewers::populated(version)];
}
if version >= 0 {
m.max_lifetime_ms = 1i64;
}
m
}
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct CreatableRenewers {
pub principal_type: String,
pub principal_name: String,
pub unknown_tagged_fields: UnknownTaggedFields,
}
impl Encode for CreatableRenewers {
fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
let flex = version >= 2;
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 flex {
let tagged = WriteTaggedFields::new();
tagged.write(buf, &self.unknown_tagged_fields);
}
Ok(())
}
fn encoded_len(&self, version: i16) -> usize {
let flex = version >= 2;
let mut n: usize = 0;
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 flex {
let known_pairs: Vec<(u32, usize)> = Vec::new();
n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
}
n
}
}
impl Decode<'_> for CreatableRenewers {
fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
let flex = version >= 2;
let mut out = Self::default();
if version >= 0 {
out.principal_type = if flex {
get_compact_string_owned(buf)?
} else {
get_string_owned(buf)?
};
}
if version >= 0 {
out.principal_name = if flex {
get_compact_string_owned(buf)?
} else {
get_string_owned(buf)?
};
}
if flex {
out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
}
Ok(out)
}
}
#[cfg(test)]
impl CreatableRenewers {
#[must_use]
pub fn populated(version: i16) -> Self {
let mut m = Self::default();
if version >= 0 {
m.principal_type = "x".to_string();
}
if version >= 0 {
m.principal_name = "x".to_string();
}
m
}
}
#[must_use]
#[allow(unused_comparisons)]
pub fn default_json(version: i16) -> ::serde_json::Value {
let mut obj = ::serde_json::Map::new();
if version >= 3 {
obj.insert("ownerPrincipalType".to_string(), ::serde_json::Value::Null);
}
if version >= 3 {
obj.insert("ownerPrincipalName".to_string(), ::serde_json::Value::Null);
}
obj.insert("renewers".to_string(), ::serde_json::Value::Array(vec![]));
obj.insert("maxLifetimeMs".to_string(), ::serde_json::json!(0));
::serde_json::Value::Object(obj)
}
impl crate::ProtocolRequest for CreateDelegationTokenRequest {
const API_KEY: i16 = API_KEY;
const MIN_VERSION: i16 = MIN_VERSION;
const MAX_VERSION: i16 = MAX_VERSION;
const FLEXIBLE_MIN: i16 = FLEXIBLE_MIN;
type Response = super::create_delegation_token_response::CreateDelegationTokenResponse;
}