use crate::{
ConnAck, Connect, Protocol, PubAck, PubComp, PubRec, PubRel, Publish, QoS, SubAck, Subscribe,
};
use alloc::string::String;
use alloc::vec;
use alloc::vec::Vec;
use bytes::Bytes;
use core::fmt;
impl Connect {
pub fn new<S: Into<String>>(id: S) -> Connect {
Connect {
protocol: Protocol::MQTT(4),
keep_alive: 10,
client_id: id.into(),
clean_session: true,
last_will: None,
username: None,
password: None,
}
}
pub fn set_username<S: Into<String>>(&mut self, u: S) -> &mut Connect {
self.username = Some(u.into());
self
}
pub fn set_password<S: Into<String>>(&mut self, p: S) -> &mut Connect {
self.password = Some(p.into());
self
}
pub fn len(&self) -> usize {
let mut len = 8 + "MQTT".len() + self.client_id.len();
if let Some(ref last_will) = self.last_will {
len += 4 + last_will.topic.len() + last_will.message.len();
}
if let Some(ref username) = self.username {
len += 2 + username.len();
}
if let Some(ref password) = self.password {
len += 2 + password.len();
}
len
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
#[repr(u8)]
pub enum ConnectReturnCode {
Accepted = 0,
RefusedProtocolVersion,
RefusedIdentifierRejected,
ServerUnavailable,
BadUsernamePassword,
NotAuthorized,
}
impl ConnAck {
pub fn new(code: ConnectReturnCode, session_present: bool) -> ConnAck {
ConnAck {
code,
session_present,
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct LastWill {
pub topic: String,
pub message: String,
pub qos: QoS,
pub retain: bool,
}
impl Publish {
pub fn new<S: Into<String>, P: Into<Vec<u8>>>(topic: S, qos: QoS, payload: P) -> Publish {
Publish {
dup: false,
qos,
retain: false,
pkid: 0,
topic: topic.into(),
payload: bytes::Bytes::from(payload.into()),
bytes: Bytes::new(),
}
}
pub fn set_pkid(&mut self, pkid: u16) -> &mut Self {
self.pkid = pkid;
self
}
}
impl PubAck {
pub fn new(pkid: u16) -> PubAck {
PubAck { pkid }
}
}
impl PubRec {
pub fn new(pkid: u16) -> PubRec {
PubRec { pkid }
}
}
impl PubRel {
pub fn new(pkid: u16) -> PubRel {
PubRel { pkid }
}
}
impl PubComp {
pub fn new(pkid: u16) -> PubComp {
PubComp { pkid }
}
}
impl Subscribe {
pub fn new<S: Into<String>>(topic: S, qos: QoS) -> Subscribe {
let topic = SubscribeTopic {
topic_path: topic.into(),
qos,
};
Subscribe {
pkid: 0,
topics: vec![topic],
}
}
pub fn empty_subscribe() -> Subscribe {
Subscribe {
pkid: 0,
topics: Vec::new(),
}
}
pub fn add(&mut self, topic: String, qos: QoS) -> &mut Self {
let topic = SubscribeTopic {
topic_path: topic,
qos,
};
self.topics.push(topic);
self
}
}
#[derive(Clone, PartialEq)]
pub struct SubscribeTopic {
pub topic_path: String,
pub qos: QoS,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SubscribeReturnCodes {
Success(QoS),
Failure,
}
impl SubAck {
pub fn new(pkid: u16, return_codes: Vec<SubscribeReturnCodes>) -> SubAck {
SubAck { pkid, return_codes }
}
}
impl fmt::Debug for Publish {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"Topic = {}, Qos = {:?}, Retain = {}, Pkid = {:?}, Payload Size = {}",
self.topic,
self.qos,
self.retain,
self.pkid,
self.payload.len()
)
}
}
impl fmt::Debug for Connect {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"Protocol = {:?}, Keep alive = {:?}, Client id = {}, Clean session = {}",
self.protocol, self.keep_alive, self.client_id, self.clean_session,
)
}
}
impl fmt::Debug for Subscribe {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"Filters = {:?}, Packet id = {:?}",
self.pkid, self.topics
)
}
}
impl fmt::Debug for SubscribeTopic {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Filter = {}, Qos = {:?}", self.topic_path, self.qos)
}
}