use bytes::{Buf, BufMut};
use crate::primitives::fixed::{get_i8, get_i32, put_i8, put_i32};
use crate::primitives::string_bytes::{
bytes_len, compact_bytes_len, get_bytes_owned, get_compact_bytes_owned, put_bytes,
put_compact_bytes,
};
use crate::primitives::string_bytes::{
compact_string_len, get_compact_string_owned, get_string_owned, put_compact_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 = 51;
pub const MIN_VERSION: i16 = 0;
pub const MAX_VERSION: i16 = 0;
pub const FLEXIBLE_MIN: i16 = 0;
#[inline]
fn is_flexible(version: i16) -> bool {
version >= FLEXIBLE_MIN
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct AlterUserScramCredentialsRequest {
pub deletions: Vec<ScramCredentialDeletion>,
pub upsertions: Vec<ScramCredentialUpsertion>,
pub unknown_tagged_fields: UnknownTaggedFields,
}
impl Encode for AlterUserScramCredentialsRequest {
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 {
{
crate::primitives::array::put_array_len(buf, (self.deletions).len(), flex);
for it in &self.deletions {
it.encode(buf, version)?;
}
}
}
if version >= 0 {
{
crate::primitives::array::put_array_len(buf, (self.upsertions).len(), flex);
for it in &self.upsertions {
it.encode(buf, version)?;
}
}
}
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 += {
let prefix =
crate::primitives::array::array_len_prefix_len((self.deletions).len(), flex);
let body: usize = (self.deletions)
.iter()
.map(|it| it.encoded_len(version))
.sum();
prefix + body
};
}
if version >= 0 {
n += {
let prefix =
crate::primitives::array::array_len_prefix_len((self.upsertions).len(), flex);
let body: usize = (self.upsertions)
.iter()
.map(|it| it.encoded_len(version))
.sum();
prefix + body
};
}
if flex {
let known_pairs: Vec<(u32, usize)> = Vec::new();
n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
}
n
}
}
impl Decode<'_> for AlterUserScramCredentialsRequest {
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 >= 0 {
out.deletions = {
let n = crate::primitives::array::get_array_len(buf, flex)?;
let mut v = Vec::with_capacity(n);
for _ in 0..n {
v.push(ScramCredentialDeletion::decode(buf, version)?);
}
v
};
}
if version >= 0 {
out.upsertions = {
let n = crate::primitives::array::get_array_len(buf, flex)?;
let mut v = Vec::with_capacity(n);
for _ in 0..n {
v.push(ScramCredentialUpsertion::decode(buf, version)?);
}
v
};
}
if flex {
out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
}
Ok(out)
}
}
#[cfg(test)]
impl AlterUserScramCredentialsRequest {
#[must_use]
pub fn populated(version: i16) -> Self {
let mut m = Self::default();
if version >= 0 {
m.deletions = vec![ScramCredentialDeletion::populated(version)];
}
if version >= 0 {
m.upsertions = vec![ScramCredentialUpsertion::populated(version)];
}
m
}
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct ScramCredentialDeletion {
pub name: String,
pub mechanism: i8,
pub unknown_tagged_fields: UnknownTaggedFields,
}
impl Encode for ScramCredentialDeletion {
fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
let flex = version >= 0;
if version >= 0 {
if flex {
put_compact_string(buf, &self.name);
} else {
put_string(buf, &self.name);
}
}
if version >= 0 {
put_i8(buf, self.mechanism);
}
if flex {
let tagged = WriteTaggedFields::new();
tagged.write(buf, &self.unknown_tagged_fields);
}
Ok(())
}
fn encoded_len(&self, version: i16) -> usize {
let flex = version >= 0;
let mut n: usize = 0;
if version >= 0 {
n += if flex {
compact_string_len(&self.name)
} else {
string_len(&self.name)
};
}
if version >= 0 {
n += 1;
}
if flex {
let known_pairs: Vec<(u32, usize)> = Vec::new();
n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
}
n
}
}
impl Decode<'_> for ScramCredentialDeletion {
fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
let flex = version >= 0;
let mut out = Self::default();
if version >= 0 {
out.name = if flex {
get_compact_string_owned(buf)?
} else {
get_string_owned(buf)?
};
}
if version >= 0 {
out.mechanism = get_i8(buf)?;
}
if flex {
out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
}
Ok(out)
}
}
#[cfg(test)]
impl ScramCredentialDeletion {
#[must_use]
pub fn populated(version: i16) -> Self {
let mut m = Self::default();
if version >= 0 {
m.name = "x".to_string();
}
if version >= 0 {
m.mechanism = 1i8;
}
m
}
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct ScramCredentialUpsertion {
pub name: String,
pub mechanism: i8,
pub iterations: i32,
pub salt: ::bytes::Bytes,
pub salted_password: ::bytes::Bytes,
pub unknown_tagged_fields: UnknownTaggedFields,
}
impl Encode for ScramCredentialUpsertion {
fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
let flex = version >= 0;
if version >= 0 {
if flex {
put_compact_string(buf, &self.name);
} else {
put_string(buf, &self.name);
}
}
if version >= 0 {
put_i8(buf, self.mechanism);
}
if version >= 0 {
put_i32(buf, self.iterations);
}
if version >= 0 {
if flex {
put_compact_bytes(buf, &self.salt);
} else {
put_bytes(buf, &self.salt);
}
}
if version >= 0 {
if flex {
put_compact_bytes(buf, &self.salted_password);
} else {
put_bytes(buf, &self.salted_password);
}
}
if flex {
let tagged = WriteTaggedFields::new();
tagged.write(buf, &self.unknown_tagged_fields);
}
Ok(())
}
fn encoded_len(&self, version: i16) -> usize {
let flex = version >= 0;
let mut n: usize = 0;
if version >= 0 {
n += if flex {
compact_string_len(&self.name)
} else {
string_len(&self.name)
};
}
if version >= 0 {
n += 1;
}
if version >= 0 {
n += 4;
}
if version >= 0 {
n += if flex {
compact_bytes_len(&self.salt)
} else {
bytes_len(&self.salt)
};
}
if version >= 0 {
n += if flex {
compact_bytes_len(&self.salted_password)
} else {
bytes_len(&self.salted_password)
};
}
if flex {
let known_pairs: Vec<(u32, usize)> = Vec::new();
n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
}
n
}
}
impl Decode<'_> for ScramCredentialUpsertion {
fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
let flex = version >= 0;
let mut out = Self::default();
if version >= 0 {
out.name = if flex {
get_compact_string_owned(buf)?
} else {
get_string_owned(buf)?
};
}
if version >= 0 {
out.mechanism = get_i8(buf)?;
}
if version >= 0 {
out.iterations = get_i32(buf)?;
}
if version >= 0 {
out.salt = if flex {
get_compact_bytes_owned(buf)?
} else {
get_bytes_owned(buf)?
};
}
if version >= 0 {
out.salted_password = if flex {
get_compact_bytes_owned(buf)?
} else {
get_bytes_owned(buf)?
};
}
if flex {
out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
}
Ok(out)
}
}
#[cfg(test)]
impl ScramCredentialUpsertion {
#[must_use]
pub fn populated(version: i16) -> Self {
let mut m = Self::default();
if version >= 0 {
m.name = "x".to_string();
}
if version >= 0 {
m.mechanism = 1i8;
}
if version >= 0 {
m.iterations = 1i32;
}
if version >= 0 {
m.salt = ::bytes::Bytes::from_static(b"x");
}
if version >= 0 {
m.salted_password = ::bytes::Bytes::from_static(b"x");
}
m
}
}
#[must_use]
#[allow(unused_comparisons)]
pub fn default_json(version: i16) -> ::serde_json::Value {
let mut obj = ::serde_json::Map::new();
obj.insert("deletions".to_string(), ::serde_json::Value::Array(vec![]));
obj.insert("upsertions".to_string(), ::serde_json::Value::Array(vec![]));
::serde_json::Value::Object(obj)
}
impl crate::ProtocolRequest for AlterUserScramCredentialsRequest {
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::alter_user_scram_credentials_response::AlterUserScramCredentialsResponse;
}