mod outbound;
mod session;
pub use session::Session;
use crate::{
QoS, ResourceError, Retain,
publication::{OwnedResponseTarget, Publication, ResponseTarget},
types::Properties,
};
use embedded_io_async::{ErrorType, Read, Write};
pub trait Io: Read + Write + ErrorType {}
impl<T> Io for T where T: Read + Write + ErrorType {}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub(crate) enum OpKind {
PublishAtLeastOnce,
PublishExactlyOnce,
Subscribe,
Unsubscribe,
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct Op {
kind: OpKind,
packet_id: u16,
generation: u32,
}
impl Op {
pub(crate) fn new(kind: OpKind, packet_id: u16, generation: u32) -> Self {
Self {
kind,
packet_id,
generation,
}
}
}
#[must_use = "inspect the returned status before deciding how to proceed"]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum OpStatus {
Pending,
Complete,
Invalidated,
}
#[derive(Debug)]
pub struct InboundPublish<'a> {
topic: &'a str,
payload: &'a [u8],
properties: Properties<'a>,
retain: Retain,
qos: QoS,
}
impl<'a> InboundPublish<'a> {
pub(crate) fn new(
topic: &'a str,
payload: &'a [u8],
properties: Properties<'a>,
retain: Retain,
qos: QoS,
) -> Self {
Self {
topic,
payload,
properties,
retain,
qos,
}
}
pub const fn topic(&self) -> &'a str {
self.topic
}
pub const fn payload(&self) -> &'a [u8] {
self.payload
}
pub const fn properties(&self) -> &Properties<'a> {
&self.properties
}
pub const fn retain(&self) -> Retain {
self.retain
}
pub const fn qos(&self) -> QoS {
self.qos
}
pub fn response_topic(&'a self) -> Option<&'a str> {
self.properties.response_topic()
}
pub fn correlation_data(&'a self) -> Option<&'a [u8]> {
self.properties.correlation_data()
}
fn response_target(&'a self) -> Option<ResponseTarget<'a>> {
Some(ResponseTarget {
topic: self.response_topic()?,
correlation_data: self.correlation_data(),
})
}
pub fn reply<P>(&'a self, payload: P) -> Option<Publication<'a, P>> {
self.response_target()
.map(|target| target.publication(payload))
}
pub fn reply_owned<const TOPIC: usize, const CORRELATION: usize>(
&'a self,
) -> Result<Option<OwnedResponseTarget<TOPIC, CORRELATION>>, ResourceError> {
self.response_target()
.map(ResponseTarget::to_owned)
.transpose()
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum ConnectEvent {
Connected,
Reconnected,
}