in src/protocol/resp/src/request/set.rs [51:164]
fn try_from(other: Message) -> Result<Self, Error> {
if let Message::Array(array) = other {
if array.inner.is_none() {
return Err(Error::new(ErrorKind::Other, "malformed command"));
}
let mut array = array.inner.unwrap();
if array.len() < 3 {
return Err(Error::new(ErrorKind::Other, "malformed command"));
}
let key = take_bulk_string(&mut array)?;
if key.is_empty() {
return Err(Error::new(ErrorKind::Other, "malformed command"));
}
let value = take_bulk_string(&mut array)?;
let mut expire_time = None;
let mut mode = SetMode::Set;
let mut get_old = false;
let mut i = 1;
while i < array.len() {
if let Message::BulkString(field) = &array[i] {
if field.inner.is_none() {
return Err(Error::new(ErrorKind::Other, "malformed command"));
}
let field = field.inner.as_ref().unwrap();
match field.as_ref().as_ref() {
b"EX" => {
if expire_time.is_some() || array.len() < i + 2 {
return Err(Error::new(ErrorKind::Other, "malformed command"));
}
let s = take_bulk_string_as_u64(&mut array)?;
expire_time = Some(ExpireTime::Seconds(s));
i += 1;
}
b"PX" => {
if expire_time.is_some() || array.len() < i + 2 {
return Err(Error::new(ErrorKind::Other, "malformed command"));
}
let ms = take_bulk_string_as_u64(&mut array)?;
expire_time = Some(ExpireTime::Milliseconds(ms));
i += 1;
}
b"EXAT" => {
if expire_time.is_some() || array.len() < i + 2 {
return Err(Error::new(ErrorKind::Other, "malformed command"));
}
let s = take_bulk_string_as_u64(&mut array)?;
expire_time = Some(ExpireTime::UnixSeconds(s));
i += 1;
}
b"PXAT" => {
if expire_time.is_some() || array.len() < i + 2 {
return Err(Error::new(ErrorKind::Other, "malformed command"));
}
let ms = take_bulk_string_as_u64(&mut array)?;
expire_time = Some(ExpireTime::UnixMilliseconds(ms));
i += 1;
}
b"KEEPTTL" => {
if expire_time.is_some() {
return Err(Error::new(ErrorKind::Other, "malformed command"));
}
expire_time = Some(ExpireTime::KeepTtl);
}
b"NX" => {
if mode != SetMode::Set {
return Err(Error::new(ErrorKind::Other, "malformed command"));
}
mode = SetMode::Add;
}
b"XX" => {
if mode != SetMode::Set {
return Err(Error::new(ErrorKind::Other, "malformed command"));
}
mode = SetMode::Replace;
}
b"GET" => {
if get_old {
return Err(Error::new(ErrorKind::Other, "malformed command"));
}
get_old = true;
}
_ => {
return Err(Error::new(ErrorKind::Other, "malformed command"));
}
}
} else {
return Err(Error::new(ErrorKind::Other, "malformed command"));
}
i += 1;
}
Ok(Self {
key,
value,
expire_time,
mode,
get_old,
})
} else {
Err(Error::new(ErrorKind::Other, "malformed command"))
}
}