Created by
Gordon Tyler
| use coap_lite::option_value::OptionValueU16;
use coap_lite::{CoapOption, CoapRequest, ContentFormat};
pub trait CoapRequestHelper {
fn set_domain(&mut self, domain: &str);
fn set_query(&mut self, query: &str);
fn add_accept(&mut self, accept: ContentFormat);
fn set_content_format(&mut self, content_format: ContentFormat);
fn set_data(&mut self, data: Vec<u8>);
}
impl<Endpoint> CoapRequestHelper for CoapRequest<Endpoint> {
fn set_domain(&mut self, domain: &str) {
self.message
.set_option(CoapOption::UriHost, [domain.as_bytes().to_vec()].into());
}
fn set_query(&mut self, query: &str) {
self.message
.set_option(CoapOption::UriQuery, [query.as_bytes().to_vec()].into());
}
fn add_accept(&mut self, accept: ContentFormat) {
self.message.add_option_as(
CoapOption::Accept,
OptionValueU16(content_format_as_u16(accept)),
);
}
fn set_content_format(&mut self, content_format: ContentFormat) {
self.message.set_options_as(
CoapOption::ContentFormat,
[OptionValueU16(content_format_as_u16(content_format))].into(),
);
}
fn set_data(&mut self, data: Vec<u8>) {
self.message.payload = data;
}
}
fn content_format_as_u16(cf: ContentFormat) -> u16 {
let num = usize::from(cf);
let num: u16 = num.try_into().unwrap();
num
}
|