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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
use crate::common::{
    error::{inv_arg, oe_inv_arg, Error, Result},
    types::ArbData,
};
use serde::{Deserialize, Serialize};

/// Represents an ArbCmd structure, consisting of interface and operation
/// identifier strings and an ArbData object.
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct ArbCmd {
    interface_identifier: String,
    operation_identifier: String,
    data: ArbData,
}

impl ArbCmd {
    /// Verifies that the given identifier does not contain invalid characters.
    fn verify_id(id: String) -> Result<String> {
        if id.chars().any(|x| !(x.is_ascii_alphanumeric() || x == '_')) {
            inv_arg(format!(
                "\"{}\" is not a valid identifier; it contains characters outside [a-zA-Z0-9_]",
                id
            ))?
        } else if id == "" {
            inv_arg("identifiers must not be empty")?
        } else {
            Ok(id)
        }
    }

    /// Constructs an ArbCmd.
    ///
    /// The identifiers may only contain characters in the pattern
    /// `[a-zA-Z0-9_]`. It this is not the case, this function panics.
    pub fn new(
        interface_identifier: impl Into<String>,
        operation_identifier: impl Into<String>,
        data: ArbData,
    ) -> ArbCmd {
        ArbCmd {
            interface_identifier: ArbCmd::verify_id(interface_identifier.into()).unwrap(),
            operation_identifier: ArbCmd::verify_id(operation_identifier.into()).unwrap(),
            data,
        }
    }

    /// Constructs an ArbCmd.
    ///
    /// The identifiers may only contain characters in the pattern
    /// `[a-zA-Z0-9_]`. It this is not the case, this function fails.
    pub fn try_from(
        interface_identifier: impl Into<String>,
        operation_identifier: impl Into<String>,
        data: ArbData,
    ) -> Result<ArbCmd> {
        Ok(ArbCmd {
            interface_identifier: ArbCmd::verify_id(interface_identifier.into())?,
            operation_identifier: ArbCmd::verify_id(operation_identifier.into())?,
            data,
        })
    }

    /// Returns a reference to the interface identifier for this ArbCmd.
    pub fn interface_identifier(&self) -> &str {
        &self.interface_identifier
    }

    /// Returns a reference to the operation identifier for this ArbCmd.
    pub fn operation_identifier(&self) -> &str {
        &self.operation_identifier
    }

    /// Returns a reference to the data for this ArbCmd.
    pub fn data(&self) -> &ArbData {
        &self.data
    }

    /// Returns a mutable reference to the data for this ArbCmd.
    pub fn data_mut(&mut self) -> &mut ArbData {
        &mut self.data
    }
}

impl Into<ArbData> for ArbCmd {
    fn into(self) -> ArbData {
        self.data
    }
}

impl ::std::str::FromStr for ArbCmd {
    type Err = Error;

    /// Constructs an ArbCmd from its string representation. The following
    /// representations are allowed:
    ///
    ///  - `<interface-id>.<operation-id>` (use `ArbData::default()`)
    ///  - `<interface-id>.<operation-id>:<json>,<arg1>,<arg2>,[...]`
    ///    (use `ArbData::from_str()`)
    ///  - `<interface-id>.<operation-id>.<arg1>,<arg2>,[...]` (use
    ///    `ArbData::from_str_args_only()`)
    fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
        // Split off and validate the interface identifier.
        let mut x = s.splitn(2, '.');
        let interface_identifier = ArbCmd::verify_id(x.next().unwrap().to_string())?;
        let s = x.next().ok_or_else(oe_inv_arg(
            "expected period after interface identifier while parsing ArbCmd",
        ))?;
        assert_eq!(x.next(), None);

        // Figure out where and how to split the operation identifier from the
        // data argument.
        let offs_period = s.find('.');
        let offs_semicolon = s.find(':');
        enum ArgMode {
            Omitted,
            JsonOmited(usize),
            Complete(usize),
        };
        let mode = match offs_period {
            Some(offs_period) => match offs_semicolon {
                Some(offs_semicolon) => {
                    if offs_period < offs_semicolon {
                        ArgMode::JsonOmited(offs_period)
                    } else {
                        ArgMode::Complete(offs_semicolon)
                    }
                }
                None => ArgMode::JsonOmited(offs_period),
            },
            None => match offs_semicolon {
                Some(offs_semicolon) => ArgMode::Complete(offs_semicolon),
                None => ArgMode::Omitted,
            },
        };

