Skip to content

Commit

Permalink
[CHORE] Make a ChromaError type for RuntimeTypeError
Browse files Browse the repository at this point in the history
  • Loading branch information
rescrv committed Sep 26, 2024
1 parent 848f277 commit 754cb14
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 20 deletions.
37 changes: 25 additions & 12 deletions rust/blockstore/src/key.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
use std::hash::{Hash, Hasher};

use chroma_error::{ChromaError, ErrorCodes};

use super::Key;

// TODO(rescrv): This used to be a panic/unwrap, but could be a nicer type.
#[derive(thiserror::Error, Debug)]
#[error("runtime type error")]
pub struct RuntimeTypeError;

impl ChromaError for RuntimeTypeError {
fn code(&self) -> ErrorCodes {
ErrorCodes::Internal
}
}

#[derive(Clone, PartialEq, PartialOrd, Debug)]
pub enum KeyWrapper {
String(String),
Expand Down Expand Up @@ -29,12 +42,12 @@ impl From<&str> for KeyWrapper {
}

impl<'referred_data> TryFrom<&'referred_data KeyWrapper> for &'referred_data str {
type Error = &'static str;
type Error = RuntimeTypeError;

fn try_from(key: &'referred_data KeyWrapper) -> Result<Self, &'static str> {
fn try_from(key: &'referred_data KeyWrapper) -> Result<Self, RuntimeTypeError> {
match key {
KeyWrapper::String(s) => Ok(s),
_ => Err("Invalid conversion"),
_ => Err(RuntimeTypeError),
}
}
}
Expand All @@ -46,12 +59,12 @@ impl From<f32> for KeyWrapper {
}

impl TryFrom<&KeyWrapper> for f32 {
type Error = &'static str;
type Error = RuntimeTypeError;

fn try_from(key: &KeyWrapper) -> Result<Self, &'static str> {
fn try_from(key: &KeyWrapper) -> Result<Self, RuntimeTypeError> {
match key {
KeyWrapper::Float32(f) => Ok(*f),
_ => Err("Invalid conversion"),
_ => Err(RuntimeTypeError),
}
}
}
Expand All @@ -63,12 +76,12 @@ impl From<bool> for KeyWrapper {
}

impl TryFrom<&KeyWrapper> for bool {
type Error = &'static str;
type Error = RuntimeTypeError;

fn try_from(key: &KeyWrapper) -> Result<Self, &'static str> {
fn try_from(key: &KeyWrapper) -> Result<Self, RuntimeTypeError> {
match key {
KeyWrapper::Bool(b) => Ok(*b),
_ => Err("Invalid conversion"),
_ => Err(RuntimeTypeError),
}
}
}
Expand All @@ -80,12 +93,12 @@ impl From<u32> for KeyWrapper {
}

impl TryFrom<&KeyWrapper> for u32 {
type Error = &'static str;
type Error = RuntimeTypeError;

fn try_from(key: &KeyWrapper) -> Result<Self, &'static str> {
fn try_from(key: &KeyWrapper) -> Result<Self, RuntimeTypeError> {
match key {
KeyWrapper::Uint32(u) => Ok(*u),
_ => Err("Invalid conversion"),
_ => Err(RuntimeTypeError),
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions rust/blockstore/src/memory/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use super::{
};
use crate::{
arrow::types::{ArrowReadableKey, ArrowReadableValue},
key::KeyWrapper,
key::{KeyWrapper, RuntimeTypeError},
provider::{CreateError, OpenError},
BlockfileReader, BlockfileWriter, Key, Value,
};
Expand All @@ -29,7 +29,7 @@ impl MemoryBlockfileProvider {
'new,
K: Key
+ Into<KeyWrapper>
+ TryFrom<&'new KeyWrapper, Error = &'static str>
+ TryFrom<&'new KeyWrapper, Error = RuntimeTypeError>
+ ArrowReadableKey<'new>
+ 'new,
V: Value + Readable<'new> + ArrowReadableValue<'new> + 'new,
Expand Down
4 changes: 2 additions & 2 deletions rust/blockstore/src/memory/reader_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::{
super::{BlockfileError, Key, Value},
storage::{Readable, Storage, StorageBuilder, StorageManager, Writeable},
};
use crate::key::KeyWrapper;
use crate::key::{KeyWrapper, RuntimeTypeError};
use chroma_error::ChromaError;

#[derive(Clone)]
Expand Down Expand Up @@ -73,7 +73,7 @@ pub struct MemoryBlockfileReader<K: Key, V: Value> {

impl<
'storage,
K: Key + Into<KeyWrapper> + TryFrom<&'storage KeyWrapper, Error = &'static str>,
K: Key + Into<KeyWrapper> + TryFrom<&'storage KeyWrapper, Error = RuntimeTypeError>,
V: Value + Readable<'storage>,
> MemoryBlockfileReader<K, V>
{
Expand Down
4 changes: 2 additions & 2 deletions rust/blockstore/src/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use super::arrow::types::{
ArrowReadableKey, ArrowReadableValue, ArrowWriteableKey, ArrowWriteableValue,
};
use super::config::BlockfileProviderConfig;
use super::key::KeyWrapper;
use super::key::{KeyWrapper, RuntimeTypeError};
use super::memory::provider::MemoryBlockfileProvider;
use super::memory::storage::{Readable, Writeable};
use super::types::BlockfileWriter;
Expand Down Expand Up @@ -62,7 +62,7 @@ impl BlockfileProvider {
'new,
K: Key
+ Into<KeyWrapper>
+ TryFrom<&'new KeyWrapper, Error = &'static str>
+ TryFrom<&'new KeyWrapper, Error = RuntimeTypeError>
+ ArrowReadableKey<'new>
+ 'new,
V: Value + Readable<'new> + ArrowReadableValue<'new> + 'new,
Expand Down
4 changes: 2 additions & 2 deletions rust/blockstore/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use super::arrow::flusher::ArrowBlockfileFlusher;
use super::arrow::types::{
ArrowReadableKey, ArrowReadableValue, ArrowWriteableKey, ArrowWriteableValue,
};
use super::key::KeyWrapper;
use super::key::{KeyWrapper, RuntimeTypeError};
use super::memory::reader_writer::{
MemoryBlockfileFlusher, MemoryBlockfileReader, MemoryBlockfileWriter,
};
Expand Down Expand Up @@ -236,7 +236,7 @@ impl<
'referred_data,
K: Key
+ Into<KeyWrapper>
+ TryFrom<&'referred_data KeyWrapper, Error = &'static str>
+ TryFrom<&'referred_data KeyWrapper, Error = RuntimeTypeError>
+ ArrowReadableKey<'referred_data>,
V: Value + Readable<'referred_data> + ArrowReadableValue<'referred_data>,
> BlockfileReader<'referred_data, K, V>
Expand Down

0 comments on commit 754cb14

Please sign in to comment.