Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ typesense_derive = { path = "./typesense_derive", version = "0.3" }
anyhow = "1"
base64 = "0.22"
bon = "3"
chrono = "0.4"
clap = { version = "4", features = ["derive"] }
hmac = "0.12"
indexmap = { version = "2", features = ["serde"] }
Expand Down
2 changes: 2 additions & 0 deletions typesense/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ description = "Client for typesense"

[features]
default = ["derive"]
chrono = ["dep:chrono"]

# Provide derive(Typesense) macro.
derive = ["typesense_derive"]
Expand All @@ -22,6 +23,7 @@ typesense_derive = { workspace = true, optional = true }
anyhow = { workspace = true }
base64 = { workspace = true }
bon = { workspace = true }
chrono = { workspace = true, optional = true }
hmac = { workspace = true }
reqwest-retry = { workspace = true }
serde = { workspace = true }
Expand Down
67 changes: 45 additions & 22 deletions typesense/src/traits/field_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub type FieldType = String;
/// Trait that should implement each type of a document, in order to properly serialize the
/// Collection Schema according to the Typesense reference.
pub trait ToTypesenseField {
/// Static function that should implement the types of the typesense documents.
/// Mapping of a Typesense type.
fn to_typesense_type() -> &'static str;
}
/// Generic implementation for any type that is also a Typesense document.
Expand All @@ -25,22 +25,54 @@ impl<T: Document> ToTypesenseField for Vec<T> {
}
}

impl<T: ToTypesenseField> ToTypesenseField for Option<T> {
#[inline(always)]
fn to_typesense_type() -> &'static str {
T::to_typesense_type()
}
}

/// macro used internally to add implementations of ToTypesenseField for several rust types.
#[macro_export]
macro_rules! impl_to_typesense_field (
($for:ty, $typesense_variant:expr) => {
($for:ty, $typesense_type:expr) => {
impl $crate::prelude::ToTypesenseField for $for {
#[inline(always)]
fn to_typesense_type() -> &'static str {
$typesense_variant
$typesense_type
}
}
impl $crate::prelude::ToTypesenseField for Vec<$for> {
#[inline(always)]
fn to_typesense_type() -> &'static str {
concat!($typesense_type, "[]")
}
}
impl $crate::prelude::ToTypesenseField for Vec<Option<$for>> {
#[inline(always)]
fn to_typesense_type() -> &'static str {
concat!($typesense_type, "[]")
}
}
};
($for:ty, $typesense_variant:expr, $any:ident) => {
impl<$any> $crate::prelude::ToTypesenseField for $for {

($for:ty, $typesense_type:expr, $any:ident $(: $any_bound:path)?) => {
impl<$any $(: $any_bound)?> $crate::prelude::ToTypesenseField for $for {
#[inline(always)]
fn to_typesense_type() -> &'static str {
$typesense_type
}
}
impl<$any $(: $any_bound)?> $crate::prelude::ToTypesenseField for Vec<$for> {
#[inline(always)]
fn to_typesense_type() -> &'static str {
$typesense_variant
concat!($typesense_type, "[]")
}
}
impl<$any $(: $any_bound)?> $crate::prelude::ToTypesenseField for Vec<Option<$for>> {
#[inline(always)]
fn to_typesense_type() -> &'static str {
concat!($typesense_type, "[]")
}
}
};
Expand All @@ -63,19 +95,10 @@ impl_to_typesense_field!(bool, "bool");
impl_to_typesense_field!(HashMap<String, T>, "object", T);
impl_to_typesense_field!(BTreeMap<String, T>, "object", T);

impl_to_typesense_field!(Vec<String>, "string[]");
impl_to_typesense_field!(Vec<i8>, "int32[]");
impl_to_typesense_field!(Vec<u8>, "int32[]");
impl_to_typesense_field!(Vec<i16>, "int32[]");
impl_to_typesense_field!(Vec<u16>, "int32[]");
impl_to_typesense_field!(Vec<i32>, "int32[]");
impl_to_typesense_field!(Vec<u32>, "int64[]");
impl_to_typesense_field!(Vec<i64>, "int64[]");
impl_to_typesense_field!(Vec<u64>, "int64[]");
impl_to_typesense_field!(Vec<isize>, "int64[]");
impl_to_typesense_field!(Vec<usize>, "int64[]");
impl_to_typesense_field!(Vec<f32>, "float[]");
impl_to_typesense_field!(Vec<f64>, "float[]");
impl_to_typesense_field!(Vec<bool>, "bool[]");
impl_to_typesense_field!(Vec<HashMap<String, T>>, "object[]", T);
impl_to_typesense_field!(Vec<BTreeMap<String, T>>, "object[]", T);
#[cfg(feature = "chrono")]
mod chrono_support {
impl_to_typesense_field!(chrono::DateTime<T>, "string", T: chrono::TimeZone);
impl_to_typesense_field!(chrono::NaiveDate, "string");
impl_to_typesense_field!(chrono::NaiveDateTime, "string");
impl_to_typesense_field!(chrono::NaiveTime, "string");
}
2 changes: 1 addition & 1 deletion typesense_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ fn impl_typesense_collection(item: ItemStruct) -> syn::Result<TokenStream> {
let name_partial = Ident::new(&(ident.to_string() + "Partial"), ident.span());

let generated_code = quote! {
#[derive(Default, Serialize)]
#[derive(Default, ::serde::Serialize)]
#vis struct #name_partial {
#(#optional_fields)*
}
Expand Down
Loading