Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use serde::ser::{Serialize, Serializer};
- #[derive(Debug)]
- pub enum RedisType {
- SimpleString(String),
- Array(Vec<RedisType>),
- }
- impl Serialize for RedisType {
- fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
- where
- S: Serializer,
- {
- let raw_str = match self {
- RedisType::SimpleString(s) => format!("+{}\r\n", s),
- RedisType::Array(v) => format!(
- "*{}\r\n{}",
- v.len(),
- v.iter().map(|e| e.serialize(serializer)).join("")
- ),
- };
- serializer.serialize_str(raw_str.as_str())
- }
- }
- #[cfg(test)]
- mod tests {
- use super::*;
- #[test]
- fn test_serialize_redis_type() {
- let actual = RedisType::SimpleString("foo".to_string())
- .serialize()
- .unwrap();
- assert_eq!(actual, "+foo\r\n");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement