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
use std::task::Poll;

use ntex::{io::IoBoxed, util::poll_fn, util::ready, util::Either};

use super::cmd::Command;
use super::codec::Codec;
use super::errors::{CommandError, Error};

/// Redis client
pub struct SimpleClient {
    io: IoBoxed,
}

impl SimpleClient {
    /// Create new simple client
    pub(crate) fn new(io: IoBoxed) -> Self {
        SimpleClient { io }
    }

    /// Execute redis command
    pub async fn exec<U>(&self, cmd: U) -> Result<U::Output, CommandError>
    where
        U: Command,
    {
        self.io.encode(cmd.to_request(), &Codec)?;

        poll_fn(|cx| match ready!(self.io.poll_recv(&Codec, cx)) {
            Ok(Some(item)) => Poll::Ready(U::to_output(
                item.into_result().map_err(CommandError::Error)?,
            )),
            Err(Either::Left(err)) => Poll::Ready(Err(CommandError::Protocol(err))),
            Err(Either::Right(err)) => Poll::Ready(Err(CommandError::Protocol(err.into()))),
            Ok(None) => Poll::Ready(Err(CommandError::Protocol(Error::Disconnected))),
        })
        .await
    }
}