risingwave_connector/source/mqtt/
mod.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Copyright 2024 RisingWave Labs
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

pub mod enumerator;
pub mod source;
pub mod split;

use std::collections::HashMap;
use std::fmt::{Display, Formatter};

use serde_derive::Deserialize;
use serde_with::{serde_as, DisplayFromStr};
use thiserror::Error;
use with_options::WithOptions;

use crate::connector_common::{MqttCommon, MqttQualityOfService};
use crate::source::mqtt::enumerator::MqttSplitEnumerator;
use crate::source::mqtt::source::{MqttSplit, MqttSplitReader};
use crate::source::SourceProperties;

pub const MQTT_CONNECTOR: &str = "mqtt";

#[derive(Debug, Clone, Error)]
pub struct MqttError(String);

impl Display for MqttError {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

#[serde_as]
#[derive(Clone, Debug, Deserialize, WithOptions)]
pub struct MqttProperties {
    #[serde(flatten)]
    pub common: MqttCommon,

    /// The topic name to subscribe or publish to. When subscribing, it can be a wildcard topic. e.g /topic/#
    pub topic: String,

    /// The quality of service to use when publishing messages. Defaults to at_most_once.
    /// Could be at_most_once, at_least_once or exactly_once
    #[serde_as(as = "Option<DisplayFromStr>")]
    pub qos: Option<MqttQualityOfService>,

    #[serde(flatten)]
    pub unknown_fields: HashMap<String, String>,
}

impl SourceProperties for MqttProperties {
    type Split = MqttSplit;
    type SplitEnumerator = MqttSplitEnumerator;
    type SplitReader = MqttSplitReader;

    const SOURCE_NAME: &'static str = MQTT_CONNECTOR;
}

impl crate::source::UnknownFields for MqttProperties {
    fn unknown_fields(&self) -> HashMap<String, String> {
        self.unknown_fields.clone()
    }
}