From 4856360c6a0635979e1ebf55c508e74945115585 Mon Sep 17 00:00:00 2001 From: projectmoon Date: Fri, 16 Oct 2020 13:07:19 +0000 Subject: [PATCH] Custom schema violation error for DB value retrieval. --- src/db.rs | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/db.rs b/src/db.rs index 14b998f..f3fa959 100644 --- a/src/db.rs +++ b/src/db.rs @@ -4,6 +4,11 @@ use thiserror::Error; use zerocopy::byteorder::I32; use zerocopy::{AsBytes, LayoutVerified}; +/// User variables are stored as little-endian 32-bit integers in the +/// database. This type alias makes the database code more pleasant to +/// read. +type LittleEndianI32Layout<'a> = LayoutVerified<&'a [u8], I32>; + #[derive(Clone)] pub struct Database { db: Db, @@ -14,6 +19,9 @@ pub enum DataError { #[error("value does not exist for key: {0}")] KeyDoesNotExist(String), + #[error("key violates expected schema: {0}")] + SchemaViolation(String), + #[error("internal database error: {0}")] InternalError(#[from] sled::Error), } @@ -40,11 +48,14 @@ impl Database { let key = to_key(room_id, username, variable_name); if let Some(raw_value) = self.db.get(&key)? { - let layout: LayoutVerified<&[u8], I32> = - LayoutVerified::new_unaligned(&*raw_value).expect("bytes do not fit schema"); + let layout = LittleEndianI32Layout::new_unaligned(raw_value.as_ref()); - let value: I32 = *layout; - Ok(value.get()) + if let Some(layout) = layout { + let value: I32 = *layout; + Ok(value.get()) + } else { + Err(DataError::SchemaViolation(String::from_utf8(key).unwrap())) + } } else { Err(DataError::KeyDoesNotExist(String::from_utf8(key).unwrap())) }