redis_oxide/core/
value.rs

1//! RESP (`REdis` Serialization Protocol) value types
2
3use crate::core::error::{RedisError, RedisResult};
4use bytes::Bytes;
5
6/// RESP protocol value
7#[derive(Debug, Clone, PartialEq, Eq)]
8pub enum RespValue {
9    /// Simple string: +OK\r\n
10    SimpleString(String),
11    /// Error: -ERR message\r\n
12    Error(String),
13    /// Integer: :1000\r\n
14    Integer(i64),
15    /// Bulk string: $6\r\nfoobar\r\n
16    BulkString(Bytes),
17    /// Null bulk string: $-1\r\n
18    Null,
19    /// Array: *2\r\n$3\r\nfoo\r\n$3\r\nbar\r\n
20    Array(Vec<RespValue>),
21}
22
23impl RespValue {
24    /// Convert to a string if possible
25    ///
26    /// # Errors
27    ///
28    /// Returns an error if the value cannot be converted to a string.
29    pub fn as_string(&self) -> RedisResult<String> {
30        match self {
31            Self::SimpleString(s) => Ok(s.clone()),
32            Self::BulkString(b) => String::from_utf8(b.to_vec())
33                .map_err(|e| RedisError::Type(format!("Invalid UTF-8: {e}"))),
34            Self::Null => Err(RedisError::Type("Value is null".to_string())),
35            _ => Err(RedisError::Type(format!(
36                "Cannot convert {self:?} to string"
37            ))),
38        }
39    }
40
41    /// Convert to an integer if possible
42    ///
43    /// # Errors
44    ///
45    /// Returns an error if the value cannot be converted to an integer.
46    pub fn as_int(&self) -> RedisResult<i64> {
47        match self {
48            Self::Integer(i) => Ok(*i),
49            Self::BulkString(b) => {
50                let s = String::from_utf8(b.to_vec())
51                    .map_err(|e| RedisError::Type(format!("Invalid UTF-8: {e}")))?;
52                s.parse::<i64>()
53                    .map_err(|e| RedisError::Type(format!("Cannot parse integer: {e}")))
54            }
55            _ => Err(RedisError::Type(format!(
56                "Cannot convert {self:?} to integer"
57            ))),
58        }
59    }
60
61    /// Convert to bytes if possible
62    ///
63    /// # Errors
64    ///
65    /// Returns an error if the value cannot be converted to bytes.
66    pub fn as_bytes(&self) -> RedisResult<Bytes> {
67        match self {
68            Self::BulkString(b) => Ok(b.clone()),
69            Self::SimpleString(s) => Ok(Bytes::from(s.as_bytes().to_vec())),
70            Self::Null => Err(RedisError::Type("Value is null".to_string())),
71            _ => Err(RedisError::Type(format!(
72                "Cannot convert {self:?} to bytes"
73            ))),
74        }
75    }
76
77    /// Convert to an array if possible
78    ///
79    /// # Errors
80    ///
81    /// Returns an error if the value cannot be converted to an array.
82    pub fn as_array(&self) -> RedisResult<Vec<Self>> {
83        match self {
84            Self::Array(arr) => Ok(arr.clone()),
85            _ => Err(RedisError::Type(format!(
86                "Cannot convert {self:?} to array"
87            ))),
88        }
89    }
90
91    /// Check if this is a null value
92    #[must_use]
93    pub const fn is_null(&self) -> bool {
94        matches!(self, Self::Null)
95    }
96
97    /// Check if this is an error
98    #[must_use]
99    pub const fn is_error(&self) -> bool {
100        matches!(self, Self::Error(_))
101    }
102
103    /// Extract error message if this is an error
104    #[must_use]
105    pub fn into_error(self) -> Option<String> {
106        match self {
107            Self::Error(msg) => Some(msg),
108            _ => None,
109        }
110    }
111}
112
113impl From<String> for RespValue {
114    fn from(s: String) -> Self {
115        Self::BulkString(Bytes::from(s.into_bytes()))
116    }
117}
118impl From<&str> for RespValue {
119    fn from(s: &str) -> Self {
120        Self::BulkString(Bytes::from(s.as_bytes().to_vec()))
121    }
122}
123impl From<i64> for RespValue {
124    fn from(i: i64) -> Self {
125        Self::Integer(i)
126    }
127}
128impl From<Vec<u8>> for RespValue {
129    fn from(b: Vec<u8>) -> Self {
130        Self::BulkString(Bytes::from(b))
131    }
132}
133impl From<Bytes> for RespValue {
134    fn from(b: Bytes) -> Self {
135        Self::BulkString(b)
136    }
137}