-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[ENH] Add rust protobufs and conversion. Add build.rs, protobufs, and conversions #1513
Changes from all commits
30a5dc8
30d0880
7f2065a
1823c17
a70e68f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
tonic_build::configure().compile( | ||
&[ | ||
"../../idl/chromadb/proto/chroma.proto", | ||
"../../idl/chromadb/proto/coordinator.proto", | ||
], | ||
&["../../idl/"], | ||
)?; | ||
Ok(()) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
mod assignment; | ||
mod config; | ||
mod errors; | ||
mod types; | ||
|
||
mod chroma_proto { | ||
tonic::include_proto!("chroma"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This links the built proto |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
use super::{Metadata, MetadataValueConversionError}; | ||
use crate::{ | ||
chroma_proto, | ||
errors::{ChromaError, ErrorCodes}, | ||
}; | ||
use thiserror::Error; | ||
use uuid::Uuid; | ||
|
||
#[derive(Debug, PartialEq)] | ||
pub(crate) struct Collection { | ||
pub(crate) id: Uuid, | ||
pub(crate) name: String, | ||
pub(crate) topic: String, | ||
pub(crate) metadata: Option<Metadata>, | ||
pub(crate) dimension: Option<i32>, | ||
pub(crate) tenant: String, | ||
pub(crate) database: String, | ||
} | ||
|
||
#[derive(Error, Debug)] | ||
pub(crate) enum CollectionConversionError { | ||
#[error("Invalid UUID")] | ||
InvalidUuid, | ||
#[error(transparent)] | ||
MetadataValueConversionError(#[from] MetadataValueConversionError), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Example of wrapping an error |
||
} | ||
|
||
impl ChromaError for CollectionConversionError { | ||
HammadB marked this conversation as resolved.
Show resolved
Hide resolved
|
||
fn code(&self) -> crate::errors::ErrorCodes { | ||
match self { | ||
CollectionConversionError::InvalidUuid => ErrorCodes::InvalidArgument, | ||
CollectionConversionError::MetadataValueConversionError(e) => e.code(), | ||
} | ||
} | ||
} | ||
|
||
impl TryFrom<chroma_proto::Collection> for Collection { | ||
type Error = CollectionConversionError; | ||
|
||
fn try_from(proto_collection: chroma_proto::Collection) -> Result<Self, Self::Error> { | ||
let collection_uuid = match Uuid::try_parse(&proto_collection.id) { | ||
Ok(uuid) => uuid, | ||
Err(_) => return Err(CollectionConversionError::InvalidUuid), | ||
}; | ||
let collection_metadata: Option<Metadata> = match proto_collection.metadata { | ||
Some(proto_metadata) => match proto_metadata.try_into() { | ||
Ok(metadata) => Some(metadata), | ||
Err(e) => return Err(CollectionConversionError::MetadataValueConversionError(e)), | ||
}, | ||
None => None, | ||
}; | ||
Ok(Collection { | ||
id: collection_uuid, | ||
name: proto_collection.name, | ||
topic: proto_collection.topic, | ||
metadata: collection_metadata, | ||
dimension: proto_collection.dimension, | ||
tenant: proto_collection.tenant, | ||
database: proto_collection.database, | ||
}) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_collection_try_from() { | ||
let proto_collection = chroma_proto::Collection { | ||
id: "00000000-0000-0000-0000-000000000000".to_string(), | ||
name: "foo".to_string(), | ||
topic: "bar".to_string(), | ||
metadata: None, | ||
dimension: None, | ||
tenant: "baz".to_string(), | ||
database: "qux".to_string(), | ||
}; | ||
let converted_collection: Collection = proto_collection.try_into().unwrap(); | ||
assert_eq!(converted_collection.id, Uuid::nil()); | ||
assert_eq!(converted_collection.name, "foo".to_string()); | ||
assert_eq!(converted_collection.topic, "bar".to_string()); | ||
assert_eq!(converted_collection.metadata, None); | ||
assert_eq!(converted_collection.dimension, None); | ||
assert_eq!(converted_collection.tenant, "baz".to_string()); | ||
assert_eq!(converted_collection.database, "qux".to_string()); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
clean up