1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/// A minimal stateless testing CoAP server interface.
use coap_message::{MinimalWritableMessage, MutableWritableMessage, ReadableMessage};

/// A CoAP request handler. This gets called by a CoAP server implementation that the handler is
/// assigned to; the server has the handler digest the request's data into a RequestData structure,
/// possibly calls estimate_length before allocating a response message, and then asks the handler to
/// populate the allocated response message with data persisted in the RequestData structure.
pub trait Handler {
    type RequestData;

    /// Error type for extract_request_data.
    ///
    /// Typical types to use here are [core::convert::Infallible] (which, due to the possibility of
    /// unknown critical CoAP options, is only practical when their presence is carried in the
    /// extracted data) or types provided by libraries (eg.
    /// [coap-handler-implementations::option_processing::BadOption]).
    type ExtractRequestError: core::fmt::Debug + coap_message::error::RenderableOnMinimal;
    /// Error type for writing response data
    ///
    /// This is generic over writable messages because the most typical errors to show up here are
    /// generated when writing to the messages. A suitable type here is `M::UnionError`.
    // FIXME could we carry an error type in here instead of the message? if so, does the same
    // apply to render?
    type BuildResponseError<M: MinimalWritableMessage>: core::fmt::Debug
        + coap_message::error::RenderableOnMinimal;

    fn extract_request_data<M: ReadableMessage>(
        &mut self,
        request: &M,
    ) -> Result<Self::RequestData, Self::ExtractRequestError>;
    fn estimate_length(&mut self, request: &Self::RequestData) -> usize;
    fn build_response<M: MutableWritableMessage>(
        &mut self,
        response: &mut M,
        request: Self::RequestData,
    ) -> Result<(), Self::BuildResponseError<M>>;
}