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
use crate::{ParseError as Error, Response, ResponseContent};
use nom::{
    branch::alt,
    bytes::complete::{tag, take_till, take_while1},
    character::complete::{i8, line_ending},
    combinator::{cut, map},
    sequence::{delimited, pair, terminated},
};

pub type IResult<I, O> = nom::IResult<I, O, Error>;

impl<I> nom::error::ParseError<I> for Error {
    fn from_error_kind(_input: I, _kind: nom::error::ErrorKind) -> Self {
        Self::Unknown
    }

    fn append(_input: I, _kind: nom::error::ErrorKind, other: Self) -> Self {
        other
    }
}

pub fn response(input: &[u8]) -> IResult<&[u8], Response> {
    map(pair(header, content), |(header, content)| Response {
        header,
        content,
    })(input)
}

fn header(input: &[u8]) -> IResult<&[u8], &[u8]> {
    let prefix = map_err(tag("+"), |_: Error| Error::NoPrefix);
    let header = map_err(take_while1(|c: u8| c.is_ascii_uppercase()), |_: Error| {
        Error::BadCommand
    });
    let delimiter = map_err(tag(": "), |_: Error| Error::NoDelimiter);
    delimited(prefix, header, delimiter)(input)
}

fn content(input: &[u8]) -> IResult<&[u8], ResponseContent> {
    let content = alt((
        map(error, ResponseContent::Error),
        map(data, ResponseContent::Data),
    ));
    let terminator = map_err(line_ending, |_: Error| Error::NoTerminator);
    terminated(content, terminator)(input)
}

fn error(input: &[u8]) -> IResult<&[u8], i8> {
    let opening = tag("ERROR(");
    let code = map_err(cut(i8), |_: Error| Error::BadErrorCode);
    let closing = map_err(cut(tag(")")), |_: Error| Error::UnclosedErrorParen);
    delimited(opening, code, closing)(input)
}

fn data(input: &[u8]) -> IResult<&[u8], &[u8]> {
    take_till(|c| b"\r\n".contains(&c))(input)
}

/// Parser combinator that maps error
fn map_err<I, O, E1, E2, F, G>(mut parser: F, mut f: G) -> impl FnMut(I) -> nom::IResult<I, O, E2>
where
    F: nom::Parser<I, O, E1>,
    G: FnMut(E1) -> E2,
{
    move |input: I| parser.parse(input).map_err(|e| e.map(&mut f))
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn error() {
        let (rest, response) = response(b"+DR: ERROR(-1)\r\n").unwrap();
        assert!(rest.is_empty());
        assert_eq!(
            response,
            Response {
                header: b"DR",
                content: ResponseContent::Error(-1)
            }
        );
    }

    #[test]
    fn data() {
        let (rest, response) = response(b"+DR: not an error\r\n").unwrap();
        assert!(rest.is_empty());
        assert_eq!(
            response,
            Response {
                header: b"DR",
                content: ResponseContent::Data(b"not an error")
            }
        );
    }

    #[test]
    fn no_prefix() {
        let error = response(b"DR: not an error\r\n").unwrap_err();
        assert_eq!(error, nom::Err::Error(Error::NoPrefix));
    }

    #[test]
    fn bad_command() {
        let error = response(b"+: not an error\r\n").unwrap_err();
        assert_eq!(error, nom::Err::Error(Error::BadCommand));
    }

    #[test]
    fn unclodes_error_paren() {
        let error = response(b"+DR: ERROR(-1\r\n").unwrap_err();
        assert_eq!(error, nom::Err::Failure(Error::UnclosedErrorParen));
    }

    #[test]
    fn bad_error_code() {
        let error = response(b"+DR: ERROR(code)\r\n").unwrap_err();
        assert_eq!(error, nom::Err::Failure(Error::BadErrorCode));
    }

    #[test]
    fn no_delimiter() {
        let error = response(b"+DR not an error\r\n").unwrap_err();
        assert_eq!(error, nom::Err::Error(Error::NoDelimiter));
    }

    #[test]
    fn no_terminator() {
        let error = response(b"+DR: not an error").unwrap_err();
        assert_eq!(error, nom::Err::Error(Error::NoTerminator));
    }

    #[test]
    fn no_error_terminator() {
        let error = response(b"+DR: ERROR(-1)").unwrap_err();
        assert_eq!(error, nom::Err::Error(Error::NoTerminator));
    }
}