-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
264 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
name: Release | ||
|
||
on: | ||
release: | ||
types: | ||
- created | ||
|
||
env: | ||
CARGO_TERM_COLOR: always | ||
|
||
jobs: | ||
release: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v2 | ||
- name: Run tests | ||
run: cargo test --verbose | ||
- name: Release on Crates.io | ||
run: | | ||
cargo login ${{ secrets.CARGO_TOKEN }} | ||
cargo publish |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
name: Rust | ||
|
||
on: | ||
push: | ||
branches: ["main"] | ||
pull_request: | ||
branches: ["main"] | ||
|
||
env: | ||
CARGO_TERM_COLOR: always | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
- name: Build | ||
run: cargo build --verbose | ||
- name: Run tests | ||
run: cargo test --verbose |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/target |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
[package] | ||
name = "tinyresp" | ||
version = "0.0.1" | ||
edition = "2021" | ||
authors = ["Luciano Mammino", "Roberto Gambuzzi"] | ||
description = "A tiny Rust library implementing the Redis Serialization Protocol (RESP)" | ||
documentation = "https://docs.rs/tinyresp" | ||
repository = "https://github.com/lmammino/tinyresp" | ||
keywords = ["redis"] | ||
categories = ["api-bindings", "parser-implementations"] | ||
license = "MIT" | ||
readme = "README.md" | ||
|
||
[dependencies] | ||
nom = "7.1.3" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License Copyright (c) 2024 Luciano Mammino, Roberto Gambuzzi | ||
|
||
Permission is hereby granted, | ||
free of charge, to any person obtaining a copy of this software and associated | ||
documentation files (the "Software"), to deal in the Software without | ||
restriction, including without limitation the rights to use, copy, modify, merge, | ||
publish, distribute, sublicense, and/or sell copies of the Software, and to | ||
permit persons to whom the Software is furnished to do so, subject to the | ||
following conditions: | ||
|
||
The above copyright notice and this permission notice | ||
(including the next paragraph) shall be included in all copies or substantial | ||
portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF | ||
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO | ||
EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR | ||
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
//! A simple parser for the RESP protocol | ||
//! Still under heavy development | ||
use nom::{ | ||
bytes::complete::{tag, take, take_while}, | ||
character::complete::{i32, i64, one_of}, | ||
combinator::{eof, verify}, | ||
sequence::terminated, | ||
IResult, | ||
}; | ||
use std::{ | ||
collections::{HashMap, HashSet}, | ||
hash::Hash, | ||
}; | ||
|
||
#[derive(Debug, PartialEq, Eq, Hash, Clone)] | ||
pub enum Value<'a> { | ||
SimpleString(&'a str), | ||
SimpleError(&'a str), | ||
Integer(i64), | ||
BulkString(&'a str), | ||
Array(Vec<Value<'a>>), | ||
Null, | ||
Boolean(bool), | ||
Double(&'a str), | ||
BigNumber(&'a str), | ||
BulkError(&'a str), | ||
VerbatimString(&'a str), | ||
// Map(HashMap<&'a str, Value<'a>>), | ||
// Set(HashSet<Value<'a>>), | ||
Pushes, | ||
} | ||
|
||
pub fn parse_value(input: &str) -> IResult<&str, Value> { | ||
let (input, type_char) = one_of("+-:$*_#,(!=%~>")(input)?; | ||
let parser = match type_char { | ||
'+' => parse_simple_string, | ||
'-' => parse_simple_error, | ||
':' => parse_integer, | ||
'$' => parse_bulk_string, | ||
'*' => todo!(), | ||
'_' => todo!(), | ||
'#' => todo!(), | ||
',' => todo!(), | ||
'(' => todo!(), | ||
'!' => todo!(), | ||
'=' => todo!(), | ||
'%' => todo!(), | ||
'~' => todo!(), | ||
'>' => todo!(), | ||
_ => unreachable!("Invalid type char"), | ||
}; | ||
|
||
terminated(parser, eof)(input) | ||
} | ||
|
||
fn u32_or_minus1(input: &str) -> IResult<&str, i32> { | ||
let (input, value) = verify(i32, |v| v >= &-1)(input)?; | ||
Ok((input, value)) | ||
} | ||
|
||
fn crlf(input: &str) -> IResult<&str, &str> { | ||
tag("\r\n")(input) | ||
} | ||
|
||
fn parse_simple_string_raw(input: &str) -> IResult<&str, &str> { | ||
terminated(take_while(|c| c != '\r' && c != '\n'), crlf)(input) | ||
} | ||
|
||
fn parse_simple_string(input: &str) -> IResult<&str, Value> { | ||
let (input, value) = parse_simple_string_raw(input)?; | ||
Ok((input, Value::SimpleString(value))) | ||
} | ||
|
||
fn parse_simple_error(input: &str) -> IResult<&str, Value> { | ||
let (input, value) = parse_simple_string_raw(input)?; | ||
Ok((input, Value::SimpleError(value))) | ||
} | ||
|
||
fn parse_integer(input: &str) -> IResult<&str, Value> { | ||
let (input, value) = terminated(i64, crlf)(input)?; | ||
Ok((input, Value::Integer(value))) | ||
} | ||
|
||
fn parse_bulk_string(input: &str) -> IResult<&str, Value> { | ||
let (input, length) = terminated(u32_or_minus1, crlf)(input)?; | ||
if length == -1 { | ||
return Ok((input, Value::Null)); | ||
} | ||
|
||
let (input, value) = terminated(take(length as usize), crlf)(input)?; | ||
Ok((input, Value::BulkString(value))) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_parse_simple_string() { | ||
assert_eq!(parse_value("+OK\r\n"), Ok(("", Value::SimpleString("OK")))); | ||
assert!(parse_value("+O\nK\r\n").is_err()); | ||
assert!(parse_value("+OK\r\nTHIS_SHOULD_NOT_BE_HERE").is_err()); | ||
} | ||
|
||
#[test] | ||
fn test_parse_simple_error() { | ||
assert_eq!( | ||
parse_value("-Error message\r\n"), | ||
Ok(("", Value::SimpleError("Error message"))) | ||
); | ||
assert_eq!( | ||
parse_value("-ERR unknown command 'asdf'\r\n"), | ||
Ok(("", Value::SimpleError("ERR unknown command 'asdf'"))) | ||
); | ||
assert_eq!( | ||
parse_value("-WRONGTYPE Operation against a key holding the wrong kind of value\r\n"), | ||
Ok(( | ||
"", | ||
Value::SimpleError( | ||
"WRONGTYPE Operation against a key holding the wrong kind of value" | ||
) | ||
)) | ||
); | ||
assert!(parse_value("-Error\nmessage\r\n").is_err()); | ||
assert!(parse_value("-Error message\r\nTHIS_SHOULD_NOT_BE_HERE").is_err()); | ||
} | ||
|
||
#[test] | ||
fn test_parse_integer() { | ||
assert_eq!(parse_value(":1000\r\n"), Ok(("", Value::Integer(1000)))); | ||
assert_eq!(parse_value(":-1000\r\n"), Ok(("", Value::Integer(-1000)))); | ||
assert!(parse_value(":1000\n").is_err()); | ||
assert!(parse_value(":1000\r\nTHIS_SHOULD_NOT_BE_HERE").is_err()); | ||
} | ||
|
||
#[test] | ||
fn test_parse_bulk_string() { | ||
assert_eq!( | ||
parse_value("$5\r\nhello\r\n"), | ||
Ok(("", Value::BulkString("hello"))) | ||
); | ||
assert_eq!(parse_value("$0\r\n\r\n"), Ok(("", Value::BulkString("")))); | ||
assert_eq!(parse_value("$-1\r\n"), Ok(("", Value::Null))); | ||
assert_eq!( | ||
parse_value("$10\r\nhello\r\nfoo\r\n"), | ||
Ok(("", Value::BulkString("hello\r\nfoo"))) | ||
); | ||
assert!(parse_value("$-2\r\n").is_err()); | ||
assert!(parse_value("$10\r\n12345\r\n").is_err()); | ||
assert!(parse_value("$10\r\n12345\r\n").is_err()); | ||
} | ||
} |