Skip to content

Commit

Permalink
feat: add interconversion between string and bytes type
Browse files Browse the repository at this point in the history
  • Loading branch information
he-ym committed Jan 12, 2024
1 parent 3314396 commit 39b37e9
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
8 changes: 4 additions & 4 deletions src/data_store/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,15 @@ impl KeyValueStore {
}

/// Gets a Value (in Vec<u8> type) associated to the Key in the KeyValueStore
pub fn get_bytes(&mut self, key: String) -> Option<Result<&Vec<u8>, CacheError>> {
pub fn get_bytes(&mut self, key: String) -> Option<Result<Vec<u8>, CacheError>> {
match self._get_or_none_if_expired(&key) {
Some(value_entry) => Some(value_entry.get_value_as_bytes()),
_ => None,
}
}

/// Gets a Value (converted to String type) associated to the Key in the KeyValueStore
pub fn get_string(&mut self, key: String) -> Option<Result<&String, CacheError>> {
pub fn get_string(&mut self, key: String) -> Option<Result<String, CacheError>> {
match self._get_or_none_if_expired(&key) {
Some(value_entry) => Some(value_entry.get_value_as_string()),
_ => None,
Expand All @@ -116,7 +116,7 @@ impl KeyValueStore {
/// and returns the Value (in Vec<u8> type)
pub fn pop_bytes(&mut self, key: String) -> Option<Result<Vec<u8>, CacheError>> {
match self._remove_and_none_if_expired(&key) {
Some(value_entry) => Some(value_entry.get_value_as_bytes().cloned()),
Some(value_entry) => Some(value_entry.get_value_as_bytes()),
_ => None,
}
}
Expand All @@ -125,7 +125,7 @@ impl KeyValueStore {
/// and returns the Value (converted to String type)
pub fn pop_string(&mut self, key: String) -> Option<Result<String, CacheError>> {
match self._remove_and_none_if_expired(&key) {
Some(value_entry) => Some(value_entry.get_value_as_string().cloned()),
Some(value_entry) => Some(value_entry.get_value_as_string()),
_ => None,
}
}
Expand Down
15 changes: 11 additions & 4 deletions src/data_store/value_entry.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use std::{
collections::{HashMap, HashSet},
string::FromUtf8Error,
time::Instant,
};

#[derive(Debug)]
pub enum CacheError {
InvalidType,
InvalidTypeCast(FromUtf8Error),
}

#[derive(Clone)]
Expand Down Expand Up @@ -64,16 +66,21 @@ impl ValueEntry {
}
}

pub fn get_value_as_bytes(&self) -> Result<&Vec<u8>, CacheError> {
pub fn get_value_as_bytes(&self) -> Result<Vec<u8>, CacheError> {
match &self.value {
CacheValue::Bytes(bytes) => Ok(&bytes),
CacheValue::Bytes(bytes) => Ok(bytes.to_owned()),
CacheValue::String(string) => Ok(string.to_owned().into_bytes()),
_ => Err(CacheError::InvalidType),
}
}

pub fn get_value_as_string(&self) -> Result<&String, CacheError> {
pub fn get_value_as_string(&self) -> Result<String, CacheError> {
match &self.value {
CacheValue::String(value_string) => Ok(&value_string),
CacheValue::String(value_string) => Ok(value_string.to_owned()),
CacheValue::Bytes(value_bytes) => match String::from_utf8(value_bytes.to_owned()) {
Err(e) => Err(CacheError::InvalidTypeCast(e)),
Ok(string_value) => Ok(string_value),
},
_ => Err(CacheError::InvalidType),
}
}
Expand Down
15 changes: 13 additions & 2 deletions src/public_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,14 @@ fn test_set_get_string() {
let mut store = KeyValueStore::new(0);
store.set_string("ABC".to_string(), "HELLO".to_string(), Some(5000));
assert_eq!(
*store.get_string("ABC".to_string()).unwrap().unwrap(),
store.get_string("ABC".to_string()).unwrap().unwrap(),
"HELLO".to_string()
);

// Can set string convertible bytes and fetch it back as string.
store.set_bytes("XYZ".to_string(), "HELLO".as_bytes().to_vec(), Some(5000));
assert_eq!(
store.get_string("ABC".to_string()).unwrap().unwrap(),
"HELLO".to_string()
);
}
Expand All @@ -61,9 +68,13 @@ fn test_set_get_bytes() {
let mut store = KeyValueStore::new(0);
store.set_bytes("ABC".to_string(), "HELLO".as_bytes().to_vec(), Some(5000));
assert_eq!(
*store.get_bytes("ABC".to_string()).unwrap().unwrap(),
store.get_bytes("ABC".to_string()).unwrap().unwrap(),
"HELLO".as_bytes().to_vec()
);
assert_eq!(
store.get_string("ABC".to_string()).unwrap().unwrap(),
"HELLO".to_string()
);
}

#[test]
Expand Down

0 comments on commit 39b37e9

Please sign in to comment.