aws_iot_device_sdk/
tunneling.rs

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