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
/// # RESP2
/// This module provides utilities to parse the RESP2 protocol.
use nom::branch::alt;
use nom::multi::many_m_n;
use nom::{bytes::streaming::tag, IResult};

use crate::utils::{parse_bytes_with_length, parse_integer_with_prefix, parse_str_with_prefix};

/// Resp2Type represents all possible response types from the RESP2 protocol.
#[derive(Debug, PartialEq)]
pub enum Resp2Type<'a> {
    String(&'a str),
    Error(&'a str),
    Integer(usize),
    BulkString(&'a [u8]),
    Null,
    Array(Vec<Resp2Type<'a>>),
}

fn parse_simple_string(input: &[u8]) -> IResult<&[u8], Resp2Type> {
    let (input, string) = parse_str_with_prefix(input, '+')?;

    Ok((input, Resp2Type::String(string)))
}

fn parse_error(input: &[u8]) -> IResult<&[u8], Resp2Type> {
    let (input, string) = parse_str_with_prefix(input, '-')?;

    Ok((input, Resp2Type::Error(string)))
}

fn parse_integer(input: &[u8]) -> IResult<&[u8], Resp2Type> {
    let (input, int) = parse_integer_with_prefix(input, ':')?;

    Ok((input, Resp2Type::Integer(int)))
}

fn parse_bulk_string(input: &[u8]) -> IResult<&[u8], Resp2Type> {
    let (input, length) = parse_integer_with_prefix(input, '$')?;
    let (input, bytes) = parse_bytes_with_length(input, length)?;

    Ok((input, Resp2Type::BulkString(bytes)))
}

fn parse_null_string(input: &[u8]) -> IResult<&[u8], Resp2Type> {
    let (input, _) = tag("$-1\r\n")(input)?;

    Ok((input, Resp2Type::Null))
}

fn parse_array(input: &[u8]) -> IResult<&[u8], Resp2Type> {
    let (input, length) = parse_integer_with_prefix(input, '*')?;
    let (input, result) = many_m_n(length, length, parse)(input)?;

    Ok((input, Resp2Type::Array(result)))
}

/// Parse bytes into a `Resp2Type` enum
pub fn parse(input: &[u8]) -> IResult<&[u8], Resp2Type> {
    alt((
        parse_simple_string,
        parse_error,
        parse_integer,
        parse_bulk_string,
        parse_null_string,
        parse_array,
    ))(input)
}

#[cfg(test)]
mod tests {
    use crate::resp2::{parse, Resp2Type};

    #[test]
    fn test_parse_simple_string() {
        assert_eq!(
            parse(&b"+OK\r\n"[..]),
            Ok((&b""[..], Resp2Type::String("OK")))
        );
    }

    #[test]
    fn test_parse_error() {
        assert_eq!(
            parse(&b"-Error message\r\n"[..]),
            Ok((&b""[..], Resp2Type::Error("Error message")))
        );
    }

    #[test]
    fn test_parse_integer() {
        assert_eq!(
            parse(&b":100\r\n"[..]),
            Ok((&b""[..], Resp2Type::Integer(100)))
        );
    }

    #[test]
    fn test_parse_bulk_string() {
        assert_eq!(
            parse(&b"$10\r\n1234567890\r\n"[..]),
            Ok((&b""[..], Resp2Type::BulkString(&b"1234567890"[..])))
        );
    }

    #[test]
    fn test_parse_null_string() {
        assert_eq!(parse(&b"$-1\r\n"[..]), Ok((&b""[..], Resp2Type::Null)));
    }

    #[test]
    fn test_parse_array_empty() {
        assert_eq!(
            parse(&b"*0\r\n"[..]),
            Ok((&b""[..], Resp2Type::Array(vec![])))
        );
    }

    #[test]
    fn test_parse_array_mixed_objecs() {
        assert_eq!(
            parse(&b"*3\r\n:1\r\n:2\r\n$3\r\nfoo\r\n"[..]),
            Ok((
                &b""[..],
                Resp2Type::Array(vec![
                    Resp2Type::Integer(1),
                    Resp2Type::Integer(2),
                    Resp2Type::BulkString(&b"foo"[..])
                ])
            ))
        );
    }
}