Basic Redis Protocol specification in Rust

Overview

Basic Redis protocol implementation

Implements basic RESP encoding and parsing.

The client

Connecting to Redis

#[tokio::main]
async fn main() -> Result<()> {
  let mut redis = Redis::connect("127.0.0.1:6379").await?;

  Ok(())
}

Sending commands to Redis

println!("ERROR: {}", e), Reply::Ok(data_type) => println!("OK: {:?}", data_type), }; Ok(()) } ">
#[tokio::main]
async fn main() -> Result<()> {
  let mut redis = Redis::connect("127.0.0.1:6379").await?;

  match redis.send("LLEN mylist").await? {
    Reply::Error(e) => println!("ERROR: {}", e),
    Reply::Ok(data_type) => println!("OK: {:?}", data_type),
  };

  Ok(())
}

The RESP parser

Parsing Simple Strings

use crate::resp;

assert_eq!(
  resp::parse(b"+OK\r\n".to_vec()),
  Ok(DataType::SimpleString(String::from("OK"))),
)

Parsing Error

use crate::resp;

assert_eq!(
  resp::parse(b"-ERR unknown command 'foobar'\r\n".to_vec()),
  Ok(DataType::Error(String::from(
    "ERR unknown command 'foobar'",
  ))),
)

Parsing integers

use crate::resp;

assert_eq!(resp::parse(b":0\r\n".to_vec()), Ok(DataType::Int(0))),
assert_eq!(resp::parse(b":1000\r\n".to_vec()), Ok(DataType::Int(1000))),
assert_eq!(resp::parse(b":-3\r\n".to_vec()), Ok(DataType::Int(-3))),

Parsing Bulk Strings

use crate::resp;

assert_eq!(
  resp::parse(b"$0\r\n\r\n".to_vec()),
  Ok(DataType::BulkString(String::new()))
)
assert_eq!(
  resp::parse(b"$6\r\nfoobar\r\n".to_vec()),
  Ok(DataType::BulkString(String::from("foobar"))),
)

Parsing Arrays

use crate::resp;

assert_eq!(resp::parse(b"*0\r\n".to_vec()), Ok(DataType::Array(vec![])))

assert_eq!(
  resp::parse(b"*3\r\n:1\r\n:2\r\n:3\r\n".to_vec()),
  Ok(DataType::Array(vec![
      DataType::Int(1),
      DataType::Int(2),
      DataType::Int(3),
    ])
  ),
)

Parsing Null

use crate::resp;

assert_eq!(resp::parse(b"$-1\r\n".to_vec()), Ok(DataType::Null))
assert_eq!(resp::parse(b"*-1\r\n".to_vec()), Ok(DataType::Null))

The RESP encoder

Encoding basic commands

