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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
use crate::common::*;
use arrayvec::{ArrayString, ArrayVec};

use self::Topic::*;

const API_BRIDGE: &str = "/shadow/";
const API_BRIDGE_NAME: &str = "/shadow/name/";
const OP_GET: &str = "get";
const OP_DELETE: &str = "delete";
const OP_UPDATE: &str = "update";
const SUFFIX_DOCUMENTS: &str = "/documents";
const SUFFIX_DELTA: &str = "/delta";
/// A shadow topic string takes one of the two forms,
/// in the case of an unnamed ("Classic") shadow.
/// Or, in the case of a named shadow
/// The shadow_name part is None when unnamed shadow.
#[derive(Debug)]
pub struct ThingShadow<'a> {
    pub thing_name: &'a str,
    pub shadow_name: Option<&'a str>,
    pub shadow_op: Topic,
}

/// Each of these values describes the type of a shadow message.
// https://docs.aws.amazon.com/iot/latest/developerguide/device-shadow-mqtt.html
#[derive(Debug, PartialEq)]
pub enum Topic {
    Get = 0,
    GetAccepted,
    GetRejected,
    Delete,
    DeleteAccepted,
    DeleteRejected,
    Update,
    UpdateAccepted,
    UpdateRejected,
    UpdateDocuments,
    UpdateDelta,
}

/// Assemble shadow topic string when Thing Name or Shadow Name is only known at run time.
///
/// # Example
/// ```
/// use aws_iot_device_sdk::shadow::Topic::*;
/// use aws_iot_device_sdk::{shadow};
/// use arrayvec::{ArrayString, ArrayVec};
///
/// let topic = shadow::get_topic(shadow::Topic::Get, "chloe", None).unwrap();
/// assert_eq!("$aws/things/chloe/shadow/get", topic.as_str())
/// ```

pub fn get_topic(
    topic_type: Topic,
    thing_name: &str,
    named: Option<&str>,
) -> Result<ArrayString<SHADOW_TOPIC_MAX_LENGTH>, Error> {
    is_valid_thing_name(thing_name)?;
    let mut s = ArrayString::<SHADOW_TOPIC_MAX_LENGTH>::new();
    s.push_str(AWS_THINGS_PREFIX);
    s.push_str(thing_name);
    match named {
        // Classic shadow topic
        None => {
            s.push_str(API_BRIDGE);
            s.push_str(op(&topic_type));
            s.push_str(suffix(&topic_type));
            Ok(s)
        }
        // Named shadow topic
        Some(shadow_name) => {
            is_valid_shadow_name(shadow_name)?;
            s.push_str(API_BRIDGE_NAME);
            s.push_str(shadow_name);
            s.push_str("/");
            s.push_str(op(&topic_type));
            s.push_str(suffix(&topic_type));
            Ok(s)
        }
    }
}

fn op(topic_type: &Topic) -> &str {
    match topic_type {
        Get | GetAccepted | GetRejected => OP_GET,
        Delete | DeleteAccepted | DeleteRejected => OP_DELETE,
        Update | UpdateAccepted | UpdateRejected | UpdateDocuments | UpdateDelta => OP_UPDATE,
    }
}
fn suffix(topic_type: &Topic) -> &str {
    match topic_type {
        GetAccepted | DeleteAccepted | UpdateAccepted => SUFFIX_ACCEPTED,
        GetRejected | DeleteRejected | UpdateRejected => SUFFIX_REJECTED,
        UpdateDocuments => SUFFIX_DOCUMENTS,
        UpdateDelta => SUFFIX_DELTA,
        _ => "",
    }
}

