use crate::traits::Document;
use std::collections::{BTreeMap, HashMap};
pub type FieldType = String;
pub trait ToTypesenseField {
fn to_typesense_type() -> &'static str;
}
impl<T: Document> ToTypesenseField for T {
#[inline(always)]
fn to_typesense_type() -> &'static str {
"object"
}
}
impl<T: Document> ToTypesenseField for Vec<T> {
#[inline(always)]
fn to_typesense_type() -> &'static str {
"object[]"
}
}
impl<T: ToTypesenseField> ToTypesenseField for Option<T> {
#[inline(always)]
fn to_typesense_type() -> &'static str {
T::to_typesense_type()
}
}
#[macro_export]
macro_rules! impl_to_typesense_field (
($for:ty, $typesense_type:expr) => {
impl $crate::prelude::ToTypesenseField for $for {
#[inline(always)]
fn to_typesense_type() -> &'static str {
$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_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 {
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, "[]")
}
}
};
);
impl_to_typesense_field!(String, "string");
impl_to_typesense_field!(i8, "int32");
impl_to_typesense_field!(u8, "int32");
impl_to_typesense_field!(i16, "int32");
impl_to_typesense_field!(u16, "int32");
impl_to_typesense_field!(i32, "int32");
impl_to_typesense_field!(u32, "int64");
impl_to_typesense_field!(i64, "int64");
impl_to_typesense_field!(u64, "int64");
impl_to_typesense_field!(isize, "int64");
impl_to_typesense_field!(usize, "int64");
impl_to_typesense_field!(f32, "float");
impl_to_typesense_field!(f64, "float");
impl_to_typesense_field!(bool, "bool");
impl_to_typesense_field!(HashMap<String, T>, "object", T);
impl_to_typesense_field!(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");
}