1
0

mod.rs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. pub mod checksum;
  2. pub mod locked;
  3. use crate::{error::Error, value_try_from, value_vec_try_from};
  4. use bytes::{Bytes, BytesMut};
  5. use redis_zero_protocol_parser::Value as ParsedValue;
  6. use std::{
  7. collections::{HashMap, HashSet, VecDeque},
  8. convert::{TryFrom, TryInto},
  9. str::FromStr,
  10. };
  11. #[derive(Debug, PartialEq, Clone)]
  12. pub enum Value {
  13. Hash(locked::Value<HashMap<Bytes, Bytes>>),
  14. List(locked::Value<VecDeque<checksum::Value>>),
  15. Set(locked::Value<HashSet<Bytes>>),
  16. Array(Vec<Value>),
  17. Blob(Bytes),
  18. String(String),
  19. Err(String, String),
  20. Integer(i64),
  21. Boolean(bool),
  22. Float(f64),
  23. BigInteger(i128),
  24. Null,
  25. Queued,
  26. Ok,
  27. }
  28. impl From<&Value> for Vec<u8> {
  29. fn from(value: &Value) -> Vec<u8> {
  30. match value {
  31. Value::Null => b"*-1\r\n".to_vec(),
  32. Value::Array(x) => {
  33. let mut s: Vec<u8> = format!("*{}\r\n", x.len()).into();
  34. for i in x.iter() {
  35. let b: Vec<u8> = i.into();
  36. s.extend(b);
  37. }
  38. s
  39. }
  40. Value::Integer(x) => format!(":{}\r\n", x).into(),
  41. Value::BigInteger(x) => format!("({}\r\n", x).into(),
  42. Value::Float(x) => format!(",{}\r\n", x).into(),
  43. Value::Blob(x) => {
  44. let s = format!("${}\r\n", x.len());
  45. let mut s: BytesMut = s.as_str().as_bytes().into();
  46. s.extend_from_slice(x);
  47. s.extend_from_slice(b"\r\n");
  48. s.to_vec()
  49. }
  50. Value::Err(x, y) => format!("-{} {}\r\n", x, y).into(),
  51. Value::String(x) => format!("+{}\r\n", x).into(),
  52. Value::Queued => "+QUEUED\r\n".into(),
  53. Value::Ok => "+OK\r\n".into(),
  54. _ => b"-WRONGTYPE Operation against a key holding the wrong kind of value\r\n".to_vec(),
  55. }
  56. }
  57. }
  58. impl TryFrom<&Value> for i64 {
  59. type Error = Error;
  60. fn try_from(val: &Value) -> Result<Self, Self::Error> {
  61. match val {
  62. Value::BigInteger(x) => (*x).try_into().map_err(|_| Error::NotANumber),
  63. Value::Integer(x) => Ok(*x),
  64. Value::Blob(x) => bytes_to_number::<i64>(x),
  65. Value::String(x) => x.parse::<i64>().map_err(|_| Error::NotANumber),
  66. _ => Err(Error::NotANumber),
  67. }
  68. }
  69. }
  70. impl TryFrom<&Value> for f64 {
  71. type Error = Error;
  72. fn try_from(val: &Value) -> Result<Self, Self::Error> {
  73. match val {
  74. Value::Float(x) => Ok(*x),
  75. Value::Blob(x) => bytes_to_number::<f64>(x),
  76. Value::String(x) => x.parse::<f64>().map_err(|_| Error::NotANumber),
  77. _ => Err(Error::NotANumber),
  78. }
  79. }
  80. }
  81. pub fn bytes_to_number<T: FromStr>(bytes: &Bytes) -> Result<T, Error> {
  82. let x = String::from_utf8_lossy(bytes);
  83. x.parse::<T>().map_err(|_| Error::NotANumber)
  84. }
  85. impl From<Value> for Vec<u8> {
  86. fn from(value: Value) -> Vec<u8> {
  87. (&value).into()
  88. }
  89. }
  90. impl<'a> From<&ParsedValue<'a>> for Value {
  91. fn from(value: &ParsedValue) -> Self {
  92. match value {
  93. ParsedValue::String(x) => Self::String((*x).to_string()),
  94. ParsedValue::Blob(x) => Self::Blob(Bytes::copy_from_slice(*x)),
  95. ParsedValue::Array(x) => {
  96. Self::Array(x.iter().map(|x| Value::try_from(x).unwrap()).collect())
  97. }
  98. ParsedValue::Boolean(x) => Self::Boolean(*x),
  99. ParsedValue::BigInteger(x) => Self::BigInteger(*x),
  100. ParsedValue::Integer(x) => Self::Integer(*x),
  101. ParsedValue::Float(x) => Self::Float(*x),
  102. ParsedValue::Error(x, y) => Self::Err((*x).to_string(), (*y).to_string()),
  103. ParsedValue::Null => Self::Null,
  104. }
  105. }
  106. }
  107. value_try_from!(f64, Value::Float);
  108. value_try_from!(i32, Value::Integer);
  109. value_try_from!(i64, Value::Integer);
  110. value_try_from!(i128, Value::BigInteger);
  111. impl From<usize> for Value {
  112. fn from(value: usize) -> Value {
  113. Value::Integer(value as i64)
  114. }
  115. }
  116. impl From<&str> for Value {
  117. fn from(value: &str) -> Value {
  118. Value::Blob(Bytes::copy_from_slice(value.as_bytes()))
  119. }
  120. }
  121. impl From<HashMap<Bytes, Bytes>> for Value {
  122. fn from(value: HashMap<Bytes, Bytes>) -> Value {
  123. Value::Hash(locked::Value::new(value))
  124. }
  125. }
  126. impl From<VecDeque<checksum::Value>> for Value {
  127. fn from(value: VecDeque<checksum::Value>) -> Value {
  128. Value::List(locked::Value::new(value))
  129. }
  130. }
  131. impl From<HashSet<Bytes>> for Value {
  132. fn from(value: HashSet<Bytes>) -> Value {
  133. Value::Set(locked::Value::new(value))
  134. }
  135. }
  136. value_vec_try_from!(&str);
  137. impl From<String> for Value {
  138. fn from(value: String) -> Value {
  139. value.as_str().into()
  140. }
  141. }
  142. impl From<Vec<Value>> for Value {
  143. fn from(value: Vec<Value>) -> Value {
  144. Value::Array(value)
  145. }
  146. }