coap_message/
numbers.rs

1/// A message code
2/// A CoAP code
3///
4/// For the code's meaning, see the [CoAP Codes IANA subregistry] or the [coap_numbers::code]
5/// module that lists them.
6///
7/// [CoAP Codes IANA subregistry]: https://www.iana.org/assignments/core-parameters/core-parameters.xhtml#codes
8/// [coap_numbers::code]: https://docs.rs/coap-numbers/0.1.2/coap_numbers/code/
9///
10/// All code implementations can be converted to u8 (as they correspond to an 8-bit unsigned
11/// integer in the CoAP protocol). The conversion back is fallible to allow strict message
12/// implementations to only accept codes supported by the implementation, and/or to limit the
13/// values by type state. For example, a strict implementation may distinguish between request and
14/// response messages, and only allow the respective code class into their objects (simultaneously
15/// creating usable niches in the in-memory representations).
16pub trait Code: Into<u8> + core::convert::TryFrom<u8> {
17    /// Error of the `.new()` method; ideally, this is also the TryFrom::Error (which can not
18    /// be restricted without [Associated type
19    /// bounds](https://github.com/rust-lang/rust/issues/52662) being stabilized).
20    ///
21    /// Once that is available, the built-in new method will be deprecated and eventually
22    /// removed -- a change that will be trivial on code using it provided that this error is
23    /// indeed the error of TryFrom.
24    ///
25    /// The error, when rendered, should represent a 5.00 style error, because what went wrong was
26    /// that the server CoAP library can not represent the response code intended by the handler.
27    type Error: core::fmt::Debug + crate::error::RenderableOnMinimal;
28
29    /// This constructor is a more concrete variant of the TryFrom function whose error code is
30    /// practically usable (see [Self::Error])
31    fn new(code: u8) -> Result<Self, <Self as Code>::Error>;
32}
33
34impl Code for u8 {
35    type Error = core::convert::Infallible;
36
37    fn new(code: u8) -> Result<Self, core::convert::Infallible> {
38        Ok(code)
39    }
40}
41
42/// A CoAP option number
43///
44/// For its meaning, see the [CoAP Option Numbers IANA subregistry] or the
45/// [coap_numbers::option] module that lists them, and provides helpers for decoding their
46/// properties.
47///
48/// [CoAP Option Numbers IANA subregistry]: https://www.iana.org/assignments/core-parameters/core-parameters.xhtml#option-numbers
49/// [coap_numbers::option]: https://docs.rs/coap-numbers/0.1.2/coap_numbers/option/
50///
51/// In analogy to [Code], these are convertible to [u16] and fallibly convertible from there.
52pub trait OptionNumber: Into<u16> + core::convert::TryFrom<u16> {
53    /// Error of the `.new()` method; ideally, this is also the TryFrom::Error (which can not
54    /// be restricted without [Associated type
55    /// bounds](https://github.com/rust-lang/rust/issues/52662) being stabilized).
56    ///
57    /// Once that is available, the built-in new method will be deprecated and eventually
58    /// removed -- a change that will be trivial on code using it provided that this error is
59    /// indeed the error of TryFrom.
60    ///
61    /// The error, when rendered, should represent a 5.00 style error, because what went wrong was
62    /// that the server handler attempted to use a CoAP option which the server could not
63    /// represent.
64    type Error: core::fmt::Debug + crate::error::RenderableOnMinimal;
65
66    /// This constructor is a more concrete variant of the TryFrom function whose error code is
67    /// practically usable (see [Self::Error])
68    fn new(option: u16) -> Result<Self, <Self as OptionNumber>::Error>;
69}
70
71impl OptionNumber for u16 {
72    type Error = core::convert::Infallible;
73
74    fn new(option: u16) -> Result<Self, core::convert::Infallible> {
75        Ok(option)
76    }
77}