ruvio-client 0.1.0

RESP2 client for the Ruvio key-value server
Documentation
/// One RESP2 value.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum RespValue {
    /// A simple string, such as `OK`.
    Simple(String),
    /// An error reply. [`crate::Client::execute`] returns this as [`crate::Error::Server`].
    Error(String),
    /// A signed integer.
    Integer(i64),
    /// A bulk string, including the empty bulk.
    Bulk(Vec<u8>),
    /// An array of values.
    Array(Vec<RespValue>),
    /// A null bulk or a null array.
    Null,
}

impl RespValue {
    /// UTF-8 text of a simple string or a bulk string. `None` when the value is null.
    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")))
            }
        }
    }
}