use rama::{Context, Service};
use tansu_sans_io::{ApiKey, Frame, Header, LeaveGroupRequest};
use tracing::instrument;
use crate::{Error, Result, coordinator::group::Coordinator};
#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct LeaveGroupService;
impl ApiKey for LeaveGroupService {
const KEY: i16 = LeaveGroupRequest::KEY;
}
impl<C> Service<C, Frame> for LeaveGroupService
where
C: Coordinator,
{
type Response = Frame;
type Error = Error;
#[instrument(skip(ctx, req))]
async fn serve(&self, mut ctx: Context<C>, req: Frame) -> Result<Self::Response, Self::Error> {
let correlation_id = req.correlation_id()?;
let coordinator = ctx.state_mut();
let leave = LeaveGroupRequest::try_from(req.body)?;
coordinator
.leave(
leave.group_id.as_str(),
leave.member_id.as_deref(),
leave.members.as_deref(),
)
.await
.map(|body| Frame {
size: 0,
header: Header::Response { correlation_id },
body,
})
}
}