async_coap/send_desc/
request.rs1use super::*;
17
18#[derive(Debug)]
20pub enum CoapRequest {}
21
22impl CoapRequest {
23 #[inline(always)]
29 pub fn get<IC>() -> SendGet<IC> {
30 Default::default()
31 }
32
33 #[inline(always)]
40 pub fn observe<IC>() -> SendObserve<IC> {
41 Default::default()
42 }
43
44 #[inline(always)]
50 pub fn post<IC>() -> SendPost<IC> {
51 Default::default()
52 }
53
54 #[inline(always)]
60 pub fn put<IC>() -> SendPut<IC> {
61 Default::default()
62 }
63
64 #[inline(always)]
70 pub fn delete<IC>() -> SendDelete<IC> {
71 Default::default()
72 }
73
74 #[inline(always)]
83 pub fn method<IC>(msg_code: MsgCode) -> CoapRequestMethod<IC> {
84 debug_assert!(msg_code.is_method(), "{:?} is not a method", msg_code);
85 CoapRequestMethod {
86 msg_code,
87 phantom: PhantomData,
88 }
89 }
90}
91
92macro_rules! send_desc_def_method {
93 ($(#[$tags:meta])* $name:ident, $code:expr, $handler:expr) => {
94 $(#[$tags])*
95 #[derive(Debug)]
96 pub struct $ name < IC > (PhantomData < IC > );
97 send_desc_def_method!(@rest ($name,$code,$handler));
98 };
99 ($name:ident, $code:expr, $handler:expr) => {
100 pub struct $ name < IC > (PhantomData < IC > );
101 send_desc_def_method!(@rest ($name,$code,$handler));
102 };
103 (@rest ($name:ident, $code:expr, $handler:expr)) => {
104 impl<IC> SendDescUnicast for $name<IC> {}
105
106 impl<IC> Default for $name<IC> {
107 #[inline(always)]
108 fn default() -> Self {
109 Self(PhantomData)
110 }
111 }
112
113 impl<IC> $name<IC> {
114 #[inline(always)]
116 pub fn nonconfirmable(self) -> Nonconfirmable<$name<IC>> {
117 Nonconfirmable(self)
118 }
119
120 #[inline(always)]
122 pub fn multicast(self) -> Multicast<$name<IC>> {
123 Multicast(self)
124 }
125 }
126
127 impl<IC: InboundContext> SendDesc<IC, ()> for $name<IC> {
128 fn write_options(
129 &self,
130 _msg: &mut dyn OptionInsert,
131 _socket_addr: &IC::SocketAddr,
132 _start: Bound<OptionNumber>,
133 _end: Bound<OptionNumber>,
134 ) -> Result<(), Error> {
135 Ok(())
136 }
137
138 fn write_payload(
139 &self,
140 msg: &mut dyn MessageWrite,
141 _socket_addr: &IC::SocketAddr,
142 ) -> Result<(), Error> {
143 msg.set_msg_code($code);
144 Ok(())
145 }
146
147 fn handler(
148 &mut self,
149 context: Result<&IC, Error>,
150 ) -> Result<ResponseStatus<()>, Error> {
151 let context = context?;
152
153 if context.is_dupe() {
154 return Ok(ResponseStatus::Continue);
156 }
157
158 let code = context.message().msg_code();
159 ($handler)(code)
160 }
161 }
162 };
163}
164
165send_desc_def_method!(
166 SendGet,
168 MsgCode::MethodGet,
169 |code| {
170 match code {
171 MsgCode::SuccessContent | MsgCode::SuccessValid => Ok(ResponseStatus::Done(())),
172 MsgCode::ClientErrorNotFound => Err(Error::ResourceNotFound),
173 MsgCode::ClientErrorForbidden => Err(Error::Forbidden),
174 MsgCode::ClientErrorUnauthorized => Err(Error::Unauthorized),
175 code if code.is_client_error() => Err(Error::ClientRequestError),
176 _ => Err(Error::ServerError),
177 }
178 }
179);
180
181send_desc_def_method!(
182 SendPut,
184 MsgCode::MethodPut,
185 |code| {
186 match code {
187 MsgCode::SuccessCreated | MsgCode::SuccessChanged | MsgCode::SuccessValid => {
188 Ok(ResponseStatus::Done(()))
189 }
190 MsgCode::ClientErrorNotFound => Err(Error::ResourceNotFound),
191 MsgCode::ClientErrorForbidden => Err(Error::Forbidden),
192 MsgCode::ClientErrorUnauthorized => Err(Error::Unauthorized),
193 code if code.is_client_error() => Err(Error::ClientRequestError),
194 _ => Err(Error::ServerError),
195 }
196 }
197);
198
199send_desc_def_method!(
200 SendPost,
202 MsgCode::MethodPost,
203 |code| {
204 match code {
205 code if code.is_success() => Ok(ResponseStatus::Done(())),
206 MsgCode::ClientErrorNotFound => Err(Error::ResourceNotFound),
207 MsgCode::ClientErrorForbidden => Err(Error::Forbidden),
208 MsgCode::ClientErrorUnauthorized => Err(Error::Unauthorized),
209 code if code.is_client_error() => Err(Error::ClientRequestError),
210 _ => Err(Error::ServerError),
211 }
212 }
213);
214
215send_desc_def_method!(
216 SendDelete,
218 MsgCode::MethodDelete,
219 |code| {
220 match code {
221 MsgCode::SuccessDeleted => Ok(ResponseStatus::Done(())),
222 MsgCode::ClientErrorNotFound => Err(Error::ResourceNotFound),
223 MsgCode::ClientErrorForbidden => Err(Error::Forbidden),
224 MsgCode::ClientErrorUnauthorized => Err(Error::Unauthorized),
225 code if code.is_client_error() => Err(Error::ClientRequestError),
226 _ => Err(Error::ServerError),
227 }
228 }
229);
230
231#[derive(Debug)]
234pub struct CoapRequestMethod<IC> {
235 msg_code: MsgCode,
236 phantom: PhantomData<IC>,
237}
238
239impl<IC> SendDescUnicast for CoapRequestMethod<IC> {}
240
241impl<IC> CoapRequestMethod<IC> {
242 #[inline(always)]
244 pub fn nonconfirmable(self) -> Nonconfirmable<CoapRequestMethod<IC>> {
245 Nonconfirmable(self)
246 }
247
248 #[inline(always)]
250 pub fn multicast(self) -> Multicast<CoapRequestMethod<IC>> {
251 Multicast(self)
252 }
253}
254
255impl<IC> SendDesc<IC, ()> for CoapRequestMethod<IC>
256where
257 IC: InboundContext,
258{
259 fn write_options(
260 &self,
261 _msg: &mut dyn OptionInsert,
262 _socket_addr: &IC::SocketAddr,
263 _start: Bound<OptionNumber>,
264 _end: Bound<OptionNumber>,
265 ) -> Result<(), Error> {
266 Ok(())
267 }
268
269 fn write_payload(
270 &self,
271 msg: &mut dyn MessageWrite,
272 _socket_addr: &IC::SocketAddr,
273 ) -> Result<(), Error> {
274 msg.set_msg_code(self.msg_code);
275 Ok(())
276 }
277
278 fn handler(&mut self, context: Result<&IC, Error>) -> Result<ResponseStatus<()>, Error> {
279 let context = context?;
280
281 if context.is_dupe() {
282 return Ok(ResponseStatus::Continue);
284 }
285
286 let code = context.message().msg_code();
287
288 match code {
289 code if code.is_success() => Ok(ResponseStatus::Done(())),
290 MsgCode::ClientErrorNotFound => Err(Error::ResourceNotFound),
291 MsgCode::ClientErrorForbidden => Err(Error::Forbidden),
292 MsgCode::ClientErrorUnauthorized => Err(Error::Unauthorized),
293 code if code.is_client_error() => Err(Error::ClientRequestError),
294 _ => Err(Error::ServerError),
295 }
296 }
297}