async_coap/send_desc/
emit.rs

1// Copyright 2019 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
15
16use super::*;
17use crate::message::OwnedImmutableMessage;
18
19impl<SD: SendDescUnicast> SendDescUnicast for EmitAnyResponse<SD> {}
20impl<SD: SendDescMulticast> SendDescMulticast for EmitAnyResponse<SD> {}
21
22/// Combinator for Send Descriptors created by [`SendDescExt::emit_any_response`].
23#[derive(Debug)]
24pub struct EmitAnyResponse<SD> {
25    pub(super) inner: SD,
26}
27
28impl<SD> EmitAnyResponse<SD> {
29    pub(super) fn new(inner: SD) -> EmitAnyResponse<SD> {
30        EmitAnyResponse { inner }
31    }
32}
33
34impl<SD, IC> SendDesc<IC, OwnedImmutableMessage> for EmitAnyResponse<SD>
35where
36    SD: SendDesc<IC, ()> + Send,
37    IC: InboundContext,
38{
39    send_desc_passthru_timing!(inner);
40    send_desc_passthru_options!(inner);
41    send_desc_passthru_payload!(inner);
42    send_desc_passthru_supports_option!(inner);
43
44    fn handler(
45        &mut self,
46        context: Result<&IC, Error>,
47    ) -> Result<ResponseStatus<OwnedImmutableMessage>, Error> {
48        let msg = context.ok().map(|x| x.message());
49
50        match (self.inner.handler(context), msg) {
51            (_, Some(msg)) => Ok(ResponseStatus::Done(msg.to_owned())),
52            (Ok(ResponseStatus::SendNext), None) => Ok(ResponseStatus::SendNext),
53            (Ok(ResponseStatus::Continue), None) => Ok(ResponseStatus::Continue),
54            (Ok(ResponseStatus::Done(())), None) => unreachable!(),
55            (Err(e), None) => Err(e),
56        }
57    }
58}
59
60/// Combinator for Send Descriptors created by [`SendDescExt::emit_successful_response`].
61#[derive(Debug)]
62pub struct EmitSuccessfulResponse<SD> {
63    pub(super) inner: SD,
64}
65
66impl<SD> EmitSuccessfulResponse<SD> {
67    pub(super) fn new(inner: SD) -> EmitSuccessfulResponse<SD> {
68        EmitSuccessfulResponse { inner }
69    }
70}
71
72impl<SD, IC> SendDesc<IC, OwnedImmutableMessage> for EmitSuccessfulResponse<SD>
73where
74    SD: SendDesc<IC, ()> + Send,
75    IC: InboundContext,
76{
77    send_desc_passthru_timing!(inner);
78    send_desc_passthru_options!(inner);
79    send_desc_passthru_payload!(inner);
80    send_desc_passthru_supports_option!(inner);
81
82    fn handler(
83        &mut self,
84        context: Result<&IC, Error>,
85    ) -> Result<ResponseStatus<OwnedImmutableMessage>, Error> {
86        let msg = context.ok().map(|x| x.message());
87
88        match (self.inner.handler(context), msg) {
89            (Err(e), _) => Err(e),
90            (_, Some(msg)) => Ok(ResponseStatus::Done(msg.to_owned())),
91            (Ok(ResponseStatus::SendNext), None) => Ok(ResponseStatus::SendNext),
92            (Ok(ResponseStatus::Continue), None) => Ok(ResponseStatus::Continue),
93            (Ok(ResponseStatus::Done(())), None) => unreachable!(),
94        }
95    }
96}
97
98impl<SD: SendDescUnicast> SendDescUnicast for EmitMsgCode<SD> {}
99impl<SD: SendDescMulticast> SendDescMulticast for EmitMsgCode<SD> {}
100
101/// Combinator for Send Descriptors created by [`SendDescExt::emit_msg_code`].
102#[derive(Debug)]
103pub struct EmitMsgCode<SD> {
104    pub(super) inner: SD,
105}
106
107impl<SD> EmitMsgCode<SD> {
108    pub(super) fn new(inner: SD) -> EmitMsgCode<SD> {
109        EmitMsgCode { inner }
110    }
111}
112
113impl<SD, IC> SendDesc<IC, MsgCode> for EmitMsgCode<SD>
114where
115    SD: SendDesc<IC, ()> + Send,
116    IC: InboundContext,
117{
118    send_desc_passthru_timing!(inner);
119    send_desc_passthru_options!(inner);
120    send_desc_passthru_payload!(inner);
121    send_desc_passthru_supports_option!(inner);
122
123    fn handler(&mut self, context: Result<&IC, Error>) -> Result<ResponseStatus<MsgCode>, Error> {
124        let msg_code = context.ok().map(|x| x.message().msg_code());
125
126        self.inner.handler(context).map(|x| match (x, msg_code) {
127            (ResponseStatus::SendNext, _) => ResponseStatus::SendNext,
128            (_, Some(msg)) => ResponseStatus::Done(msg.to_owned()),
129            (_, _) => unreachable!(),
130        })
131    }
132}