mco_redis/cmd/
utils.rs

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