Crate kramer

source ·
Expand description

An implementation of the redis protocol specification with an execution helper using the TcpStream provided by async-std.

Example

use kramer::{Command, StringCommand, Arity, Insertion};
use std::env::{var};
use std::io::prelude::*;

fn get_redis_url() -> String {
  let host = var("REDIS_HOST").unwrap_or(String::from("0.0.0.0"));
  let port = var("REDIS_PORT").unwrap_or(String::from("6379"));
  format!("{}:{}", host, port)
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
  let url = get_redis_url();
  let cmd = Command::Keys::<_, &str>("*");
  let mut stream = std::net::TcpStream::connect(url)?;
  write!(stream, "{}", cmd)?;
  write!(stream, "{}", StringCommand::Set(Arity::One(("name", "kramer")), None, Insertion::Always))?;
  Ok(())
}

Enums

  • The arity type here is used to mean a single or non-single container.
  • Redis authorization supports password and user/password authorization schemes.
  • The main Command enum here represents all of the different variants of redis commands that are supported by the library.
  • HashCommand represents the possible redis operations of keys that are a hash type.
  • Redis provides the ability to conditionally apply an inseration based on the existence of the a value that is equal.
  • Lists.
  • Redis responses may either be an array of values, a single value, or an error.
  • A response line is the type that is parsed from a single \r\n delimited string returned from the redis server.
  • A redis response value may either be empty, a bulk string, or an integer.
  • The SetCommand is used for working with redis keys that are sets: unique collections of values.
  • For lists, items can either be inserted on the left or right; this translates to whether or not the generated command is LPOP or RPOP (for example).
  • The StringCommand enum represents the most basic, key-value commands that redis offers; top-level keys with values being either strings or numbers.

Functions

  • Writes a command to the connection and will attempt to read a response.
  • By default, all commands will be formatted via the Display trait into the string representation that they would be sent over the wire as. This function should help users visualize commands in the format that they would issue them into the redis-cli as.
  • After sending a command, the read here is used to parse the response from our connection into the response enum.
  • This method will attempt to establish a new connection and execute the command.