aws_iot_device_sdk_embedded_rust/
tunneling.rs

1use crate::common::*;
2
3const API_BRIDGE: &str = "/tunnels/";
4const API_CHANGED: &str = "notify";
5
6/// Check if the given topic is one of the Device Defender topics.
7///
8/// # Example
9/// ```
10/// use aws_iot_device_sdk_embedded_rust::{tunneling};
11///
12/// let tunnels = tunneling::match_topic("$aws/things/chloe/tunnels/notify");
13/// assert_eq!(tunnels, Ok(()));
14///
15/// ```
16pub fn match_topic(topic: &str) -> Result<(), Error> {
17    // $aws/things/thing-name/tunnels/notify
18    is_valid_mqtt_topic(topic)?;
19
20    let s = is_valid_prefix(topic, AWS_THINGS_PREFIX)?;
21
22    let mid = s.find('/').ok_or(Error::FAIL);
23    let (thing_name, mut s) = s.split_at(mid?);
24    is_valid_thing_name(thing_name)?;
25
26    s = is_valid_bridge(s, API_BRIDGE)?;
27
28    if s == API_CHANGED {
29        return Ok(());
30    }
31    Err(Error::NoMatch)
32}
33
34#[cfg(test)]
35mod tests {
36    use crate::tunneling;
37    #[test]
38    fn tunnels_match_topic() {
39        let tunnels = tunneling::match_topic("$aws/things/chloe/tunnels/notify");
40        assert_eq!(tunnels, Ok(()));
41    }
42}