use const_fn::const_fn;
use crate::{
types::{MqttBinary, MqttString, QoS, TopicName, Will},
v5::property::WillDelayInterval,
};
#[derive(Debug, Clone)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct Options<'c> {
pub will_qos: QoS,
pub will_retain: bool,
pub will_topic: TopicName<'c>,
pub will_delay_interval: u32,
pub payload_format_indicator: Option<bool>,
pub message_expiry_interval: Option<u32>,
pub content_type: Option<MqttString<'c>>,
pub response_topic: Option<TopicName<'c>>,
pub correlation_data: Option<MqttBinary<'c>>,
pub will_message: MqttBinary<'c>,
}
impl<'c> Options<'c> {
#[must_use]
pub const fn new(topic: TopicName<'c>, message: MqttBinary<'c>) -> Options<'c> {
Options {
will_qos: QoS::AtMostOnce,
will_retain: false,
will_topic: topic,
will_delay_interval: 0,
payload_format_indicator: None,
message_expiry_interval: None,
content_type: None,
response_topic: None,
correlation_data: None,
will_message: message,
}
}
#[must_use]
pub const fn qos(mut self, qos: QoS) -> Self {
self.will_qos = qos;
self
}
#[must_use]
pub const fn at_least_once(self) -> Self {
self.qos(QoS::AtLeastOnce)
}
#[must_use]
pub const fn exactly_once(self) -> Self {
self.qos(QoS::ExactlyOnce)
}
#[must_use]
pub const fn retain(mut self) -> Self {
self.will_retain = true;
self
}
#[must_use]
pub const fn delay_interval(mut self, delay_interval: u32) -> Self {
self.will_delay_interval = delay_interval;
self
}
#[must_use]
pub const fn payload_format_indicator(mut self, is_payload_utf8: bool) -> Self {
self.payload_format_indicator = Some(is_payload_utf8);
self
}
#[must_use]
pub const fn message_expiry_interval(mut self, message_expiry_interval: u32) -> Self {
self.message_expiry_interval = Some(message_expiry_interval);
self
}
#[const_fn(cfg(not(feature = "alloc")))]
#[must_use]
pub const fn content_type(mut self, content_type: MqttString<'c>) -> Self {
self.content_type = Some(content_type);
self
}
#[const_fn(cfg(not(feature = "alloc")))]
#[must_use]
pub const fn response_topic(mut self, response_topic: TopicName<'c>) -> Self {
self.response_topic = Some(response_topic);
self
}
#[const_fn(cfg(not(feature = "alloc")))]
#[must_use]
pub const fn correlation_data(mut self, correlation_data: MqttBinary<'c>) -> Self {
self.correlation_data = Some(correlation_data);
self
}
}
impl<'c> Options<'c> {
pub(crate) fn as_borrowed_will(&'c self) -> Will<'c> {
Will {
will_topic: self.will_topic.as_borrowed(),
will_delay_interval: match self.will_delay_interval {
0 => None,
i => Some(WillDelayInterval(i)),
},
payload_format_indicator: self.payload_format_indicator.map(Into::into),
message_expiry_interval: self.message_expiry_interval.map(Into::into),
content_type: self
.content_type
.as_ref()
.map(MqttString::as_borrowed)
.map(Into::into),
response_topic: self
.response_topic
.as_ref()
.map(TopicName::as_borrowed)
.map(Into::into),
correlation_data: self
.correlation_data
.as_ref()
.map(MqttBinary::as_borrowed)
.map(Into::into),
will_message: self.will_message.as_borrowed(),
}
}
}