risingwave_connector/source/mqtt/
mod.rs

1// Copyright 2025 RisingWave Labs
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15pub mod enumerator;
16pub mod source;
17pub use enumerator::MqttSplitEnumerator;
18pub mod split;
19
20use std::collections::HashMap;
21use std::fmt::{Display, Formatter};
22
23use serde_derive::Deserialize;
24use serde_with::{DisplayFromStr, serde_as};
25use thiserror::Error;
26use with_options::WithOptions;
27
28use crate::connector_common::{MqttCommon, MqttQualityOfService};
29use crate::enforce_secret::EnforceSecret;
30use crate::error::ConnectorResult;
31use crate::source::SourceProperties;
32use crate::source::mqtt::source::{MqttSplit, MqttSplitReader};
33
34pub const MQTT_CONNECTOR: &str = "mqtt";
35
36#[derive(Debug, Clone, Error)]
37pub struct MqttError(String);
38
39impl Display for MqttError {
40    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
41        write!(f, "{}", self.0)
42    }
43}
44
45#[serde_as]
46#[derive(Clone, Debug, Deserialize, WithOptions)]
47pub struct MqttProperties {
48    #[serde(flatten)]
49    pub common: MqttCommon,
50
51    /// The topic name to subscribe or publish to. When subscribing, it can be a wildcard topic. e.g /topic/#
52    pub topic: String,
53
54    /// The quality of service to use when publishing messages. Defaults to at_most_once.
55    /// Could be at_most_once, at_least_once or exactly_once
56    #[serde_as(as = "Option<DisplayFromStr>")]
57    pub qos: Option<MqttQualityOfService>,
58
59    #[serde(flatten)]
60    pub unknown_fields: HashMap<String, String>,
61}
62
63impl EnforceSecret for MqttProperties {
64    fn enforce_secret<'a>(prop_iter: impl Iterator<Item = &'a str>) -> ConnectorResult<()> {
65        for prop in prop_iter {
66            MqttCommon::enforce_one(prop)?;
67        }
68        Ok(())
69    }
70}
71
72impl SourceProperties for MqttProperties {
73    type Split = MqttSplit;
74    type SplitEnumerator = MqttSplitEnumerator;
75    type SplitReader = MqttSplitReader;
76
77    const SOURCE_NAME: &'static str = MQTT_CONNECTOR;
78}
79
80impl crate::source::UnknownFields for MqttProperties {
81    fn unknown_fields(&self) -> HashMap<String, String> {
82        self.unknown_fields.clone()
83    }
84}