coap_message/
lib.rs

1#![no_std]
2//! CoAP mesage abstraction
3//! =======================
4//!
5//! This crate defines interfaces for readable and writable [CoAP] messages, which are used in the
6//! Internet of Things to transfer state in representations (REST). With these interfaces, it
7//! allows CoAP servers and clients to be written independently of the actual CoAP library that is
8//! used to serialize and deserialize the messages to the protocol.
9//!
10//! [CoAP]: http://coap.space/
11//!
12//! The defined traits only cover the properties of CoAP at the request/response exchange level,
13//! not of the transport specific details. In particular, they do not expose tokens, message IDs or
14//! message types: Those are not even present in some transports, and in general there is no need
15//! for libraries to expose them. In the same vein, no methods are described to allocate messages:
16//! For constrained devices, that may not even be possible, and with CoAP libraries that support
17//! multiple configured transports, the type of message allocated may easily depend on the intended
18//! use. They do not cover remote addresses either, as these too are transport specific.
19//!
20//! The crate also provides some trivial implementations of the types: [Code] for [u8], and
21//! [OptionNumber] for [u16].
22//!
23//! Usage
24//! -----
25//!
26//! A larger chunk of examples and demos is available at [coap-message-demos], which
27//! illustrates both how to build coap-message based handlers, and how to use them in practice with
28//! different implementations of CoAP. Most of the applications in there use coap-message through
29//! [the implementations in coap-handler], which provide short-cuts for common cases.
30//!
31//! [coap-message-demos]: https://gitlab.com/chrysn/coap-message-demos
32//! [the implementations in coap-handler]: https://gitlab.com/chrysn/coap-handler/-/blob/master/src/implementations.rs
33//!
34//! If you are on the implementing side of the message traits, you may want to have a look at
35//! [coap-message-utils], which contains useful building blocks.
36//!
37//! [coap-message-utils]: https://crates.io/crates/coap-message-utils
38//!
39//! Error handling
40//! --------------
41//!
42//! The readable message types are designed with minimal fallability -- for example, iterating over
43//! the options can not raise a "premature end of message" style error, and the payload is always
44//! "present" (albeit possibly empty). This encodes the concept that having a message type implies
45//! validity of the message.
46//!
47//! Libraries that insist on minimizing the cycles spent parsing (as typical in embedded
48//! situations) can opt to express the formatting error as a critical, not-safe-to-forward option
49//! (eg. the private-use number 65535). As consumers of a message that has not been checked in
50//! advance must check all options before acting on the message anyway, this ensures that the error
51//! does not go unnoticed.
52//!
53//! Operations on writable messages are fallible. The typical error cause there is overflowing the
54//! size of message that the transport can carry (the path MTU on UDP, or the peer's
55//! Max-Message-Size on TCP) or the size of the allocated buffer. Errors resulting from the stack
56//! not implementing certain features (eg. not implementing all options or codes) can be checked
57//! ahead of attempted use because they use the associated types [Code] and [OptionNumber] that are
58//! created fallibly.
59
60mod message;
61mod numbers;
62
63pub use message::{
64    MessageOption, MinimalWritableMessage, MutableWritableMessage, ReadableMessage,
65    SeekWritableMessage, WithSortedOptions,
66};
67
68pub use numbers::{Code, OptionNumber};
69pub mod error;
70pub mod helpers;
71
72mod core_impl;