diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2024-04-05 11:36:50 +0200 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2024-04-05 11:44:44 +0200 |
commit | dd4c376cde57fc822dfbe0a4db86ef2b2d2e0509 (patch) | |
tree | 30622f46348bf9425238cfd9e0d7569ef082cd5e | |
parent | 0a9f156717d8b32876df0d4e435ac8198b080012 (diff) |
Add builder for MqttClient
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
-rw-r--r-- | src/client/builder.rs | 54 | ||||
-rw-r--r-- | src/client/mod.rs | 5 |
2 files changed, 59 insertions, 0 deletions
diff --git a/src/client/builder.rs b/src/client/builder.rs new file mode 100644 index 0000000..e981e2e --- /dev/null +++ b/src/client/builder.rs @@ -0,0 +1,54 @@ +// +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. +// + +use std::sync::Arc; + +use futures::lock::Mutex; + +use super::send::Callbacks; +use super::send::ClientHandlers; +use super::send::HandleAcknowledgeFn; +use super::send::OnPacketRecvFn; +use super::InnerClient; +use super::MqttClient; + +pub struct MqttClientBuilder { + handlers: ClientHandlers, +} + +impl MqttClientBuilder { + pub(super) fn new() -> Self { + Self { + handlers: ClientHandlers::default(), + } + } + + pub fn with_on_packet_recv(mut self, f: OnPacketRecvFn) -> Self { + self.handlers.on_packet_recv = f; + self + } + + pub fn with_handle_acknowledge(mut self, f: HandleAcknowledgeFn) -> Self { + self.handlers.handle_acknowledge = f; + self + } + + pub async fn build(self) -> Result<super::MqttClient, MqttClientBuilderError> { + Ok({ + MqttClient { + inner: Arc::new(Mutex::new(InnerClient { + connection_state: None, + session_state: None, + default_handlers: self.handlers, + outstanding_callbacks: Callbacks::new(), + })), + } + }) + } +} + +#[derive(Debug, thiserror::Error)] +pub enum MqttClientBuilderError {} diff --git a/src/client/mod.rs b/src/client/mod.rs index bd46b51..8ff460f 100644 --- a/src/client/mod.rs +++ b/src/client/mod.rs @@ -4,6 +4,7 @@ // file, You can obtain one at http://mozilla.org/MPL/2.0/. // +pub mod builder; pub mod connect; mod receive; pub mod send; @@ -41,6 +42,10 @@ impl MqttClient { })), } } + + pub fn builder() -> builder::MqttClientBuilder { + builder::MqttClientBuilder::new() + } } #[cfg(test)] |