coap_handler_implementations/
wkc.rs1use coap_handler::Handler;
4use coap_message::{MinimalWritableMessage, MutableWritableMessage, ReadableMessage};
5
6use coap_handler::{Attribute, Record, Reporting};
7
8pub struct NotReporting<H: Handler>(H);
15
16impl<H: Handler> NotReporting<H> {
17 pub fn new(handler: H) -> Self {
18 NotReporting(handler)
19 }
20}
21
22impl<H: Handler> Handler for NotReporting<H> {
24 type RequestData = H::RequestData;
25 type ExtractRequestError = H::ExtractRequestError;
26 type BuildResponseError<M: MinimalWritableMessage> = H::BuildResponseError<M>;
27 fn extract_request_data<M: ReadableMessage>(
28 &mut self,
29 m: &M,
30 ) -> Result<H::RequestData, Self::ExtractRequestError> {
31 self.0.extract_request_data(m)
32 }
33 fn estimate_length(&mut self, r: &Self::RequestData) -> usize {
34 self.0.estimate_length(r)
35 }
36 fn build_response<M: MutableWritableMessage>(
37 &mut self,
38 m: &mut M,
39 r: Self::RequestData,
40 ) -> Result<(), Self::BuildResponseError<M>> {
41 self.0.build_response(m, r)
42 }
43}
44
45pub struct EmptyRecord;
51
52impl Record for EmptyRecord {
53 type PathElement = &'static &'static str; type PathElements = core::iter::Empty<&'static &'static str>;
55 type Attributes = core::iter::Empty<Attribute>;
56
57 fn path(&self) -> Self::PathElements {
58 core::iter::empty()
59 }
60 fn rel(&self) -> Option<&str> {
61 None
62 }
63 fn attributes(&self) -> Self::Attributes {
64 core::iter::empty()
65 }
66}
67
68impl<H: Handler> Reporting for NotReporting<H> {
69 type Record<'a>
70 = EmptyRecord
71 where
72 Self: 'a;
73 type Reporter<'a>
74 = core::iter::Empty<EmptyRecord>
75 where
76 Self: 'a;
77
78 fn report(&self) -> Self::Reporter<'_> {
79 core::iter::empty()
80 }
81}
82
83pub struct ConstantSingleRecordReport<'a, H: Handler> {
90 handler: H,
91 path: &'a [&'a str],
92 attributes: &'a [Attribute],
93 rel: Option<&'a str>,
94}
95
96impl<'a, H: Handler> ConstantSingleRecordReport<'a, H> {
97 pub fn new(handler: H, attributes: &'a [Attribute]) -> Self {
99 ConstantSingleRecordReport {
100 handler,
101 path: &[],
102 attributes,
103 rel: None,
104 }
105 }
106
107 pub fn new_with_path(handler: H, attributes: &'a [Attribute], path: &'a [&'a str]) -> Self {
117 ConstantSingleRecordReport {
118 handler,
119 path,
120 attributes,
121 rel: None,
122 }
123 }
124
125 pub fn with_rel(self, rel: &'a str) -> Self {
127 Self {
128 rel: Some(rel),
129 ..self
130 }
131 }
132}
133
134impl<H: Handler> Handler for ConstantSingleRecordReport<'_, H> {
136 type RequestData = H::RequestData;
137 type ExtractRequestError = H::ExtractRequestError;
138 type BuildResponseError<M: MinimalWritableMessage> = H::BuildResponseError<M>;
139 fn extract_request_data<M: ReadableMessage>(
140 &mut self,
141 m: &M,
142 ) -> Result<H::RequestData, Self::ExtractRequestError> {
143 self.handler.extract_request_data(m)
144 }
145 fn estimate_length(&mut self, r: &Self::RequestData) -> usize {
146 self.handler.estimate_length(r)
147 }
148 fn build_response<M: MutableWritableMessage>(
149 &mut self,
150 m: &mut M,
151 r: Self::RequestData,
152 ) -> Result<(), Self::BuildResponseError<M>> {
153 self.handler.build_response(m, r)
154 }
155}
156
157pub struct ConstantSliceRecord<'a> {
166 path: &'a [&'a str],
167 attributes: &'a [Attribute],
168 rel: Option<&'a str>,
169}
170
171impl<'a> ConstantSliceRecord<'a> {
172 pub fn new(attributes: &'a [Attribute]) -> Self {
177 Self {
178 path: &[],
179 attributes,
180 rel: None,
181 }
182 }
183
184 pub fn new_with_path(path: &'a [&'a str], attributes: &'a [Attribute]) -> Self {
186 Self {
187 path,
188 attributes,
189 rel: None,
190 }
191 }
192
193 pub fn with_rel(self, rel: &'a str) -> Self {
195 Self {
196 rel: Some(rel),
197 ..self
198 }
199 }
200}
201
202impl<'a> Record for ConstantSliceRecord<'a> {
203 type PathElement = &'a &'a str;
204 type PathElements = core::slice::Iter<'a, &'a str>;
205 type Attributes = core::iter::Cloned<core::slice::Iter<'a, Attribute>>;
206
207 fn path(&self) -> Self::PathElements {
208 self.path.iter()
209 }
210 fn rel(&self) -> Option<&str> {
211 self.rel
212 }
213 fn attributes(&self) -> Self::Attributes {
214 self.attributes.iter().cloned()
215 }
216}
217
218impl<H: Handler> Reporting for ConstantSingleRecordReport<'_, H> {
219 type Record<'b>
220 = ConstantSliceRecord<'b>
221 where
222 Self: 'b;
223 type Reporter<'b>
224 = core::iter::Once<ConstantSliceRecord<'b>>
225 where
226 Self: 'b;
227
228 fn report(&self) -> Self::Reporter<'_> {
229 core::iter::once(ConstantSliceRecord {
230 path: self.path,
231 attributes: self.attributes,
232 rel: self.rel,
233 })
234 }
235}
236
237pub fn write_link_format(
260 w: &mut impl core::fmt::Write,
261 report: &impl Reporting,
262 prefix: &[&str],
263) -> core::fmt::Result {
264 let mut first = true;
265 for record in report.report() {
266 if !first {
267 write!(w, ",")?;
268 } else {
269 first = false;
270 }
271 write!(w, "<")?;
272 for p in prefix.iter() {
273 write!(w, "/{p}")?;
274 }
275 for p in record.path() {
276 write!(w, "/{}", p.as_ref())?;
277 }
278 write!(w, ">")?;
279 if let Some(rel) = record.rel() {
280 write!(w, ";rel=\"{rel}\"")?;
281 }
282 for attr in record.attributes() {
283 attr.write_link_format(w)?;
284 }
285 }
286 report.write_extra_link_format(w, &mut first)?;
287 Ok(())
288}