use super::*;
use crate::message::OwnedImmutableMessage;
impl<SD: SendDescUnicast> SendDescUnicast for EmitAnyResponse<SD> {}
impl<SD: SendDescMulticast> SendDescMulticast for EmitAnyResponse<SD> {}
#[derive(Debug)]
pub struct EmitAnyResponse<SD> {
pub(super) inner: SD,
}
impl<SD> EmitAnyResponse<SD> {
pub(super) fn new(inner: SD) -> EmitAnyResponse<SD> {
EmitAnyResponse { inner }
}
}
impl<SD, IC> SendDesc<IC, OwnedImmutableMessage> for EmitAnyResponse<SD>
where
SD: SendDesc<IC, ()> + Send,
IC: InboundContext,
{
send_desc_passthru_timing!(inner);
send_desc_passthru_options!(inner);
send_desc_passthru_payload!(inner);
send_desc_passthru_supports_option!(inner);
fn handler(
&mut self,
context: Result<&IC, Error>,
) -> Result<ResponseStatus<OwnedImmutableMessage>, Error> {
let msg = context.ok().map(|x| x.message());
match (self.inner.handler(context), msg) {
(_, Some(msg)) => Ok(ResponseStatus::Done(msg.to_owned())),
(Ok(ResponseStatus::SendNext), None) => Ok(ResponseStatus::SendNext),
(Ok(ResponseStatus::Continue), None) => Ok(ResponseStatus::Continue),
(Ok(ResponseStatus::Done(())), None) => unreachable!(),
(Err(e), None) => Err(e),
}
}
}
#[derive(Debug)]
pub struct EmitSuccessfulResponse<SD> {
pub(super) inner: SD,
}
impl<SD> EmitSuccessfulResponse<SD> {
pub(super) fn new(inner: SD) -> EmitSuccessfulResponse<SD> {
EmitSuccessfulResponse { inner }
}
}
impl<SD, IC> SendDesc<IC, OwnedImmutableMessage> for EmitSuccessfulResponse<SD>
where
SD: SendDesc<IC, ()> + Send,
IC: InboundContext,
{
send_desc_passthru_timing!(inner);
send_desc_passthru_options!(inner);
send_desc_passthru_payload!(inner);
send_desc_passthru_supports_option!(inner);
fn handler(
&mut self,
context: Result<&IC, Error>,
) -> Result<ResponseStatus<OwnedImmutableMessage>, Error> {
let msg = context.ok().map(|x| x.message());
match (self.inner.handler(context), msg) {
(Err(e), _) => Err(e),
(_, Some(msg)) => Ok(ResponseStatus::Done(msg.to_owned())),
(Ok(ResponseStatus::SendNext), None) => Ok(ResponseStatus::SendNext),
(Ok(ResponseStatus::Continue), None) => Ok(ResponseStatus::Continue),
(Ok(ResponseStatus::Done(())), None) => unreachable!(),
}
}
}
impl<SD: SendDescUnicast> SendDescUnicast for EmitMsgCode<SD> {}
impl<SD: SendDescMulticast> SendDescMulticast for EmitMsgCode<SD> {}
#[derive(Debug)]
pub struct EmitMsgCode<SD> {
pub(super) inner: SD,
}
impl<SD> EmitMsgCode<SD> {
pub(super) fn new(inner: SD) -> EmitMsgCode<SD> {
EmitMsgCode { inner }
}
}
impl<SD, IC> SendDesc<IC, MsgCode> for EmitMsgCode<SD>
where
SD: SendDesc<IC, ()> + Send,
IC: InboundContext,
{
send_desc_passthru_timing!(inner);
send_desc_passthru_options!(inner);
send_desc_passthru_payload!(inner);
send_desc_passthru_supports_option!(inner);
fn handler(&mut self, context: Result<&IC, Error>) -> Result<ResponseStatus<MsgCode>, Error> {
let msg_code = context.ok().map(|x| x.message().msg_code());
self.inner.handler(context).map(|x| match (x, msg_code) {
(ResponseStatus::SendNext, _) => ResponseStatus::SendNext,
(_, Some(msg)) => ResponseStatus::Done(msg.to_owned()),
(_, _) => unreachable!(),
})
}
}