Skip to main content

rust_mqtt/client/options/
disconnect.rs

1use crate::config::SessionExpiryInterval;
2
3/// Options for a disconnection to the server with a DISCONNECT packet.
4#[derive(Debug, Clone, Copy)]
5#[cfg_attr(feature = "defmt", derive(defmt::Format))]
6pub struct Options {
7    /// If set to true, the server publishes the will message.
8    pub publish_will: bool,
9
10    /// The session expiry interval property. Not allowed to be set to a non-zero value
11    /// if the session expiry interval property in the CONNECT packet has been 0.
12    /// This value overrides the session expiry interval negotiated in the handshake.
13    pub session_expiry_interval: Option<SessionExpiryInterval>,
14}
15
16impl Default for Options {
17    fn default() -> Self {
18        Self::new()
19    }
20}
21
22impl Options {
23    /// Creates new disconnect options with will publication disabled and no session expiry interval.
24    #[must_use]
25    pub const fn new() -> Self {
26        Self {
27            publish_will: false,
28            session_expiry_interval: None,
29        }
30    }
31
32    /// Sets the publish will flag to true.
33    #[must_use]
34    pub const fn publish_will(mut self) -> Self {
35        self.publish_will = true;
36        self
37    }
38    /// Sets the session expiry interval property.
39    #[must_use]
40    pub const fn session_expiry_interval(mut self, interval: SessionExpiryInterval) -> Self {
41        self.session_expiry_interval = Some(interval);
42        self
43    }
44}