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(Debug, 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 Default for ConnectOptions {
18    fn default() -> Self {
19        Self {
20            client_id: String::new(),
21            keep_alive: Duration::from_secs(60),
22            clean_start: true,
23            username: None,
24            password: None,
25            will: None,
26            properties: ConnectProperties::default(),
27            protocol_version: ProtocolVersion::V5,
28        }
29    }
30}
31
32impl ConnectOptions {
33    #[must_use]
34    pub fn new(client_id: impl Into<String>) -> Self {
35        Self {
36            client_id: client_id.into(),
37            keep_alive: Duration::from_secs(60),
38            clean_start: true,
39            username: None,
40            password: None,
41            will: None,
42            properties: ConnectProperties::default(),
43            protocol_version: ProtocolVersion::V5,
44        }
45    }
46
47    #[must_use]
48    pub fn with_protocol_version(mut self, version: ProtocolVersion) -> Self {
49        self.protocol_version = version;
50        self
51    }
52
53    #[must_use]
54    pub fn with_keep_alive(mut self, duration: Duration) -> Self {
55        self.keep_alive = duration;
56        self
57    }
58
59    #[must_use]
60    pub fn with_clean_start(mut self, clean: bool) -> Self {
61        self.clean_start = clean;
62        self
63    }
64
65    #[must_use]
66    pub fn with_credentials(
67        mut self,
68        username: impl Into<String>,
69        password: impl AsRef<[u8]>,
70    ) -> Self {
71        self.username = Some(username.into());
72        self.password = Some(password.as_ref().to_vec());
73        self
74    }
75
76    #[must_use]
77    pub fn with_will(mut self, will: WillMessage) -> Self {
78        self.will = Some(will);
79        self
80    }
81
82    #[must_use]
83    pub fn with_session_expiry_interval(mut self, interval: u32) -> Self {
84        self.properties.session_expiry_interval = Some(interval);
85        self
86    }
87
88    #[must_use]
89    pub fn with_receive_maximum(mut self, receive_maximum: u16) -> Self {
90        self.properties.receive_maximum = Some(receive_maximum);
91        self
92    }
93
94    #[must_use]
95    pub fn with_authentication_method(mut self, method: impl Into<String>) -> Self {
96        self.properties.authentication_method = Some(method.into());
97        self
98    }
99
100    #[must_use]
101    pub fn with_authentication_data(mut self, data: impl AsRef<[u8]>) -> Self {
102        self.properties.authentication_data = Some(data.as_ref().to_vec());
103        self
104    }
105}
106
107#[derive(Debug, Clone, Default)]
108pub struct ConnectProperties {
109    pub session_expiry_interval: Option<u32>,
110    pub receive_maximum: Option<u16>,
111    pub maximum_packet_size: Option<u32>,
112    pub topic_alias_maximum: Option<u16>,
113    pub request_response_information: Option<bool>,
114    pub request_problem_information: Option<bool>,
115    pub user_properties: Vec<(String, String)>,
116    pub authentication_method: Option<String>,
117    pub authentication_data: Option<Vec<u8>>,
118}
119
120#[derive(Debug, Clone, Copy, PartialEq, Eq)]
121pub struct ConnectResult {
122    pub session_present: bool,
123}