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
#![feature(async_await)]
#![deny(missing_docs)]

//! Asyncronous redis client built using futures and async await, with optional connection pooling.
//! ```
//! #![feature(async_await)]
//! use darkredis::*;
//!
//! # #[runtime::main]
//! # async fn main() {
//! // Create a connection pool with 4 connections
//! let pool = ConnectionPool::create("127.0.0.1:6379".into(), None, 4).await.unwrap();
//! let mut connection = pool.get().await; // Grab a connection from the pool
//!
//! connection.set("some-key", "Hello, world!").await.unwrap();
//! assert_eq!(connection.get("some-key").await.unwrap(), Some("Hello, world!".into()));
//! # connection.del("some-key").await.unwrap();
//! # }
//! ```

#[macro_use]
extern crate quick_error;

mod command;
mod connection;
mod connectionpool;
mod error;

#[cfg(test)]
mod test;

pub use command::{Command, CommandList};
pub use connection::Connection;
pub use connectionpool::ConnectionPool;
pub use error::Error;

///Result type used in the whole crate.
pub type Result<T> = std::result::Result<T, Error>;

///Enum depicting the various possible responses one can get from Redis.
#[derive(Debug, PartialEq)]
pub enum Value {
    ///A Redis `OK` response.
    Ok,
    ///Nil Response.
    Nil,
    ///Array response.
    Array(Vec<Value>),
    ///Integer response.
    Integer(isize),
    ///String response. This cannot be a `String` type, because Redis strings need not be valid UTF-8, unlike Rust.
    String(Vec<u8>),
}

impl Value {
    ///Returns the inner `isize` of a [`Value::Integer`](crate::Value::Integer).
    ///# Panics
    ///Panics if `self` is not a [`Value::Integer`](crate::Value::Integer)
    #[inline]
    pub fn unwrap_integer(self) -> isize {
        if let Value::Integer(i) = self {
            i
        } else {
            panic!("expected integer value, got {:?}", self)
        }
    }

    ///Returns the inner `Vec<Value>` of a `Value::Array`.
    ///# Panics
    ///Panics if `self` is not a [`Value::Array`](crate::Value::Array)
    #[inline]
    pub fn unwrap_array(self) -> Vec<Value> {
        if let Value::Array(a) = self {
            a
        } else {
            panic!("expected array value, got {:?}", self)
        }
    }

    ///Returns the inner `Vec<u8>` of a [`Value::String`](crate::Value::String).
    ///# Panics
    ///Panics if `self` is not a [`Value::String`](crate::Value::String)
    #[inline]
    pub fn unwrap_string(self) -> Vec<u8> {
        if let Value::String(s) = self {
            s
        } else {
            panic!("expected string value, got {:?}", self)
        }
    }

    ///Like `unwrap_string`, but returns an `Option` instead of panicking.
    #[inline]
    pub fn optional_string(self) -> Option<Vec<u8>> {
        match self {
            Value::String(s) => Some(s),
            _ => None,
        }
    }

    ///Like `unwrap_array`, but returns an `Option` instead of panicking.
    #[inline]
    pub fn optional_array(self) -> Option<Vec<Value>> {
        match self {
            Value::Array(a) => Some(a),
            _ => None,
        }
    }

    ///Like `unwrap_integer`, but returns an `Option` instead of panicking.
    #[inline]
    pub fn optional_integer(self) -> Option<isize> {
        match self {
            Value::Integer(i) => Some(i),
            _ => None,
        }
    }
}