diff --git a/benches/resp.rs b/benches/resp.rs index 5723a74..f155d5a 100644 --- a/benches/resp.rs +++ b/benches/resp.rs @@ -3,7 +3,18 @@ use bytes::BytesMut; use criterion::{black_box, criterion_group, criterion_main, Criterion}; use simple_redis::{parse_frame, parse_frame_length, RespFrame}; -const DATA: &str = "+OK\r\n-ERR\r\n:1000\r\n$6\r\nfoobar\r\n$-1\r\n*2\r\n+hello\r\n$5\r\nworld\r\n+foo\r\n$3\r\nbar\r\n%2\r\n+foo\r\n,-123456.789\r\n+hello\r\n$5\r\nworld\r\n*3\r\n$3\r\nset\r\n$5\r\nhello\r\n$5\r\nworld\r\n%2\r\n+hello\r\n$5\r\nworld\r\n+foo\r\n$3\r\nbar\r\n"; +// resp frames covers all kinds of real-world redis requests and responses +// cmd 1: set key value +// cmd 1 response: OK +// cmd 2: get key +// cmd 2 response: value +// cmd 3: hset key field value +// cmd 3 response: ERR +// cmd 4: hget key field +// cmd 4 response: value +// cmd 5: sadd key member +// cmd 5 response: 1 +const DATA: &str = "*3\r\n$3\r\nSET\r\n$3\r\nkey\r\n$5\r\nvalue\r\n*1\r\n+OK\r\n*2\r\n$3\r\nGET\r\n$3\r\nkey\r\n$5\r\nvalue\r\n*4\r\n$4\r\nHSET\r\n$3\r\nkey\r\n$5\r\nfield\r\n$5\r\nvalue\r\n*1\r\n-ERR\r\n*3\r\n$4\r\nHGET\r\n$3\r\nkey\r\n$5\r\nfield\r\n$5\r\nvalue\r\n*3\r\n$4\r\nSADD\r\n$3\r\nkey\r\n$6\r\nmember\r\n:1\r\n"; fn v1_decode(buf: &mut BytesMut) -> Result> { use simple_redis::RespDecode; diff --git a/src/respv2/parser.rs b/src/respv2/parser.rs index fb1e52a..f0d53c4 100644 --- a/src/respv2/parser.rs +++ b/src/respv2/parser.rs @@ -75,12 +75,11 @@ fn error(input: &mut &[u8]) -> PResult { parse_string.map(SimpleError).parse_next(input) } -// - integer: ":1234\r\n" +// - integer: ":-1234\r\n", need to take care of the sign fn integer(input: &mut &[u8]) -> PResult { - let sign = opt(alt(('+', '-'))).parse_next(input)?.unwrap_or('+'); - let sign: i64 = if sign == '+' { 1 } else { -1 }; + let sign = opt('-').parse_next(input)?.is_some(); let v: i64 = terminated(digit1.parse_to(), CRLF).parse_next(input)?; - Ok(sign * v) + Ok(if sign { -v } else { v }) } // - null bulk string: "$-1\r\n"