@@ -6,7 +6,7 @@ pub type FieldType = String;
66/// Trait that should implement each type of a document, in order to properly serialize the
77/// Collection Schema according to the Typesense reference.
88pub trait ToTypesenseField {
9- /// Static function that should implement the types of the typesense documents .
9+ /// Mapping of a Typesense type .
1010 fn to_typesense_type ( ) -> & ' static str ;
1111}
1212/// Generic implementation for any type that is also a Typesense document.
@@ -25,22 +25,54 @@ impl<T: Document> ToTypesenseField for Vec<T> {
2525 }
2626}
2727
28+ impl < T : ToTypesenseField > ToTypesenseField for Option < T > {
29+ #[ inline( always) ]
30+ fn to_typesense_type ( ) -> & ' static str {
31+ T :: to_typesense_type ( )
32+ }
33+ }
34+
2835/// macro used internally to add implementations of ToTypesenseField for several rust types.
2936#[ macro_export]
3037macro_rules! impl_to_typesense_field (
31- ( $for: ty, $typesense_variant : expr) => {
38+ ( $for: ty, $typesense_type : expr) => {
3239 impl $crate:: prelude:: ToTypesenseField for $for {
3340 #[ inline( always) ]
3441 fn to_typesense_type( ) -> & ' static str {
35- $typesense_variant
42+ $typesense_type
43+ }
44+ }
45+ impl $crate:: prelude:: ToTypesenseField for Vec <$for> {
46+ #[ inline( always) ]
47+ fn to_typesense_type( ) -> & ' static str {
48+ concat!( $typesense_type, "[]" )
49+ }
50+ }
51+ impl $crate:: prelude:: ToTypesenseField for Vec <Option <$for>> {
52+ #[ inline( always) ]
53+ fn to_typesense_type( ) -> & ' static str {
54+ concat!( $typesense_type, "[]" )
3655 }
3756 }
3857 } ;
39- ( $for: ty, $typesense_variant: expr, $any: ident) => {
40- impl <$any> $crate:: prelude:: ToTypesenseField for $for {
58+
59+ ( $for: ty, $typesense_type: expr, $any: ident $( : $any_bound: path) ?) => {
60+ impl <$any $( : $any_bound) ?> $crate:: prelude:: ToTypesenseField for $for {
61+ #[ inline( always) ]
62+ fn to_typesense_type( ) -> & ' static str {
63+ $typesense_type
64+ }
65+ }
66+ impl <$any $( : $any_bound) ?> $crate:: prelude:: ToTypesenseField for Vec <$for> {
4167 #[ inline( always) ]
4268 fn to_typesense_type( ) -> & ' static str {
43- $typesense_variant
69+ concat!( $typesense_type, "[]" )
70+ }
71+ }
72+ impl <$any $( : $any_bound) ?> $crate:: prelude:: ToTypesenseField for Vec <Option <$for>> {
73+ #[ inline( always) ]
74+ fn to_typesense_type( ) -> & ' static str {
75+ concat!( $typesense_type, "[]" )
4476 }
4577 }
4678 } ;
@@ -63,19 +95,10 @@ impl_to_typesense_field!(bool, "bool");
6395impl_to_typesense_field ! ( HashMap <String , T >, "object" , T ) ;
6496impl_to_typesense_field ! ( BTreeMap <String , T >, "object" , T ) ;
6597
66- impl_to_typesense_field ! ( Vec <String >, "string[]" ) ;
67- impl_to_typesense_field ! ( Vec <i8 >, "int32[]" ) ;
68- impl_to_typesense_field ! ( Vec <u8 >, "int32[]" ) ;
69- impl_to_typesense_field ! ( Vec <i16 >, "int32[]" ) ;
70- impl_to_typesense_field ! ( Vec <u16 >, "int32[]" ) ;
71- impl_to_typesense_field ! ( Vec <i32 >, "int32[]" ) ;
72- impl_to_typesense_field ! ( Vec <u32 >, "int64[]" ) ;
73- impl_to_typesense_field ! ( Vec <i64 >, "int64[]" ) ;
74- impl_to_typesense_field ! ( Vec <u64 >, "int64[]" ) ;
75- impl_to_typesense_field ! ( Vec <isize >, "int64[]" ) ;
76- impl_to_typesense_field ! ( Vec <usize >, "int64[]" ) ;
77- impl_to_typesense_field ! ( Vec <f32 >, "float[]" ) ;
78- impl_to_typesense_field ! ( Vec <f64 >, "float[]" ) ;
79- impl_to_typesense_field ! ( Vec <bool >, "bool[]" ) ;
80- impl_to_typesense_field ! ( Vec <HashMap <String , T >>, "object[]" , T ) ;
81- impl_to_typesense_field ! ( Vec <BTreeMap <String , T >>, "object[]" , T ) ;
98+ #[ cfg( feature = "chrono" ) ]
99+ mod chrono_support {
100+ impl_to_typesense_field ! ( chrono:: DateTime <T >, "string" , T : chrono:: TimeZone ) ;
101+ impl_to_typesense_field ! ( chrono:: NaiveDate , "string" ) ;
102+ impl_to_typesense_field ! ( chrono:: NaiveDateTime , "string" ) ;
103+ impl_to_typesense_field ! ( chrono:: NaiveTime , "string" ) ;
104+ }
0 commit comments