Skip to main content

rust_mqtt/client/options/
connect.rs

1use core::num::NonZero;
2
3use const_fn::const_fn;
4
5use crate::{
6    client::options::WillOptions,
7    config::{KeepAlive, MaximumPacketSize, SessionExpiryInterval},
8    types::{MqttBinary, MqttString},
9};
10
11/// Options for a connection.
12#[derive(Debug, Clone)]
13#[cfg_attr(feature = "defmt", derive(defmt::Format))]
14pub struct Options<'c> {
15    /// If set to true, a new session is started. If set to false, an existing session is continued.
16    pub clean_start: bool,
17
18    /// The setting of the keep alive the client wishes. Can be set to a different value by the server.
19    pub keep_alive: KeepAlive,
20
21    /// The setting of the session expiry interval the server wishes. Can be set to a different value by the server.
22    pub session_expiry_interval: SessionExpiryInterval,
23
24    /// The maximum packet size that the client is willing to accept
25    pub maximum_packet_size: MaximumPacketSize,
26
27    /// When set to true, the broker may return response information used to construct response topics.
28    pub request_response_information: bool,
29
30    /// The user name the client wishes to authenticate with.
31    pub user_name: Option<MqttString<'c>>,
32    /// The password the client wishes to perform basic authentication with.
33    pub password: Option<MqttBinary<'c>>,
34
35    /// The will configuration for the session of the connection.
36    pub will: Option<WillOptions<'c>>,
37}
38
39impl Default for Options<'_> {
40    fn default() -> Self {
41        Self::new()
42    }
43}
44
45impl<'c> Options<'c> {
46    /// Creates new connect options with no clean start, infinite keep alive and session
47    /// expiry immediately after disconnecting.
48    #[must_use]
49    pub const fn new() -> Self {
50        Self {
51            clean_start: false,
52            keep_alive: KeepAlive::Infinite,
53            session_expiry_interval: SessionExpiryInterval::EndOnDisconnect,
54            maximum_packet_size: MaximumPacketSize::Unlimited,
55            request_response_information: false,
56            user_name: None,
57            password: None,
58            will: None,
59        }
60    }
61
62    /// Sets the clean start flag to true.
63    #[must_use]
64    pub const fn clean_start(mut self) -> Self {
65        self.clean_start = true;
66        self
67    }
68    /// Sets the desired keep alive of the connection.
69    #[must_use]
70    pub const fn keep_alive(mut self, keep_alive: KeepAlive) -> Self {
71        self.keep_alive = keep_alive;
72        self
73    }
74    /// Sets the desired session expiry interval of the connection.
75    #[must_use]
76    pub const fn session_expiry_interval(mut self, interval: SessionExpiryInterval) -> Self {
77        self.session_expiry_interval = interval;
78        self
79    }
80    /// Sets the maximum packet size as a limit in bytes. A value less than 2 does not make sense.
81    #[must_use]
82    pub const fn maximum_packet_size(mut self, maximum_packet_size: NonZero<u32>) -> Self {
83        self.maximum_packet_size = MaximumPacketSize::Limit(maximum_packet_size);
84        self
85    }
86    /// Sets the request response information property to true prompting the server to return
87    /// a response information property to construct response topics.
88    #[must_use]
89    pub const fn request_response_information(mut self) -> Self {
90        self.request_response_information = true;
91        self
92    }
93    /// Sets the user name.
94    #[const_fn(cfg(not(feature = "alloc")))]
95    #[must_use]
96    pub const fn user_name(mut self, user_name: MqttString<'c>) -> Self {
97        self.user_name = Some(user_name);
98        self
99    }
100    /// Sets the password.
101    #[const_fn(cfg(not(feature = "alloc")))]
102    #[must_use]
103    pub const fn password(mut self, password: MqttBinary<'c>) -> Self {
104        self.password = Some(password);
105        self
106    }
107    /// Sets the will.
108    #[const_fn(cfg(not(feature = "alloc")))]
109    #[must_use]
110    pub const fn will(mut self, will: WillOptions<'c>) -> Self {
111        self.will = Some(will);
112        self
113    }
114}