feat(KvStore): new, get, set, remove functions implementation

- Added a new test for empty key
This commit is contained in:
2025-09-28 16:59:27 -04:00
parent 9a1699a830
commit 32a75b8650
2 changed files with 20 additions and 5 deletions

View File

@@ -1,20 +1,27 @@
pub struct KvStore {} use std::collections::HashMap;
#[derive(Default)]
pub struct KvStore {
hash_map: HashMap<String, String>,
}
impl KvStore { impl KvStore {
pub fn new() -> Self { pub fn new() -> Self {
unimplemented!() Self {
hash_map: HashMap::new(),
}
} }
pub fn set(&mut self, key: String, value: String) { pub fn set(&mut self, key: String, value: String) {
unimplemented!() self.hash_map.insert(key, value);
} }
pub fn get(&mut self, key: String) -> Option<String> { pub fn get(&mut self, key: String) -> Option<String> {
unimplemented!() self.hash_map.get(&key).cloned()
} }
pub fn remove(&mut self, key: String) { pub fn remove(&mut self, key: String) {
unimplemented!() self.hash_map.remove(&key);
} }
} }

View File

@@ -153,3 +153,11 @@ fn remove_key() {
store.remove("key1".to_owned()); store.remove("key1".to_owned());
assert_eq!(store.get("key1".to_owned()), None); assert_eq!(store.get("key1".to_owned()), None);
} }
#[test]
fn get_empty_key() {
let mut store = KvStore::new();
store.set("".to_owned(), "value1".to_owned());
assert_eq!(store.get("".to_owned()), Some("value1".to_owned()));
}