rs-zero 0.2.21

Rust-first microservice framework inspired by go-zero engineering practices
Documentation
use rumqttc::AsyncClient;

use crate::mqtt::{MqttError, MqttQoS, MqttResult};

#[derive(Debug, Clone)]
pub struct MqttPublisher {
    client: AsyncClient,
}

impl MqttPublisher {
    pub fn new(client: AsyncClient) -> Self {
        Self { client }
    }

    pub async fn publish(
        &self,
        topic: impl Into<String>,
        qos: MqttQoS,
        payload: Vec<u8>,
    ) -> MqttResult<()> {
        self.publish_with_retain(topic, qos, false, payload).await
    }

    pub async fn publish_with_retain(
        &self,
        topic: impl Into<String>,
        qos: MqttQoS,
        retain: bool,
        payload: Vec<u8>,
    ) -> MqttResult<()> {
        self.client
            .try_publish(topic.into(), qos.into(), retain, payload)
            .map_err(|error| MqttError::Publish(error.to_string()))
    }
}