typesense/field/
mod.rs

1//! Module with the common definitions for  the
2//! [fields](https://github.com/typesense/typesense/blob/v0.19.0/include/field.)
3//! available in Typesense.
4
5mod field_type;
6pub use field_type::*;
7pub use typesense_codegen::models::{Field, FieldEmbed};
8
9/// Builder for the `Field` struct.
10#[derive(Debug, Default)]
11pub struct FieldBuilder {
12    name: String,
13    typesense_type: FieldType,
14    optional: Option<bool>,
15    facet: Option<bool>,
16    index: Option<bool>,
17    locale: Option<String>,
18    sort: Option<bool>,
19    infix: Option<bool>,
20    num_dim: Option<i32>,
21    drop: Option<bool>,
22    embed: Option<Box<FieldEmbed>>,
23}
24
25impl FieldBuilder {
26    /// Create a Builder
27    #[inline]
28    pub fn new(name: impl Into<String>, typesense_type: FieldType) -> Self {
29        Self {
30            name: name.into(),
31            typesense_type,
32            ..Default::default()
33        }
34    }
35
36    /// Set if field is optional.
37    #[inline]
38    pub fn optional(mut self, optional: Option<bool>) -> Self {
39        self.optional = optional;
40        self
41    }
42
43    /// Set if field is facet.
44    #[inline]
45    pub fn facet(mut self, facet: Option<bool>) -> Self {
46        self.facet = facet;
47        self
48    }
49
50    /// Set if field is index.
51    #[inline]
52    pub fn index(mut self, index: Option<bool>) -> Self {
53        self.index = index;
54        self
55    }
56
57    /// Set field locale.
58    #[inline]
59    pub fn locale(mut self, locale: Option<String>) -> Self {
60        self.locale = locale;
61        self
62    }
63
64    /// Set sort attribute for field
65    #[inline]
66    pub fn sort(mut self, sort: Option<bool>) -> Self {
67        self.sort = sort;
68        self
69    }
70
71    /// Set infix attribute for field
72    #[inline]
73    pub fn infix(mut self, infix: Option<bool>) -> Self {
74        self.infix = infix;
75        self
76    }
77
78    /// Set num_dim attribute for field
79    #[inline]
80    pub fn num_dim(mut self, num_dim: Option<i32>) -> Self {
81        self.num_dim = num_dim;
82        self
83    }
84
85    /// Set drop attribute for field
86    #[inline]
87    pub fn drop(mut self, drop: Option<bool>) -> Self {
88        self.drop = drop;
89        self
90    }
91
92    /// Create a `Field` with the current values of the builder,
93    /// It can fail if the name or the typesense_type are not defined.
94    #[inline]
95    pub fn build(self) -> Field {
96        Field {
97            name: self.name,
98            r#type: self.typesense_type,
99            optional: self.optional,
100            facet: self.facet,
101            index: self.index,
102            locale: self.locale,
103            sort: self.sort,
104            infix: self.infix,
105            num_dim: self.num_dim,
106            drop: self.drop,
107            embed: self.embed,
108        }
109    }
110}