Skip to content

Instantly share code, notes, and snippets.

@qingant
Created March 25, 2020 17:00
Show Gist options
  • Save qingant/e3eb6e828afafccb129ad8ef1d36b093 to your computer and use it in GitHub Desktop.
Save qingant/e3eb6e828afafccb129ad8ef1d36b093 to your computer and use it in GitHub Desktop.
#[derive(Debug, Clone)]
pub enum RValue {
RStr(String),
// RBulkString(ByteString),
RNumber(f64),
Error(String),
RList(Vec<RValue>),
Nil,
}
impl RValue {
pub fn encode(&self) -> String {
match self {
RValue::Nil => String::from("*-1\r\n"),
RValue::RStr(s) => format!("+{}\r\n", s),
RValue::RNumber(n) => format!(":{}\r\n", n),
RValue::Error(e) => format!("-{}\r\n", e),
RValue::RList(list) => {
let mut r = format!("*{}\r\n", list.len());
for s in list {
r += &s.encode();
}
r
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment