File tree 3 files changed +68
-1
lines changed
3 files changed +68
-1
lines changed Original file line number Diff line number Diff line change 1
- use color_eyre:: Result ;
1
+ use color_eyre:: { eyre :: eyre , Result } ;
2
2
use sqlx:: PgPool ;
3
3
use tantivy:: {
4
4
directory:: MmapDirectory ,
@@ -74,6 +74,34 @@ pub async fn run_index(args: IndexArgs, pool: PgPool) -> Result<()> {
74
74
} ) ,
75
75
) ) ;
76
76
}
77
+ FieldType :: Boolean ( options) => {
78
+ let parse_string = options. parse_string ;
79
+ let field = schema_builder. add_bool_field ( & name, options) ;
80
+ field_parsers. push ( (
81
+ name,
82
+ field,
83
+ Box :: new ( move |value| {
84
+ if !parse_string {
85
+ return common_parse ( value) ;
86
+ }
87
+
88
+ if let Ok ( value_str) = serde_json:: from_value :: < String > ( value. clone ( ) ) {
89
+ let trimmed = value_str. trim ( ) ;
90
+ if trimmed. len ( ) < 4 || trimmed. len ( ) > 5 {
91
+ return Err ( eyre ! ( "cannot parse '{}' as boolean" , trimmed) ) ;
92
+ }
93
+ let value_str = trimmed. to_lowercase ( ) ;
94
+ match value_str. as_str ( ) {
95
+ "true" => Ok ( true . into ( ) ) ,
96
+ "false" => Ok ( false . into ( ) ) ,
97
+ _ => Err ( eyre ! ( "cannot parse '{}' as boolean" , trimmed) ) ,
98
+ }
99
+ } else {
100
+ common_parse ( value)
101
+ }
102
+ } ) ,
103
+ ) ) ;
104
+ }
77
105
FieldType :: Datetime ( options) => {
78
106
let field = schema_builder. add_date_field ( & name, options. clone ( ) ) ;
79
107
field_parsers. push ( (
Original file line number Diff line number Diff line change
1
+ use serde:: { Deserialize , Serialize } ;
2
+ use tantivy:: schema:: NumericOptions ;
3
+
4
+ use super :: default_true;
5
+
6
+ #[ derive( Debug , Clone , Serialize , Deserialize ) ]
7
+ pub struct BooleanFieldConfig {
8
+ #[ serde( default = "default_true" ) ]
9
+ pub stored : bool ,
10
+
11
+ #[ serde( default ) ]
12
+ pub fast : bool ,
13
+
14
+ #[ serde( default = "default_true" ) ]
15
+ pub indexed : bool ,
16
+
17
+ #[ serde( default = "default_true" ) ]
18
+ pub parse_string : bool ,
19
+ }
20
+
21
+ impl From < BooleanFieldConfig > for NumericOptions {
22
+ fn from ( config : BooleanFieldConfig ) -> Self {
23
+ let mut options = NumericOptions :: default ( ) ;
24
+ if config. stored {
25
+ options = options. set_stored ( ) ;
26
+ }
27
+ if config. indexed {
28
+ options = options. set_indexed ( ) ;
29
+ }
30
+ if config. fast {
31
+ options = options. set_fast ( ) ;
32
+ }
33
+ options
34
+ }
35
+ }
Original file line number Diff line number Diff line change
1
+ pub mod boolean;
1
2
pub mod datetime;
2
3
pub mod number;
3
4
pub mod text;
@@ -9,6 +10,7 @@ use serde::{Deserialize, Serialize};
9
10
use tokio:: fs:: read_to_string;
10
11
11
12
use self :: {
13
+ boolean:: BooleanFieldConfig ,
12
14
datetime:: DateTimeFieldConfig ,
13
15
number:: NumberFieldConfig ,
14
16
text:: { IndexedTextFieldType , TextFieldConfig } ,
@@ -29,6 +31,7 @@ fn default_true() -> bool {
29
31
pub enum FieldType {
30
32
Text ( TextFieldConfig ) ,
31
33
Number ( NumberFieldConfig ) ,
34
+ Boolean ( BooleanFieldConfig ) ,
32
35
Datetime ( DateTimeFieldConfig ) ,
33
36
}
34
37
@@ -44,6 +47,7 @@ impl MappingConfig {
44
47
match & self . type_ {
45
48
Text ( config) => !matches ! ( config. indexed, IndexedTextFieldType :: False ) ,
46
49
Number ( config) => config. indexed ,
50
+ Boolean ( config) => config. indexed ,
47
51
Datetime ( config) => config. indexed ,
48
52
}
49
53
}
You can’t perform that action at this time.
0 commit comments