Advertisement
Guest User

Untitled

a guest
Sep 21st, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. use serde::ser::{Serialize, Serializer};
  2.  
  3. #[derive(Debug)]
  4. pub enum RedisType {
  5. SimpleString(String),
  6. Array(Vec<RedisType>),
  7. }
  8.  
  9. impl Serialize for RedisType {
  10. fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
  11. where
  12. S: Serializer,
  13. {
  14. let raw_str = match self {
  15. RedisType::SimpleString(s) => format!("+{}\r\n", s),
  16. RedisType::Array(v) => format!(
  17. "*{}\r\n{}",
  18. v.len(),
  19. v.iter().map(|e| e.serialize(serializer)).join("")
  20. ),
  21. };
  22. serializer.serialize_str(raw_str.as_str())
  23. }
  24. }
  25.  
  26. #[cfg(test)]
  27. mod tests {
  28. use super::*;
  29.  
  30. #[test]
  31. fn test_serialize_redis_type() {
  32. let actual = RedisType::SimpleString("foo".to_string())
  33. .serialize()
  34. .unwrap();
  35. assert_eq!(actual, "+foo\r\n");
  36. }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement