use crate::rcli::session::Session;
use kevy_resp::Reply;
pub(crate) fn key_count(s: &mut Session) -> Result<u64, Vec<u8>> {
match s.request(&[b"DBSIZE"]) {
Ok(Reply::Int(n)) => Ok(n.max(0) as u64),
Ok(Reply::Error(msg)) => Err([b"Couldn't determine DBSIZE: ".as_slice(), &msg].concat()),
Ok(_) => Err(b"Non INTEGER response from DBSIZE!".to_vec()),
Err(_) => Err(b"\nI/O error".to_vec()),
}
}
pub(crate) fn allow_replica_reads(s: &mut Session) -> Result<(), Vec<u8>> {
match s.request(&[b"READONLY"]) {
Ok(Reply::Error(msg))
if msg != b"ERR This instance has cluster support disabled"
&& !msg.starts_with(b"ERR unknown command") =>
{
Err([b"Error: ".as_slice(), &msg].concat())
}
Ok(_) => Ok(()),
Err(_) => Err(b"\nI/O error".to_vec()),
}
}
pub(crate) fn pad(text: &[u8], width: usize, align: Align) -> Vec<u8> {
let fill = vec![b' '; width.saturating_sub(text.len())];
match align {
Align::Left => [text, &fill].concat(),
Align::Right => [&fill, text].concat(),
}
}
#[derive(Clone, Copy, PartialEq, Eq)]
pub(crate) enum Align {
Left,
Right,
}