Skip to content

Commit bb804ec

Browse files
committedJun 1, 2024·
Add boolean fields
1 parent cfb6d93 commit bb804ec

File tree

3 files changed

+68
-1
lines changed

3 files changed

+68
-1
lines changed
 

‎src/commands/index.rs

+29-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use color_eyre::Result;
1+
use color_eyre::{eyre::eyre, Result};
22
use sqlx::PgPool;
33
use tantivy::{
44
directory::MmapDirectory,
@@ -74,6 +74,34 @@ pub async fn run_index(args: IndexArgs, pool: PgPool) -> Result<()> {
7474
}),
7575
));
7676
}
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+
}
77105
FieldType::Datetime(options) => {
78106
let field = schema_builder.add_date_field(&name, options.clone());
79107
field_parsers.push((

‎src/config/boolean.rs

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
}

‎src/config/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
pub mod boolean;
12
pub mod datetime;
23
pub mod number;
34
pub mod text;
@@ -9,6 +10,7 @@ use serde::{Deserialize, Serialize};
910
use tokio::fs::read_to_string;
1011

1112
use self::{
13+
boolean::BooleanFieldConfig,
1214
datetime::DateTimeFieldConfig,
1315
number::NumberFieldConfig,
1416
text::{IndexedTextFieldType, TextFieldConfig},
@@ -29,6 +31,7 @@ fn default_true() -> bool {
2931
pub enum FieldType {
3032
Text(TextFieldConfig),
3133
Number(NumberFieldConfig),
34+
Boolean(BooleanFieldConfig),
3235
Datetime(DateTimeFieldConfig),
3336
}
3437

@@ -44,6 +47,7 @@ impl MappingConfig {
4447
match &self.type_ {
4548
Text(config) => !matches!(config.indexed, IndexedTextFieldType::False),
4649
Number(config) => config.indexed,
50+
Boolean(config) => config.indexed,
4751
Datetime(config) => config.indexed,
4852
}
4953
}

0 commit comments

Comments
 (0)
Please sign in to comment.