use bytes::Bytes;
use nom::{number::complete::be_i64, IResult};
use nombytes::NomBytes;
use crate::{
error::{Error, KafkaCode, Result},
parser,
protocol::{parse_header_response, HeaderResponse},
};
#[derive(Debug, PartialEq)]
pub struct SaslAuthenticationResponse {
pub header: HeaderResponse,
pub error_code: KafkaCode,
pub error_message: Option<Bytes>,
pub auth_bytes: Bytes,
pub session_lifetime_ms: i64,
}
impl TryFrom<Bytes> for SaslAuthenticationResponse {
type Error = Error;
fn try_from(s: Bytes) -> Result<Self> {
tracing::trace!("Parsing SaslAuthenticationResponse {:?}", s);
let (_, authenticate) =
parse_authenticate_response(NomBytes::new(s.clone())).map_err(|err| {
tracing::error!("ERROR: Failed parsing SaslAuthenticationResponse {:?}", err);
tracing::error!("ERROR: SaslAuthenticationResponse Bytes {:?}", s);
Error::ParsingError(s)
})?;
tracing::trace!("Parsed SaslAuthenticationResponse {:?}", authenticate);
Ok(authenticate)
}
}
pub fn parse_authenticate_response(s: NomBytes) -> IResult<NomBytes, SaslAuthenticationResponse> {
let (s, header) = parse_header_response(s)?;
let (s, error_code) = parser::parse_kafka_code(s)?;
let (s, error_message) = parser::parse_nullable_string(s)?;
let (s, auth_bytes) = parser::parse_bytes(s)?;
let (s, session_lifetime_ms) = be_i64(s)?;
Ok((
s,
SaslAuthenticationResponse {
header,
error_code,
error_message,
auth_bytes,
session_lifetime_ms,
},
))
}