Skip to content

Commit 7204ec3

Browse files
committed
Use structs from generated code for collectionSchema and Fields
1 parent a671cad commit 7204ec3

File tree

5 files changed

+72
-108
lines changed

5 files changed

+72
-108
lines changed

typesense/src/collection/schema.rs

Lines changed: 7 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,8 @@
44
//! is roughly equivalent to a table in a relational database.
55
//!
66
use crate::field::Field;
7-
use serde::{Deserialize, Serialize};
7+
pub use typesense_codegen::models::CollectionSchema;
88

9-
/// Schema used to create collections in the [Typesense API](https://typesense.org/docs/0.19.0/api/collections.html#create-a-collection).
10-
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
11-
pub struct CollectionSchema {
12-
/// Name of the collection you wish to create.
13-
name: String,
14-
/// A list of fields that you wish to index for querying, filtering and faceting.
15-
fields: Vec<Field>,
16-
/// Optional: The name of an int32 / float field that determines the order in which the search
17-
/// results are ranked when a sort_by clause is not provided during searching.
18-
/// When not present, text match score and insertion order are used
19-
#[serde(skip_serializing_if = "Option::is_none")]
20-
default_sorting_field: Option<String>,
21-
}
22-
23-
impl CollectionSchema {
24-
/// Reference to the name of the Collection
25-
pub fn name(&self) -> &String {
26-
&self.name
27-
}
28-
29-
/// Reference to the fields of the Collection
30-
pub fn fields(&self) -> &Vec<Field> {
31-
&self.fields
32-
}
33-
34-
/// Reference to the default_sorting_field of the Collection
35-
pub fn default_sorting_field(&self) -> &Option<String> {
36-
&self.default_sorting_field
37-
}
38-
}
399
/// Builder for the [CollectionSchema] struct.
4010
#[derive(Debug, Default)]
4111
pub struct CollectionSchemaBuilder {
@@ -84,6 +54,8 @@ impl CollectionSchemaBuilder {
8454
name: self.name.ok_or("name is not set")?,
8555
fields: self.fields.ok_or("typesense_type is not set")?,
8656
default_sorting_field: self.default_sorting_field,
57+
symbols_to_index: None,
58+
token_separators: None,
8759
})
8860
}
8961
}
@@ -97,15 +69,15 @@ mod test {
9769
#[test]
9870
fn collection_schema_serializes_as_expected() {
9971
let fields = [
100-
("company_name", FieldType::String, None),
101-
("num_employees", FieldType::Int32, None),
102-
("country", FieldType::String, Some(true)),
72+
("company_name", "string", None),
73+
("num_employees", "int32", None),
74+
("country", "string", Some(true)),
10375
]
10476
.iter()
10577
.map(|(name, typesense_type, facet)| {
10678
FieldBuilder::new()
10779
.name(name.to_string())
108-
.typesense_type(*typesense_type)
80+
.typesense_type(typesense_type.to_string())
10981
.facet(*facet)
11082
.build()
11183
.unwrap()

typesense/src/field/field_type.rs

Lines changed: 19 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,37 @@
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;
333

344
/// Trait that should implement each type of a document, in order to properly serialize the
355
/// Collection Schema according to the Typesense reference.
366
pub trait ToTypesenseField {
377
/// Static function that should implement the types of the typesense documents.
38-
fn to_typesense_type() -> FieldType;
8+
fn to_typesense_type() -> &'static str;
399
}
4010

4111
/// macro used internally to add implementations of ToTypesenseField for several rust types.
4212
#[macro_export]
4313
macro_rules! impl_to_typesense_field (
4414
($from:ty, $typesense_variant:expr) => {
4515
impl ToTypesenseField for $from {
46-
fn to_typesense_type() -> FieldType {
16+
fn to_typesense_type() -> &'static str {
4717
$typesense_variant
4818
}
4919
}
5020
};
5121
);
5222

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[]");

typesense/src/field/mod.rs

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,9 @@
22
//! [fields](https://github.com/typesense/typesense/blob/v0.19.0/include/field.)
33
//! available in Typesense.
44
5-
use serde::{Deserialize, Serialize};
6-
75
mod field_type;
86
pub use field_type::*;
9-
10-
/// Struct used to represent a [field](https://github.com/typesense/typesense/blob/v0.19.0/include/field.) in Typesense.
11-
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
12-
pub struct Field {
13-
/// Required. The name of the field.
14-
name: String,
15-
/// Required. The `FieldType` of the field.
16-
#[serde(rename = "type")]
17-
typesense_type: FieldType,
18-
/// Optional parameter. Indicates if the field is optional or not.
19-
#[serde(skip_serializing_if = "Option::is_none")]
20-
optional: Option<bool>,
21-
/// Optional parameter. Faceted fields are indexed verbatim without
22-
/// any tokenization or preprocessing. For example, if you are building
23-
/// a product search, color and brand could be defined as facet fields.
24-
#[serde(skip_serializing_if = "Option::is_none")]
25-
facet: Option<bool>,
26-
}
7+
pub use typesense_codegen::models::Field;
278

289
/// Builder for the `Field` struct.
2910
#[derive(Debug, Default)]
@@ -32,6 +13,11 @@ pub struct FieldBuilder {
3213
typesense_type: Option<FieldType>,
3314
optional: Option<bool>,
3415
facet: Option<bool>,
16+
index: Option<bool>,
17+
locale: Option<String>,
18+
sort: Option<bool>,
19+
drop: Option<bool>,
20+
infix: Option<bool>,
3521
}
3622

3723
impl FieldBuilder {
@@ -64,14 +50,49 @@ impl FieldBuilder {
6450
self
6551
}
6652

53+
/// Set if field is index.
54+
pub fn index(mut self, index: Option<bool>) -> Self {
55+
self.index = index;
56+
self
57+
}
58+
59+
/// Set field locale.
60+
pub fn locale(mut self, locale: Option<String>) -> Self {
61+
self.locale = locale;
62+
self
63+
}
64+
65+
/// Set sort attribute from field
66+
pub fn sort(mut self, sort: Option<bool>) -> Self {
67+
self.sort = sort;
68+
self
69+
}
70+
71+
/// Set drop attribute from field
72+
pub fn drop(mut self, drop: Option<bool>) -> Self {
73+
self.drop = drop;
74+
self
75+
}
76+
77+
/// Set infix attribute from field
78+
pub fn infix(mut self, infix: Option<bool>) -> Self {
79+
self.infix = infix;
80+
self
81+
}
82+
6783
/// Create a `Field` with the current values of the builder,
6884
/// It can fail if the name or the typesense_type are not defined.
6985
pub fn build(self) -> Result<Field, Box<dyn std::error::Error>> {
7086
Ok(Field {
7187
name: self.name.ok_or("name is not set")?,
72-
typesense_type: self.typesense_type.ok_or("typesense_type is not set")?,
88+
_type: self.typesense_type.ok_or("typesense_type is not set")?,
7389
optional: self.optional,
7490
facet: self.facet,
91+
index: self.index,
92+
sort: self.sort,
93+
drop: self.drop,
94+
locale: self.locale,
95+
infix: self.infix,
7596
})
7697
}
7798
}

typesense/tests/collection_client.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
use serde::{Deserialize, Serialize};
2-
use typesense::document::Document as DocumentTrait;
3-
use typesense::ClientBuilder;
42
use typesense::Document;
53

64
#[cfg(all(test, feature = "tokio-rt", not(target_arch = "wasm32")))]
75
mod hyper_tests {
86
use super::*;
7+
use typesense::document::Document as DocumentTrait;
8+
use typesense::ClientBuilder;
9+
910
#[tokio::test]
1011
async fn collection_create() {
1112
let host = "http://localhost:5000";

typesense_derive/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ fn to_typesense_field_type(field: &Field) -> syn::Result<proc_macro2::TokenStrea
385385
(&field.ty, quote!(None))
386386
};
387387
let typesense_field_type = quote!(
388-
<#ty as typesense::field::ToTypesenseField>::to_typesense_type()
388+
<#ty as typesense::field::ToTypesenseField>::to_typesense_type().to_string()
389389
);
390390
Ok(quote! {
391391
typesense::field::FieldBuilder::new()

0 commit comments

Comments
 (0)