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
use std::str::FromStr;

use super::FtlError;

#[derive(Debug, PartialEq)]
pub enum FtlCommand {
    HMAC,
    Connect {
        channel_id: String,
        hashed_hmac_payload: String,
    },
    Dot,
    Attribute {
        key: String,
        value: String,
    },
    Ping {
        channel_id: String,
    },
    Disconnect,
}

impl FromStr for FtlCommand {
    type Err = FtlError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "HMAC" => Ok(FtlCommand::HMAC),
            "." => Ok(FtlCommand::Dot),
            "DISCONNECT" => Ok(FtlCommand::Disconnect),
            s => {
                if &s[..4] == "PING" {
                    Ok(FtlCommand::Ping {
                        channel_id: s[5..].to_string(),
                    })
                } else if &s[..7] == "CONNECT" {
                    let parts = &mut s[8..].split(" ");

                    Ok(FtlCommand::Connect {
                        channel_id: parts
                            .next()
                            .ok_or_else(|| FtlError::MissingPart)?
                            .to_string(),
                        hashed_hmac_payload: parts
                            .next()
                            .ok_or_else(|| FtlError::MissingPart)?
                            .to_string(),
                    })
                } else {
                    if s.contains(':') {
                        let mut parts = s.split(':').map(|v| v.trim());

                        return Ok(FtlCommand::Attribute {
                            key: parts
                                .next()
                                .ok_or_else(|| FtlError::MissingPart)?
                                .to_string(),
                            value: parts
                                .next()
                                .ok_or_else(|| FtlError::MissingPart)?
                                .to_string(),
                        });
                    }

                    Err(FtlError::UnimplementedCommand)
                }
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use std::str::FromStr;

    use crate::protocol::FtlCommand;

    #[test]
    fn should_parse_hmac() {
        let command = FtlCommand::from_str("HMAC").unwrap();
        assert_eq!(command, FtlCommand::HMAC);
    }

    #[test]
    fn should_parse_dot() {
        let command = FtlCommand::from_str(".").unwrap();
        assert_eq!(command, FtlCommand::Dot);
    }

    #[test]
    fn should_parse_disconnect() {
        let command = FtlCommand::from_str("DISCONNECT").unwrap();
        assert_eq!(command, FtlCommand::Disconnect);
    }

    #[test]
    fn should_parse_ping() {
        let command = FtlCommand::from_str("PING channel_id").unwrap();
        assert_eq!(command, FtlCommand::Ping { channel_id: "channel_id".to_string() });
    }

    #[test]
    fn should_parse_connect() {
        let command = FtlCommand::from_str("CONNECT channel_id hmac").unwrap();
        assert_eq!(command, FtlCommand::Connect { channel_id: "channel_id".to_string(), hashed_hmac_payload: "hmac".to_string() });
    }

    #[test]
    fn should_parse_attribute() {
        let command = FtlCommand::from_str("ProtocolVersion: 0.9").unwrap();
        assert_eq!(command, FtlCommand::Attribute { key: "ProtocolVersion".to_string(), value: "0.9".to_string() });
    }

    #[test]
    fn should_fail_other() {
        let command = FtlCommand::from_str("i am invalid data");
        assert!(command.is_err())
    }

    #[test]
    fn doc_test() {
        use crate::protocol::FtlCommand;

        let command = FtlCommand::from_str(".").unwrap();
        assert_eq!(command, FtlCommand::Dot);

        let command = FtlCommand::from_str("PING 123").unwrap();
        assert_eq!(command, FtlCommand::Ping {
            channel_id: "123".to_string()
        });
    }
}