use super::{Command, CommandError};
use crate::codec_redis::{Request, Response};
pub fn Select(db: u32) -> SelectCommand {
SelectCommand(Request::Array(vec![
Request::from_static("SELECT"),
Request::BulkInteger(db as i64),
]))
}
pub struct SelectCommand(Request);
impl Command for SelectCommand {
type Output = bool;
fn to_request(self) -> Request {
self.0
}
fn to_output(val: Response) -> Result<Self::Output, CommandError> {
match val {
Response::String(val) => Ok(val == "OK"),
_ => Ok(false),
}
}
}
pub fn Ping() -> PingCommand {
PingCommand(Request::Array(vec![Request::from_static("PING")]))
}
pub struct PingCommand(Request);
impl Command for PingCommand {
type Output = String;
fn to_request(self) -> Request {
self.0
}
fn to_output(val: Response) -> Result<Self::Output, CommandError> {
match val {
Response::String(val) => Ok(val.to_string()),
Response::Error(val) => Err(CommandError::Error(val)),
_ => Err(CommandError::Output("Unknown response", val)),
}
}
}