-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ENH] Add rust protobufs and conversion. Add build.rs, protobufs, and…
… conversions (#1513) ## Description of changes *Summarize the changes made by this PR.* - Improvements & Bug fixes - Update dockerfile to use a fetched protoc since that is needed for protoc to include/ WKT - New functionality - Adds a build.rs so that we can build the protos into rust bindings - Adds a types/ folder with types and rust-y/idiomatic TryFrom conversions so that callers can just do .try_from() and get type inference. - Adds a macro for error type wrapping and impl'ing ChromaError on the macro. - All types are rexported from types/ so that the rest of the code can easily use it. ## Test plan *How are these changes tested?* - Add _very_ rudimentary tests for conversion. We should do a pass where we add some more rigorous conversion testing. - [x] Tests pass locally with `cargo test` ## Documentation Changes None required.
- Loading branch information
Showing
16 changed files
with
987 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
} | ||
|
||
impl ChromaError for CollectionConversionError { | ||
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()); | ||
} | ||
} |
Oops, something went wrong.