Skip to main content

mqtt5_protocol/types/
connect.rs

1use super::{ProtocolVersion, WillMessage};
2use crate::prelude::{String, Vec};
3use crate::time::Duration;
4
5#[derive(Clone)]
6pub struct ConnectOptions {
7    pub client_id: String,
8    pub keep_alive: Duration,
9    pub clean_start: bool,
10    pub username: Option<String>,
11    pub password: Option<Vec<u8>>,
12    pub will: Option<WillMessage>,
13    pub properties: ConnectProperties,
14    pub protocol_version: ProtocolVersion,
15}
16
17impl core::fmt::Debug for ConnectOptions {
18    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
19        f.debug_struct("ConnectOptions")
20            .field("client_id", &self.client_id)
21            .field("keep_alive", &self.keep_alive)
22            .field("clean_start", &self.clean_start)
23            .field("username", &self.username)
24            .field(
25                "password",
26                &if self.password.is_some() {
27                    Some("[REDACTED]")
28                } else {
29                    None::<&str>
30                },
31            )
32            .field("will", &self.will)
33            .field("properties", &self.properties)
34            .field("protocol_version", &self.protocol_version)
35            .finish()
36    }
37}
38
39impl Default for ConnectOptions {
40    fn default() -> Self {
41        Self {
42            client_id: String::new(),
43            keep_alive: Duration::from_secs(60),
44            clean_start: true,
45            username: None,
46            password: None,
47            will: None,
48            properties: ConnectProperties::default(),
49            protocol_version: ProtocolVersion::V5,
50        }
51    }
52}
53
54impl ConnectOptions {
55    #[must_use]
56    pub fn new(client_id: impl Into<String>) -> Self {
57        Self {
58            client_id: client_id.into(),
59            keep_alive: Duration::from_secs(60),
60            clean_start: true,
61            username: None,
62            password: None,
63            will: None,
64            properties: ConnectProperties::default(),
65            protocol_version: ProtocolVersion::V5,
66        }
67    }
68
69    #[must_use]
70    pub fn with_protocol_version(mut self, version: ProtocolVersion) -> Self {
71        self.protocol_version = version;
72        self
73    }
74
75    #[must_use]
76    pub fn with_keep_alive(mut self, duration: Duration) -> Self {
77        self.keep_alive = duration;
78        self
79    }
80
81    #[must_use]
82    pub fn with_clean_start(mut self, clean: bool) -> Self {
83        self.clean_start = clean;
84        self
85    }
86
87    #[must_use]
88    pub fn with_credentials(
89        mut self,
90        username: impl Into<String>,
91        password: impl AsRef<[u8]>,
92    ) -> Self {
93        self.username = Some(username.into());
94        self.password = Some(password.as_ref().to_vec());
95        self
96    }
97
98    #[must_use]
99    pub fn with_will(mut self, will: WillMessage) -> Self {
100        self.will = Some(will);
101        self
102    }
103
104    #[must_use]
105    pub fn with_session_expiry_interval(mut self, interval: u32) -> Self {
106        self.properties.session_expiry_interval = Some(interval);
107        self
108    }
109
110    #[must_use]
111    pub fn with_receive_maximum(mut self, receive_maximum: u16) -> Self {
112        self.properties.receive_maximum = Some(receive_maximum);
113        self
114    }
115
116    #[must_use]
117    pub fn with_authentication_method(mut self, method: impl Into<String>) -> Self {
118        self.properties.authentication_method = Some(method.into());
119        self
120    }
121
122    #[must_use]
123    pub fn with_authentication_data(mut self, data: impl AsRef<[u8]>) -> Self {
124        self.properties.authentication_data = Some(data.as_ref().to_vec());
125        self
126    }
127}
128
129#[derive(Debug, Clone, Default)]
130pub struct ConnectProperties {
131    pub session_expiry_interval: Option<u32>,
132    pub receive_maximum: Option<u16>,
133    pub maximum_packet_size: Option<u32>,
134    pub topic_alias_maximum: Option<u16>,
135    pub request_response_information: Option<bool>,
136    pub request_problem_information: Option<bool>,
137    pub user_properties: Vec<(String, String)>,
138    pub authentication_method: Option<String>,
139    pub authentication_data: Option<Vec<u8>>,
140}
141
142#[derive(Debug, Clone, Copy, PartialEq, Eq)]
143pub struct ConnectResult {
144    pub session_present: bool,
145}