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
// Limit imposed by the mqtt spec
pub const MQTT_TOPIC_LENGTH_MAX: usize = 65535;

pub const THINGNAME_MAX_LENGTH: usize = 128;
pub const SHADOW_NAME_LENGTH_MAX: usize = 64;
pub const JOBID_MAX_LENGTH: usize = 64;

pub const TUNNEL_TOPIC_MAX_LENGTH: usize = THINGNAME_MAX_LENGTH + 32;
pub const DEFENDER_TOPIC_MAX_LENGTH: usize = THINGNAME_MAX_LENGTH + 32;
pub const JOBS_TOPIC_MAX_LENGTH: usize = THINGNAME_MAX_LENGTH + JOBID_MAX_LENGTH + 32;
pub const SHADOW_TOPIC_MAX_LENGTH: usize = THINGNAME_MAX_LENGTH + SHADOW_NAME_LENGTH_MAX + 32;

pub const AWS_THINGS_PREFIX: &str = "$aws/things/";

pub const DEFENDER_API_BRIDGE: &str = "/defender/metrics/";
pub const JOBS_API_BRIDGE: &str = "/jobs/";
pub const SHADOW_API_BRIDGE: &str = "/shadow/";
pub const NAMED_SHADOW_API_BRIDGE: &str = "/shadow/name/";
pub const TUNNELS_API_BRIDGE: &str = "/tunnels/";

pub const SUFFIX_ACCEPTED: &str = "/accepted";
pub const SUFFIX_REJECTED: &str = "/rejected";

pub const ACCEPTED: &str = "accepted";
pub const REJECTED: &str = "rejected";

#[derive(Debug, PartialEq, Clone)]
pub enum Error {
    FAIL,                   /* function encountered error. */
    MqttTopicFailed,        /* Input mqtt topic is invalid. */
    ThingnameParseFailed,   /* Could not parse the thing name. */
    MessageTypeParseFailed, /* Could not parse the type. */
    RootParseFailed,        /* Could not parse the root. */
    ShadownameParseFailed, /* Could not parse the shadow name (in the case of a named shadow topic). */
    JobsIdParseFailed,     /* Could not parse the job id. */
    NoMatch,               /* The provided topic does not match any defender topic. */
}

/// valid parameters?
///
/// # Example
/// ```
/// ```
fn is_valid_param(s: &str, max_len: usize) -> Result<(), Error> {
    if !s.is_empty() && s.len() < max_len {
        return Ok(());
    }
    Err(Error::FAIL)
}

///
/// valid mqtt topic?
/// # Example
/// ```
/// ```
pub fn is_valid_mqtt_topic(mqtt_topic: &str) -> Result<(), Error> {
    is_valid_param(mqtt_topic, MQTT_TOPIC_LENGTH_MAX).map_err(|_| Error::MqttTopicFailed)
}

///
/// valid aws thing prefix?
/// # Example
/// ```
/// ```
pub fn is_valid_prefix<'a>(s: &'a str, pre: &str) -> Result<&'a str, Error> {
    s.strip_prefix(pre).ok_or(Error::NoMatch)
}

///
/// valid name in aws iot?
/// # Example
/// ```
/// ```
fn is_valid_name(name: &str, len: usize) -> Result<(), Error> {
    is_valid_param(name, len)?;
    for a in name.chars() {
        match a {
            '-' | '_' | '0'..='9' | 'A'..='Z' | 'a'..='z' | ':' => continue,
            _ => return Err(Error::FAIL),
        }
    }
    Ok(())
}

///
/// valid aws iot thing name?
/// # Example
/// ```
/// ```
pub fn is_valid_thing_name(thing_name: &str) -> Result<(), Error> {
    is_valid_name(thing_name, THINGNAME_MAX_LENGTH).map_err(|_| Error::ThingnameParseFailed)
}

///
/// valid aws iot shadow name?
/// # Example
/// ```
/// ```
pub fn is_valid_shadow_name(shadow_name: &str) -> Result<(), Error> {
    is_valid_name(shadow_name, SHADOW_NAME_LENGTH_MAX).map_err(|_| Error::ShadownameParseFailed)
}

///
/// valid aws iot bridge?
/// Like, "/shadow/" or "/jobs?", etc.
/// # Example
/// ```
/// ```
pub fn is_valid_bridge<'a>(s: &'a str, bridge: &str) -> Result<&'a str, Error> {
    s.strip_prefix(bridge).ok_or(Error::RootParseFailed)
}

///
/// valid aws iot job id?
/// # Example
/// ```
/// ```
pub fn is_valid_job_id(job_id: &str) -> Result<(), Error> {
    // Thing thing_name cannot be empty or longer than JOBID_MAX_LENGTH
    is_valid_param(job_id, JOBID_MAX_LENGTH)?;
    for a in job_id.chars() {
        match a {
            '-' | '_' | '0'..='9' | 'A'..='Z' | 'a'..='z' => continue,
            _ => return Err(Error::JobsIdParseFailed),
        }
    }
    Ok(())
}
#[cfg(test)]
mod tests {
    use crate::common::*;
    #[test]
    fn valid_mqtt_topic() -> Result<(), Error> {
        is_valid_mqtt_topic("hello/world")?;
        Ok(())
    }
    #[test]
    fn valid_prefix() -> Result<(), Error> {
        is_valid_prefix("hello/world", "hello/")?;
        Ok(())
    }
    #[test]
    fn valid_thing_name() -> Result<(), Error> {
        is_valid_thing_name("-_09AZaz:")?;
        Ok(())
    }
    #[test]
    fn valid_shadow_name() -> Result<(), Error> {
        is_valid_shadow_name("common")?;
        Ok(())
    }
    #[test]
    fn valid_job_id() -> Result<(), Error> {
        is_valid_job_id("_-09AZaz")?;
        Ok(())
    }
}