        // Split off and validate the operation identifier and parse the ArbCmd
        // in the way we just detected.
        match mode {
            ArgMode::Omitted => Ok(ArbCmd {
                interface_identifier,
                operation_identifier: ArbCmd::verify_id(s.to_string())?,
                data: ArbData::default(),
            }),
            ArgMode::JsonOmited(offs) => Ok(ArbCmd {
                interface_identifier,
                operation_identifier: ArbCmd::verify_id(s[..offs].to_string())?,
                data: ArbData::from_str_args_only(&s[offs + 1..])?,
            }),
            ArgMode::Complete(offs) => Ok(ArbCmd {
                interface_identifier,
                operation_identifier: ArbCmd::verify_id(s[..offs].to_string())?,
                data: ArbData::from_str(&s[offs + 1..])?,
            }),
        }
    }
}

impl ::std::fmt::Display for ArbCmd {
    /// Turns the ArbCmd object into a string representation that can be
    /// parsed by `from_str()`.
    fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
        write!(
            f,
            "{}.{}:{}",
            self.interface_identifier, self.operation_identifier, self.data
        )
    }
}

#[cfg(test)]
mod test {

    use super::ArbCmd;
    use crate::common::types::arb_data::ArbData;
    use serde_json::json;
    use std::str::FromStr;

    fn test_from_str_good(
        input: &str,
        exp_iface: &str,
        exp_oper: &str,
        exp_json: serde_json::Value,
        exp_args: Vec<&[u8]>,
    ) {
        let exp_args: Vec<Vec<u8>> = exp_args.into_iter().map(|x| x.to_vec()).collect();
        assert_eq!(
            ArbCmd::from_str(input).unwrap(),
            ArbCmd::new(
                exp_iface,
                exp_oper,
                ArbData::from_json(exp_json.to_string(), exp_args).unwrap(),
            )
        );
    }

    fn test_from_str_fail(input: &str, msg: &str) {
        assert_eq!(ArbCmd::from_str(input).unwrap_err().to_string(), msg);
    }

    #[test]
    fn from_str() {
        test_from_str_good("a.b", "a", "b", json!({}), vec![]);
        test_from_str_good("a.b.x,y,z", "a", "b", json!({}), vec![b"x", b"y", b"z"]);
        test_from_str_good(
            "a.b:{\"answer\":42}",
            "a",
            "b",
            json!({"answer": 42}),
            vec![],
        );
        test_from_str_good(
            "a.b:{\"answer\":42},x,y,z",
            "a",
            "b",
            json!({"answer": 42}),
            vec![b"x", b"y", b"z"],
        );
        test_from_str_good("a.b.:x,y,z", "a", "b", json!({}), vec![b":x", b"y", b"z"]);
        test_from_str_good(
            "a.b:{\"answer\":42},.x,y,z",
            "a",
            "b",
            json!({"answer": 42}),
            vec![b".x", b"y", b"z"],
        );
        test_from_str_fail(
            "a",
            "Invalid argument: expected period after interface identifier while parsing ArbCmd",
        );
        test_from_str_fail(
            "a%.b",
            "Invalid argument: \"a%\" is not a valid identifier; it contains characters outside [a-zA-Z0-9_]",
        );
        test_from_str_fail(
            "a.b%",
            "Invalid argument: \"b%\" is not a valid identifier; it contains characters outside [a-zA-Z0-9_]",
        );
    }

    fn test_to_str(
        iface: &str,
        oper: &str,
        json: serde_json::Value,
        args: Vec<&[u8]>,
        exp_output: &str,
    ) {
        let args: Vec<Vec<u8>> = args.into_iter().map(|x| x.to_vec()).collect();
        let cmd = ArbCmd::new(
            iface,
            oper,
            ArbData::from_json(json.to_string(), args).unwrap(),
        );
        let string = cmd.to_string();
        assert_eq!(string, exp_output);
        assert_eq!(ArbCmd::from_str(&string).unwrap(), cmd);
    }

    #[test]
    fn to_str() {
        test_to_str("a", "b", json!({}), vec![], "a.b:{}");
        test_to_str(
            "a",
            "b",
            json!({"answer": 42}),
            vec![b"x", b"y", b"z"],
            "a.b:{\"answer\":42},x,y,z",
        );
    }

    #[test]
    fn into_arbdata() {
        let cmd = ArbCmd::from_str("a.b").unwrap();
        let data: ArbData = cmd.into();
        assert_eq!(ArbCmd::from_str("a.b").unwrap().data(), &data);
    }
}