/// Given the topic string of an incoming message, determine whether it is
/// related to a device shadow;
///
/// If it is, return information about the type of device shadow message,
/// and the Thing Name and Shadow Name inside of the topic string.
///
/// # Example
/// ```
/// use aws_iot_device_sdk::shadow::Topic::*;
/// use aws_iot_device_sdk::{shadow};
///
/// let topic = "$aws/things/chloe/shadow/name/common/get/rejected";
/// let shadow = shadow::match_topic(topic).unwrap();
///
/// assert_eq!(shadow.thing_name, "chloe");
/// assert_eq!(shadow.shadow_name.unwrap(), "common");
/// assert_eq!(shadow.shadow_op, shadow::Topic::GetRejected);
/// ```
pub fn match_topic<'a>(topic: &'a str) -> Result<ThingShadow, Error> {
    is_valid_mqtt_topic(topic)?;

    let s = is_valid_prefix(topic, AWS_THINGS_PREFIX)?;

    let mid = s.find('/').ok_or(Error::NoMatch);
    let (thing_name, s) = s.split_at(mid?);
    is_valid_thing_name(thing_name)?;

    let s = is_valid_bridge(s, API_BRIDGE)?;

    let v: ArrayVec<&str, 16> = s.split('/').collect();
    match v[..] {
        // Named shadow topic
        [_named, shadow_name, op, suffix] => {
            is_valid_shadow_name(shadow_name)?;
            Ok(ThingShadow {
                thing_name,
                shadow_name: Some(shadow_name),
                shadow_op: find_message_type(op, suffix)?,
            })
        }
        // Classic shadow topic
        [op, suffix] => Ok(ThingShadow {
            thing_name,
            shadow_name: None,
            shadow_op: find_message_type(op, suffix)?,
        }),
        // Not shadow topic
        _ => Err(Error::NoMatch),
    }
}

fn find_message_type(op: &str, suffix: &str) -> Result<Topic, Error> {
    match (op, suffix) {
        ("get", "accepted") => Ok(GetAccepted),
        ("get", "rejected") => Ok(GetRejected),
        ("delete", "accepted") => Ok(DeleteAccepted),
        ("delete", "rejected") => Ok(DeleteRejected),
        ("update", "accepted") => Ok(UpdateAccepted),
        ("update", "rejected") => Ok(UpdateRejected),
        ("update", "documents") => Ok(UpdateDocuments),
        ("update", "delta") => Ok(UpdateDelta),
        _ => Err(Error::MessageTypeParseFailed),
    }
}

#[cfg(test)]
mod tests {
    use crate::shadow;
    #[test]
    fn assemble_named_topic_string() {
        let topic = shadow::get_topic(shadow::Topic::Get, "chloe", Some("common")).unwrap();
        assert_eq!("$aws/things/chloe/shadow/name/common/get", topic.as_str());
    }
    #[test]
    fn assemble_classic_topic_string() {
        let topic = shadow::get_topic(shadow::Topic::Get, "chloe", None).unwrap();
        assert_eq!("$aws/things/chloe/shadow/get", topic.as_str());
    }
    #[test]
    fn assemble_classic_topic_string_suffix() {
        let topic = shadow::get_topic(shadow::Topic::GetAccepted, "chloe", None).unwrap();
        assert_eq!("$aws/things/chloe/shadow/get/accepted", topic.as_str());
    }
    #[test]
    fn match_classic_shadow_topic_string() {
        let topic = "$aws/things/chloe/shadow/get/accepted";
        let shadow = shadow::match_topic(topic).unwrap();
        assert_eq!(shadow.thing_name, "chloe");
        assert_eq!(shadow.shadow_name, None);
        assert_eq!(shadow.shadow_op, shadow::Topic::GetAccepted);
    }
    #[test]
    fn match_named_shadow_topic_string() {
        let topic = "$aws/things/chloe/shadow/name/common/get/rejected";
        let shadow = shadow::match_topic(topic).unwrap();
        assert_eq!(shadow.thing_name, "chloe");
        assert_eq!(shadow.shadow_name.unwrap(), "common");
        assert_eq!(shadow.shadow_op, shadow::Topic::GetRejected);
    }
}