Skip to content
This repository was archived by the owner on Mar 18, 2022. It is now read-only.

Int parse error #53

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions redisless/src/server/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use redis::{Commands, Connection, RedisResult};
use std::{thread::sleep, time::Duration};

use crate::command::command_error::RedisCommandError;
use crate::server::ServerState;
use crate::storage::in_memory::InMemoryStorage;
use crate::Server;
Expand Down Expand Up @@ -37,6 +38,15 @@ fn test_incr_decr_commands() {
let value: u32 = con.get("63").unwrap();
assert_eq!(value, 79_u32);

let response: Result<i64, redis::RedisError> = con.incr("63", "foo");
match response {
Ok(_) => panic!("got valid response from incr command for key {} and value {}", "63", "foo"),
Err(error) => {
assert_eq!(error.kind(), redis::ErrorKind::ExtensionError);
assert_eq!(error.to_string(), "invalid: digit found in string");
}
};

assert_eq!(server.stop(), Some(ServerState::Stopped));
}

Expand Down
34 changes: 16 additions & 18 deletions redisless/src/server/util/run_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,14 @@ pub fn run_command_and_get_response<T: Storage>(

match storage.read(k.as_slice()) {
Some(value) => {
if let Ok(mut int_val) = std::str::from_utf8(value).unwrap().parse::<i64>()
{
int_val += 1;
let new_value = int_val.to_string().into_bytes();
storage.write(k.as_slice(), new_value.as_slice());
RedisResponse::single(Integer(int_val as i64))
} else {
// handle this error
unimplemented!()
match std::str::from_utf8(value).unwrap().parse::<i64>() {
Ok(mut int_val) => {
int_val += 1;
let new_value = int_val.to_string().into_bytes();
storage.write(k.as_slice(), new_value.as_slice());
RedisResponse::single(Integer(int_val as i64))
}
Err(error) => RedisResponse::error(RedisCommandError::IntParse(error))
}
}
None => {
Expand All @@ -146,15 +145,14 @@ pub fn run_command_and_get_response<T: Storage>(

match storage.read(k.as_slice()) {
Some(value) => {
if let Ok(mut int_val) = std::str::from_utf8(value).unwrap().parse::<i64>()
{
int_val += increment;
let new_value = int_val.to_string().into_bytes();
storage.write(k.as_slice(), new_value.as_slice());
RedisResponse::single(Integer(int_val as i64))
} else {
//RedisResponse::error(...)
unimplemented!()
match std::str::from_utf8(value).unwrap().parse::<i64>() {
Ok(mut int_val) => {
int_val += increment;
let new_value = int_val.to_string().into_bytes();
storage.write(k.as_slice(), new_value.as_slice());
RedisResponse::single(Integer(int_val as i64))
}
Err(error) => RedisResponse::error(RedisCommandError::IntParse(error))
}
}
None => {
Expand Down