ntex_redis/cmd/
utils.rs

1use ntex::util::Bytes;
2use std::convert::TryFrom;
3
4use super::{Command, CommandError};
5use crate::codec::{Request, Response};
6
7pub struct BulkOutputCommand(pub(crate) Request);
8
9impl Command for BulkOutputCommand {
10    type Output = Option<Bytes>;
11
12    fn to_request(self) -> Request {
13        self.0
14    }
15
16    fn to_output(val: Response) -> Result<Self::Output, CommandError> {
17        match val {
18            Response::Nil => Ok(None),
19            Response::Bytes(val) => Ok(Some(val)),
20            _ => Err(CommandError::Output("Cannot parse response", val)),
21        }
22    }
23}
24
25pub struct IntOutputCommand(pub(crate) Request);
26
27impl Command for IntOutputCommand {
28    type Output = i64;
29
30    fn to_request(self) -> Request {
31        self.0
32    }
33
34    fn to_output(val: Response) -> Result<Self::Output, CommandError> {
35        match val {
36            Response::Integer(val) => Ok(val),
37            _ => Err(CommandError::Output("Cannot parse response", val)),
38        }
39    }
40}
41
42pub struct BoolOutputCommand(pub(crate) Request);
43
44impl Command for BoolOutputCommand {
45    type Output = bool;
46
47    fn to_request(self) -> Request {
48        self.0
49    }
50
51    fn to_output(val: Response) -> Result<Self::Output, CommandError> {
52        Ok(bool::try_from(val)?)
53    }
54}