|
1 | | -use serde::{Deserialize, Serialize}; |
2 | | - |
3 | | -/// Types that are supported by [Typesense](https://github.com/typesense/typesense/blob/v0.19.0/include/field.h#L8). |
4 | | -#[derive(Debug, Serialize, Deserialize, PartialEq, Clone, Copy)] |
5 | | -#[serde(rename_all = "lowercase")] |
6 | | -pub enum FieldType { |
7 | | - /// string |
8 | | - String, |
9 | | - /// int32 |
10 | | - Int32, |
11 | | - /// int64 |
12 | | - Int64, |
13 | | - /// float |
14 | | - Float, |
15 | | - /// bool |
16 | | - Bool, |
17 | | - /// string[] |
18 | | - #[serde(rename = "string[]")] |
19 | | - StringArray, |
20 | | - /// int32[] |
21 | | - #[serde(rename = "int32[]")] |
22 | | - Int32Array, |
23 | | - /// int64[] |
24 | | - #[serde(rename = "int64[]")] |
25 | | - Int64Array, |
26 | | - /// float[] |
27 | | - #[serde(rename = "float[]")] |
28 | | - FloatArray, |
29 | | - /// bool[] |
30 | | - #[serde(rename = "bool[]")] |
31 | | - BoolArray, |
32 | | -} |
| 1 | +/// Type for a field. Currently it is a wrapping to a `String` but it could be extended to a enum |
| 2 | +pub type FieldType = String; |
33 | 3 |
|
34 | 4 | /// Trait that should implement each type of a document, in order to properly serialize the |
35 | 5 | /// Collection Schema according to the Typesense reference. |
36 | 6 | pub trait ToTypesenseField { |
37 | 7 | /// Static function that should implement the types of the typesense documents. |
38 | | - fn to_typesense_type() -> FieldType; |
| 8 | + fn to_typesense_type() -> &'static str; |
39 | 9 | } |
40 | 10 |
|
41 | 11 | /// macro used internally to add implementations of ToTypesenseField for several rust types. |
42 | 12 | #[macro_export] |
43 | 13 | macro_rules! impl_to_typesense_field ( |
44 | 14 | ($from:ty, $typesense_variant:expr) => { |
45 | 15 | impl ToTypesenseField for $from { |
46 | | - fn to_typesense_type() -> FieldType { |
| 16 | + fn to_typesense_type() -> &'static str { |
47 | 17 | $typesense_variant |
48 | 18 | } |
49 | 19 | } |
50 | 20 | }; |
51 | 21 | ); |
52 | 22 |
|
53 | | -impl_to_typesense_field!(String, FieldType::String); |
54 | | -impl_to_typesense_field!(u8, FieldType::Int32); |
55 | | -impl_to_typesense_field!(i32, FieldType::Int32); |
56 | | -impl_to_typesense_field!(i64, FieldType::Int64); |
57 | | -impl_to_typesense_field!(u32, FieldType::Int64); |
58 | | -impl_to_typesense_field!(usize, FieldType::Int64); |
59 | | -impl_to_typesense_field!(f32, FieldType::Float); |
60 | | -impl_to_typesense_field!(f64, FieldType::Float); |
61 | | -impl_to_typesense_field!(bool, FieldType::Bool); |
62 | | -impl_to_typesense_field!(Vec<String>, FieldType::StringArray); |
63 | | -impl_to_typesense_field!(Vec<i32>, FieldType::Int32Array); |
64 | | -impl_to_typesense_field!(Vec<i64>, FieldType::Int64Array); |
65 | | -impl_to_typesense_field!(Vec<f32>, FieldType::FloatArray); |
66 | | -impl_to_typesense_field!(Vec<f64>, FieldType::FloatArray); |
67 | | -impl_to_typesense_field!(Vec<bool>, FieldType::BoolArray); |
| 23 | +impl_to_typesense_field!(String, "string"); |
| 24 | +impl_to_typesense_field!(u8, "int32"); |
| 25 | +impl_to_typesense_field!(i32, "int32"); |
| 26 | +impl_to_typesense_field!(i64, "int64"); |
| 27 | +impl_to_typesense_field!(u32, "int64"); |
| 28 | +impl_to_typesense_field!(usize, "int64"); |
| 29 | +impl_to_typesense_field!(f32, "float"); |
| 30 | +impl_to_typesense_field!(f64, "float"); |
| 31 | +impl_to_typesense_field!(bool, "bool"); |
| 32 | +impl_to_typesense_field!(Vec<String>, "string[]"); |
| 33 | +impl_to_typesense_field!(Vec<i32>, "int32[]"); |
| 34 | +impl_to_typesense_field!(Vec<i64>, "int64[]"); |
| 35 | +impl_to_typesense_field!(Vec<f32>, "float[]"); |
| 36 | +impl_to_typesense_field!(Vec<f64>, "float[]"); |
| 37 | +impl_to_typesense_field!(Vec<bool>, "bool[]"); |
0 commit comments