use toad_array::Array;
use toad_map::Map;
use toad_msg::{Id,
Message,
MessageOptions,
OptNumber,
OptionMap,
Payload,
Token,
TryIntoBytes,
Type};
use crate::ToCoapValue;
pub mod method;
#[doc(inline)]
pub use method::Method;
pub mod builder;
#[doc(inline)]
pub use builder::*;
use crate::platform::{self, PlatformTypes};
#[derive(Debug)]
pub struct Req<P: PlatformTypes>(platform::Message<P>);
impl<P: PlatformTypes> PartialEq for Req<P> {
fn eq(&self, other: &Self) -> bool {
self.0 == other.0
}
}
impl<P: PlatformTypes> Clone for Req<P> {
fn clone(&self) -> Self {
Self(self.0.clone())
}
}
impl<P: PlatformTypes> Req<P> {
pub fn new(method: Method, path: impl AsRef<str>) -> Self {
let msg = Message { ty: Type::Con,
ver: Default::default(),
code: method.0,
id: Id(Default::default()),
opts: Default::default(),
payload: Payload(Default::default()),
token: Token(Default::default()) };
let mut self_ = Self(msg);
self_.as_mut().set_path(path.as_ref()).ok();
self_
}
pub fn method(&self) -> Method {
Method(self.0.code)
}
pub fn msg(&self) -> &platform::Message<P> {
&self.0
}
pub fn msg_mut(&mut self) -> &mut platform::Message<P> {
&mut self.0
}
pub fn path(&self) -> Result<Option<&str>, core::str::Utf8Error> {
self.get_option(toad_msg::opt::known::repeat::PATH)
.and_then(|o| o.get(0))
.map(|o| core::str::from_utf8(&o.0).map(Some))
.unwrap_or(Ok(None))
}
pub fn msg_type(&self) -> toad_msg::Type {
self.0.ty
}
pub fn non(&mut self) -> () {
self.0.ty = Type::Non;
}
pub fn get(path: impl AsRef<str>) -> Self {
Self::new(Method::GET, path)
}
pub fn post(path: impl AsRef<str>) -> Self {
Self::new(Method::POST, path)
}
pub fn put(path: impl AsRef<str>) -> Self {
Self::new(Method::PUT, path)
}
pub fn delete(path: impl AsRef<str>) -> Self {
Self::new(Method::DELETE, path)
}
pub fn set_payload<Bytes: ToCoapValue>(&mut self, payload: Bytes) {
self.0.payload = Payload(payload.to_coap_value::<P::MessagePayload>());
}
pub fn payload(&self) -> &[u8] {
&self.0.payload.0
}
pub fn get_option(&self, n: OptNumber) -> Option<&<P::MessageOptions as OptionMap>::OptValues> {
self.0
.opts
.iter()
.find(|(num, _)| **num == n)
.map(|(_, v)| v)
}
pub fn payload_str(&self) -> Result<&str, core::str::Utf8Error> {
core::str::from_utf8(self.payload())
}
pub fn opts(
&self)
-> impl Iterator<Item = (&OptNumber, &<P::MessageOptions as OptionMap>::OptValues)> {
self.0.opts.iter()
}
}
impl<P> AsRef<platform::Message<P>> for Req<P> where P: platform::PlatformTypes
{
fn as_ref(&self) -> &platform::Message<P> {
&self.0
}
}
impl<P> AsMut<platform::Message<P>> for Req<P> where P: platform::PlatformTypes
{
fn as_mut(&mut self) -> &mut platform::Message<P> {
&mut self.0
}
}
impl<P: PlatformTypes> From<Req<P>> for platform::Message<P> {
fn from(req: Req<P>) -> Self {
req.0
}
}
impl<P: PlatformTypes> TryIntoBytes for Req<P> {
type Error = <platform::Message<P> as TryIntoBytes>::Error;
fn try_into_bytes<C: Array<Item = u8>>(self) -> Result<C, Self::Error> {
platform::Message::<P>::from(self).try_into_bytes()
}
}
impl<P: PlatformTypes> From<platform::Message<P>> for Req<P> {
fn from(msg: platform::Message<P>) -> Self {
Self(msg)
}
}