coap_server/app/request.rs
1use coap_lite::{CoapRequest, CoapResponse, RequestType, ResponseType};
2
3use crate::app::response::Response;
4
5/// Wrapper type for CoAP requests carrying extra context from filtering. This API is not
6/// considered stable and in particular the inclusion of the [`CoapRequest`] reference is
7/// likely to be removed in future versions when matching/filtering is more robust.
8#[derive(Debug, Clone)]
9pub struct Request<Endpoint> {
10 pub original: CoapRequest<Endpoint>,
11 pub unmatched_path: Vec<String>,
12}
13
14impl<Endpoint> Request<Endpoint> {
15 /// Construct a new, but incomplete, [`Response`] instance that is appropriately paired
16 /// with the request. For example, the response token is set-up to match the request.
17 ///
18 /// Note that after the response is returned no further associated with the request can be
19 /// guaranteed and callers are free to override these values, thus potentially violating
20 /// the CoAP protocol spec.
21 pub fn new_response(&self) -> Response {
22 let mut response = CoapResponse::new(&self.original.message)
23 .expect("cannot get a Request without an input message that can make a Response!");
24 response.message.payload = Vec::new();
25 let default_code = match &self.original.get_method() {
26 RequestType::Get => ResponseType::Content,
27 RequestType::Post => ResponseType::Created,
28 RequestType::Put => ResponseType::Changed,
29 // Err, I'm actually not sure what these should be yet, need to check the RFC!
30 _ => ResponseType::Valid,
31 };
32 response.set_status(default_code);
33 response
34 }
35}