coap_handler/
core_implementations.rsuse crate::{Handler, PausedOperation, Reporting, Request, Response};
use coap_message::MinimalWritableMessage;
use coap_numbers::code;
impl<H> Handler for Option<H>
where
H: Handler,
{
type Error<M: MinimalWritableMessage> = Result<H::Error<M>, M::UnionError>;
async fn handle<R: Request>(
&mut self,
request: R,
) -> Result<<R as Request>::ResponseDone, Self::Error<<R as Request>::ResponseMessage>> {
let Some(slef) = self.as_mut() else {
let mut response = request.done().respond(0).await;
let response_msg = response.response();
use coap_message::Code;
response_msg
.set_code(Code::new(code::NOT_FOUND).expect("What server can't do a 4.04?"));
return Ok(response.done());
};
slef.handle(request).await.map_err(Ok)
}
}
impl<H> Reporting for Option<H>
where
H: Reporting,
{
type Record<'res> = H::Record<'res>
where
Self: 'res;
type Reporter<'res> = OptionReporter<'res, H>
where
Self: 'res;
fn report(&self) -> Self::Reporter<'_> {
OptionReporter::<H>(self.as_ref().map(|h| h.report()))
}
}
pub struct OptionReporter<'res, H: Reporting + 'res>(Option<H::Reporter<'res>>);
impl<'res, H: Reporting + 'res> Iterator for OptionReporter<'res, H> {
type Item = H::Record<'res>;
fn next(&mut self) -> Option<H::Record<'res>> {
self.0.as_mut().and_then(|s| s.next())
}
}