#[derive(Debug, Clone, PartialEq, Eq)]
pub enum RespValue {
Simple(String),
Error(String),
Integer(i64),
Bulk(Vec<u8>),
Array(Vec<RespValue>),
Null,
}
impl RespValue {
pub fn as_string(&self) -> Result<Option<String>, crate::Error> {
match self {
RespValue::Null => Ok(None),
RespValue::Simple(text) | RespValue::Error(text) => Ok(Some(text.clone())),
RespValue::Bulk(bytes) => {
let text = String::from_utf8(bytes.clone())
.map_err(|_| crate::Error::Protocol("bulk string is not UTF-8".to_owned()))?;
Ok(Some(text))
}
RespValue::Integer(_) | RespValue::Array(_) => {
Err(crate::Error::Protocol(format!("{self:?} is not a string")))
}
}
}
}