simple_redis/
types.rs

1//! # types
2//!
3//! Defines the various types and aliases used or exposed by the simple_redis library.
4//!
5
6#[cfg(test)]
7#[path = "./types_test.rs"]
8mod types_test;
9
10use std::error::Error;
11use std::fmt;
12use std::fmt::Display;
13
14#[derive(Debug)]
15/// Holds the error information
16pub enum RedisError {
17    /// Root redis error
18    RedisError(redis::RedisError),
19    /// Description text of the error reason
20    Description(&'static str),
21}
22
23impl Display for RedisError {
24    /// Formats the value using the given formatter.
25    fn fmt(&self, format: &mut fmt::Formatter) -> Result<(), fmt::Error> {
26        match self {
27            Self::RedisError(ref cause) => cause.fmt(format),
28            Self::Description(description) => description.fmt(format),
29        }
30    }
31}
32
33impl Error for RedisError {
34    fn source(&self) -> Option<&(dyn Error + 'static)> {
35        match self {
36            Self::RedisError(error) => Some(error),
37            Self::Description(_) => None,
38        }
39    }
40}
41
42/// Defines a redis command argument
43pub trait RedisArg: Sized + ToString {}
44
45macro_rules! as_redis_arg {
46    ($t:ty) => {
47        impl RedisArg for $t {}
48    };
49}
50
51impl<'a> RedisArg for &'a str {}
52
53as_redis_arg!(u8);
54as_redis_arg!(i8);
55as_redis_arg!(i16);
56as_redis_arg!(u16);
57as_redis_arg!(i32);
58as_redis_arg!(u32);
59as_redis_arg!(i64);
60as_redis_arg!(u64);
61as_redis_arg!(i128);
62as_redis_arg!(u128);
63as_redis_arg!(f32);
64as_redis_arg!(f64);
65as_redis_arg!(isize);
66as_redis_arg!(usize);
67as_redis_arg!(bool);
68
69/// PubSub message
70pub type Message = redis::Msg;
71
72/// Redis result which either holds a value or a Redis error
73pub type RedisResult<T> = Result<T, RedisError>;
74
75/// Holds empty result or error
76pub type RedisEmptyResult = RedisResult<()>;
77
78/// Holds string result or error
79pub type RedisStringResult = RedisResult<String>;
80
81/// Holds bool result or error
82pub type RedisBoolResult = RedisResult<bool>;
83
84#[derive(Debug, Clone, Copy, Default)]
85/// Enable to modify blocking operations.
86pub struct Interrupts {
87    /// Notify blocking operation to stop
88    pub stop: bool,
89    /// Next polling time in millies
90    pub next_polling_time: Option<u64>,
91}
92
93impl Interrupts {
94    /// Returns a new instance.
95    pub fn new() -> Interrupts {
96        Default::default()
97    }
98}