assert_eq!(
  resp::encode(&"LLEN mylist"),
  Ok(String::from("*2\r\n$4\r\nLLEN\r\n$6\r\nmylist\r\n"))
)
assert_eq!(
  resp::encode(&r#"SETEX mykey 10 "Hello""#),
  Ok(String::from("*4\r\n$5\r\nSETEX\r\n$5\r\nmykey\r\n:10\r\n$7\r\n\"Hello\"\r\n")),
)
You might also like...
Sharded, concurrent mini redis that support http interface implemented in rust

Rudis A mini version of redis server that provides http interface implemented in Rust. The in-memorry kv-storage is sharded and concurrent safe. Inspi

Learning Rust by implementing parts of redis.

Redis This is a simple CLI Redis inspired project that supports the GET, SET, and INCR commands. Run it Have rust installed (if you don't, visit rustu

RedisLess is a fast, lightweight, embedded and scalable in-memory Key/Value store library compatible with the Redis API.

RedisLess is a fast, lightweight, embedded and scalable in-memory Key/Value store library compatible with the Redis API.

RedisJSON - a JSON data type for Redis

RedisJSON RedisJSON is a Redis module that implements ECMA-404 The JSON Data Interchange Standard as a native data type. It allows storing, updating a

[POC] Redis Module for TiKV

RedisTikvPoc [POC] Redis Module for TiKV This is a POC repository. This Redis Module will add branch new Redis commands to operate TiKV data. After bu

Incomplete Redis client and server implementation using Tokio - for learning purposes only

mini-redis mini-redis is an incomplete, idiomatic implementation of a Redis client and server built with Tokio. The intent of this project is to provi

A port of `java.util.*SummaryStatistics` as a Redis Module

RedisNumbersStats RedisNumbersStats is a Redis module that implements a Redis version of the Java Util *SummaryStatistics classes, such as DoubleSumma

A simplified version of a Redis server supporting SET/GET commands

This is a starting point for Rust solutions to the "Build Your Own Redis" Challenge. In this challenge, you'll build a toy Redis clone that's capable

Macros for redis-rs to serialize and deserialize structs automatically

redis-macros Simple macros and wrappers to redis-rs to automatically serialize and deserialize structs with serde. Installation To install it, simply

Owner
Bruno
Bruno
🦀 REST API client implementation for freee, auto-generated from OpenAPI specification.

freee-rs REST API client implementation for freee, auto-generated from OpenAPI specification. Getting Started Add to your Cargo.toml as follows: [depe

Naoki Ikeguchi 3 Jul 14, 2022
Redis re-implemented in Rust.

rsedis Redis re-implemented in Rust. Why? To learn Rust. Use Cases rsedis does not rely on UNIX-specific features. Windows users can run it as a repla

Sebastian Waisbrot 1.6k Jan 6, 2023
Redis library for rust

redis-rs Redis-rs is a high level redis library for Rust. It provides convenient access to all Redis functionality through a very flexible but low-lev

Armin Ronacher 2.8k Jan 8, 2023
A rust Key-Value store based on Redis.

Key-Value Store A Key-Value store that uses Redis to store data. Built using an async web framework in Rust with a full Command-Line interface and log

Miguel David Salcedo 0 Jan 14, 2022
Rewrite Redis in Rust for evaluation and learning.

Drill-Redis This library has been created for the purpose of evaluating Rust functionality and performance. As such, it has not been fully tested. The

Akira Kawahara 3 Oct 18, 2022
Redis compatible server framework for Rust

Redis compatible server framework for Rust Features Create a fast custom Redis compatible server in Rust Simple API. Support for pipelining and telnet

Josh Baker 61 Nov 12, 2022
An intentionally-limited Rust implementation of the Redis server with no external dependencies.

lil-redis An intentionally-limited Rust implementation of the Redis server. lil redis is an accessible implementation of a very basic Redis server (wi

Miguel Piedrafita 37 Jan 1, 2023
📺 Netflix in Rust/ React-TS/ NextJS, Actix-Web, Async Apollo-GraphQl, Cassandra/ ScyllaDB, Async SQLx, Kafka, Redis, Tokio, Actix, Elasticsearch, Influxdb Iox, Tensorflow, AWS

Fullstack Movie Streaming Platform ?? Netflix in RUST/ NextJS, Actix-Web, Async Apollo-GraphQl, Cassandra/ ScyllaDB, Async SQLx, Spark, Kafka, Redis,

null 34 Apr 17, 2023
Lightweight async Redis client with connection pooling written in pure Rust and 100% memory safe

redi-rs (or redirs) redi-rs is a Lightweight Redis client with connection pooling written in Rust and 100% memory safe redi-rs is a Redis client writt

Oğuz Türkay 4 May 20, 2023
Simple and flexible queue implementation for Rust with support for multiple backends (Redis, RabbitMQ, SQS, etc.)

Omniqueue Omniqueue is an abstraction layer over queue backends for Rust. It includes support for RabbitMQ, Redis streams, and SQS out of the box. The

Svix 8 May 26, 2023