coap_handler_implementations/lib.rs
1//! The `coap-handler-implementations` crate provides a few convenience, example or
2//! reference implementations of the [coap-handler] interface.
3//!
4//! They range from the generic "4.04
5//! Not Found" responder up to a handler that creates a [write] formatter for GET-only resources,
6//! and even provides [block-wise transfer] that.
7//! The [TypeHandler] enables the easy creation of
8//! resource implementations with GET, PUT and POST support in CBOR format.
9//! The `HandlerBuilder` implements crude static path based routing
10//! that may suffice for some applications, and is also useful to get started quickly.
11//!
12//! [coap-handler]: https://crates.io/crates/coap-handler
13//! [write]: https://doc.rust-lang.org/core/fmt/trait.Write.html
14//! [block-wise transfer]: https://tools.ietf.org/html/rfc7959
15//!
16//! History
17//! -------
18//!
19//! This code used to be part of coap-handler, but while the interface needs to stabilize fast (as
20//! incompatible changes there propagate to implementations), the implementations still need to
21//! develop fast (but it doesn't hurt too much if one code part is using the old SimpleRenderable
22//! while another uses a newer NeverFound).
23//!
24//! Version 0.1 is what was available in coap-handler (if Cargo allowed cyclic dependencies,
25//! coap-handler would `pub use` this crate); users are encouraged to just use
26//! coap-handler-implementations directly.
27//!
28//! Options Hiding
29//! --------------
30//!
31//! A common mechanism in handlers is that handlers "consume" options. For example, the
32//! [ForkingHandler] built through [HandlerBuilder::at] "eats" the Uri-Path; likewise, an
33//! Accept-based dispatcher would consume the Accept option.
34//!
35//! This allows the handlers themselves to check for any left-over critical options and fail if
36//! they can't handle them -- without the need to mask them out there assuming (without an actual
37//! check) that prior wrappers took care of them.
38#![doc = document_features::document_features!()]
39#![no_std]
40#![cfg_attr(feature = "_nightly_docs", feature(doc_cfg))]
41#![cfg_attr(
42 feature = "_nontrivial_option_processing",
43 feature(impl_trait_in_assoc_type)
44)]
45
46use coap_message::{MinimalWritableMessage, MutableWritableMessage, ReadableMessage};
47
48mod embedded_io;
49
50pub mod helpers;
51pub mod wkc;
52mod wkc_implementation;
53#[cfg(feature = "leaky_names")]
54pub use wkc_implementation::WellKnownCore;
55
56use coap_message_utils::Error;
57
58mod forking;
59mod forking_helpers;
60
61// Merely implemented separately; might move them publicly there later and deprecate them here in
62// preparation for a breakin grename.
63pub use forking::{
64 ForkingHandler, ForkingRequestData, ForkingTreeHandler, HandlerBuilder,
65 ReportingHandlerBuilder, new_dispatcher,
66};
67
68use coap_handler::Handler;
69use core::convert::Infallible;
70
71/// A resource that unconditionally responds 4.04 Not Found.
72///
73/// This is a convenience tool for building trees of resources -- rather than special casing the
74/// "none found" situation, this handler can be used.
75pub struct NeverFound {}
76
77impl Handler for NeverFound {
78 type RequestData = Infallible;
79
80 type ExtractRequestError = Error;
81 type BuildResponseError<M: MinimalWritableMessage> = Infallible;
82
83 fn extract_request_data<M: ReadableMessage>(
84 &mut self,
85 _request: &M,
86 ) -> Result<Self::RequestData, Self::ExtractRequestError> {
87 Err(Error::not_found())
88 }
89
90 fn estimate_length(&mut self, _request: &Self::RequestData) -> usize {
91 0
92 }
93
94 fn build_response<M: MutableWritableMessage>(
95 &mut self,
96 _response: &mut M,
97 request: Self::RequestData,
98 ) -> Result<(), Infallible> {
99 match request {}
100 }
101}
102
103mod simple_rendered;
104pub use simple_rendered::{
105 SimpleRenderable, SimpleRenderableData, SimpleRendered, TypedStaticRenderable,
106};
107
108mod typed_resource;
109pub use typed_resource::{
110 DeleteRenderable, Empty, FetchRenderable, GetRenderable, IPatchRenderable, PostRenderable,
111 PutRenderable, TypeHandler, TypeRenderable, TypeRequestData, generated::*,
112};