#[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 | RespValue::Array(_) => Ok(None),
RespValue::Integer(value) => Ok(Some(value.to_string())),
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))
}
}
}
pub fn as_integer(&self) -> Option<i64> {
match self {
RespValue::Integer(value) => Some(*value),
_ => None,
}
}
pub fn as_array(&self) -> Option<&[RespValue]> {
match self {
RespValue::Array(items) => Some(items),
_ => None,
}
}
pub fn is_null(&self) -> bool {
matches!(self, RespValue::Null)
}
}
#[cfg(test)]
mod tests {
use super::RespValue;
#[test]
fn as_string_does_not_fail_on_an_array_or_integer() {
let message = RespValue::Array(vec![
RespValue::Bulk(b"message".to_vec()),
RespValue::Bulk(b"news".to_vec()),
RespValue::Bulk(b"hello".to_vec()),
]);
assert_eq!(message.as_string().unwrap(), None);
assert_eq!(
message.as_array().unwrap()[2]
.as_string()
.unwrap()
.as_deref(),
Some("hello")
);
assert_eq!(
RespValue::Integer(3).as_string().unwrap().as_deref(),
Some("3")
);
}
}