coap_handler/wkc.rs
1//! Traits to implement a .well-known/core resource easily
2//!
3//! This tries to be future-proof for building also CoRAL responses, without going out of its way
4//! for that.
5
6/// A property an advertised resource can have many of.
7///
8/// This corresponds to target attributes in Link Format, and also to properties in CoRAL without
9/// being very final yet.
10///
11/// This is a single type with static string out-references, but likely to be generalized later
12/// into a trait (but right now it's insufficiently known what it'll need to produce).
13#[non_exhaustive]
14#[derive(Copy, Clone)]
15pub enum Attribute {
16 Observable,
17 Interface(&'static str),
18 ResourceType(&'static str),
19 Title(&'static str),
20 Ct(u16), // Single only -- array could be added in an own option
21 Sz(usize),
22}
23
24/// A entry produced by Reporting, corresponding to a single link in a Link Format file.
25pub trait Record {
26 type PathElement: AsRef<str>;
27 type PathElements: Iterator<Item = Self::PathElement>;
28 type Attributes: Iterator<Item = Attribute>;
29
30 /// List of path segments (equivalent to Uri-Path option values) leading to the indicated
31 /// resoruce
32 fn path(&self) -> Self::PathElements;
33
34 /// Link relation (or None to default to the implicit "hosts")
35 ///
36 /// Note that the allowed character set is limited compared to full UTF-8 strings.
37 fn rel(&self) -> Option<&str>;
38
39 /// Target attributes of the link
40 fn attributes(&self) -> Self::Attributes;
41}
42
43/// Indicates that this resource can produce output for a .well-known/core resource.
44///
45/// Several trivial implementations ([NotReporting] for resources that should not show in
46/// .well-known/core, [ConstantSingleRecordReport] for resources with one static record) are
47/// available that cover most scenarios in which a custom [Handler](crate::Handler) is implemented.
48///
49/// [NotReporting]: https://docs.rs/coap-handler-implementations/0.3.5/coap_handler_implementations/wkc/struct.NotReporting.html
50/// [ConstantSingleRecordReport]: https://docs.rs/coap-handler-implementations/0.3.5/coap_handler_implementations/wkc/struct.ConstantSingleRecordReport.html
51pub trait Reporting {
52 type Record<'res>: Record
53 where
54 Self: 'res;
55 type Reporter<'res>: Iterator<Item = Self::Record<'res>>
56 where
57 Self: 'res;
58
59 fn report(&self) -> Self::Reporter<'_>;
60}