Skip to content

Commit

Permalink
error handling for too-large keys
Browse files Browse the repository at this point in the history
  • Loading branch information
DavJCosby committed Dec 17, 2024
1 parent 5743d58 commit e423b79
Showing 1 changed file with 34 additions and 2 deletions.
36 changes: 34 additions & 2 deletions src/driver/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ impl Data {
///
/// let retrieved: &i32 = data.get("abc")?;
/// assert_eq!(retrieved, &123);
///
///
/// let type_mismatch = data.get::<bool>("abc");
/// assert_eq!(
/// &type_mismatch.err().unwrap().message,
/// "Data associated with the key `abc` exists, but it is not of type bool."
/// );
///
///
/// let bad_key = data.get::<i32>("cba");
/// assert_eq!(
/// &bad_key.err().unwrap().message,
Expand Down Expand Up @@ -100,13 +100,45 @@ impl Data {
}

pub fn set<T: StorableData>(&mut self, key: &str, value: T) {
#[cfg(target_pointer_width = "64")]
assert!(
key.len() < 24,
"Invalid data key; Max size is 24 bytes, `{}` is {} bytes.",
key,
key.len()
);

#[cfg(target_pointer_width = "32")]
assert!(
key.len() < 24,
"Invalid data key; Max size is 12 bytes on 32-bit systems, `{}` is {} bytes.",
key,
key.len()
);

self.data.insert(
key.to_compact_string(),
Box::<DataWrapper<T>>::new(DataWrapper::new(value)),
);
}

pub fn store<T: StorableData>(&mut self, key: &str, value: T) -> &mut T {
#[cfg(target_pointer_width = "64")]
assert!(
key.len() < 24,
"Invalid data key; Max size is 24 bytes, `{}` is {} bytes.",
key,
key.len()
);

#[cfg(target_pointer_width = "32")]
assert!(
key.len() < 24,
"Invalid data key; Max size is 12 bytes on 32-bit systems, `{}` is {} bytes.",
key,
key.len()
);

self.data.insert(
key.to_compact_string(),
Box::<DataWrapper<T>>::new(DataWrapper::new(value)),
Expand Down

0 comments on commit e423b79

Please sign in to comment.