use bytes::Bytes;
use nom::IResult;
use nombytes::NomBytes;
use crate::{
error::{Error, KafkaCode, Result},
parser,
protocol::{parse_header_response, HeaderResponse},
};
#[derive(Debug, PartialEq)]
pub struct SaslHandshakeResponse {
pub header: HeaderResponse,
pub error_code: KafkaCode,
pub mechanisms: Vec<Bytes>,
}
impl TryFrom<Bytes> for SaslHandshakeResponse {
type Error = Error;
fn try_from(s: Bytes) -> Result<Self> {
tracing::trace!("Parsing SaslHandshakeResponse {:?}", s);
let (_, handshake) = parse_handshake_response(NomBytes::new(s.clone())).map_err(|err| {
tracing::error!("ERROR: Failed parsing SaslHandshakeResponse {:?}", err);
tracing::error!("ERROR: SaslHandshakeResponse Bytes {:?}", s);
Error::ParsingError(s)
})?;
tracing::trace!("Parsed SaslHandshakeResponse {:?}", handshake);
Ok(handshake)
}
}
pub fn parse_handshake_response(s: NomBytes) -> IResult<NomBytes, SaslHandshakeResponse> {
let (s, header) = parse_header_response(s)?;
let (s, error_code) = parser::parse_kafka_code(s)?;
let (s, mechanisms) = parser::parse_array(parser::parse_string)(s)?;
Ok((
s,
SaslHandshakeResponse {
header,
error_code,
mechanisms,
},
))
}