Skip to content

Commit

Permalink
Add boolean fields
Browse files Browse the repository at this point in the history
  • Loading branch information
tontinton committed Jun 1, 2024
1 parent 1c3880e commit 3fd2412
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 4 deletions.
30 changes: 29 additions & 1 deletion src/commands/index.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use color_eyre::Result;
use color_eyre::{eyre::eyre, Result};
use sqlx::PgPool;
use tantivy::{
directory::MmapDirectory,
Expand Down Expand Up @@ -74,6 +74,34 @@ pub async fn run_index(args: IndexArgs, pool: PgPool) -> Result<()> {
}),
));
}
FieldType::Boolean(options) => {
let parse_string = options.parse_string;
let field = schema_builder.add_bool_field(&name, options);
field_parsers.push((
name,
field,
Box::new(move |value| {
if !parse_string {
return common_parse(value);
}

if let Ok(value_str) = serde_json::from_value::<String>(value.clone()) {
let trimmed = value_str.trim();
if trimmed.len() < 4 || trimmed.len() > 5 {
return Err(eyre!("cannot parse '{}' as boolean", trimmed));
}
let value_str = trimmed.to_lowercase();
match value_str.as_str() {
"true" => Ok(true.into()),
"false" => Ok(false.into()),
_ => Err(eyre!("cannot parse '{}' as boolean", trimmed)),
}
} else {
common_parse(value)
}
}),
));
}
FieldType::Datetime(options) => {
let field = schema_builder.add_date_field(&name, options.clone());
field_parsers.push((
Expand Down
35 changes: 35 additions & 0 deletions src/config/boolean.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use serde::{Deserialize, Serialize};
use tantivy::schema::NumericOptions;

use super::default_true;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BooleanFieldConfig {
#[serde(default = "default_true")]
pub stored: bool,

#[serde(default)]
pub fast: bool,

#[serde(default = "default_true")]
pub indexed: bool,

#[serde(default = "default_true")]
pub parse_string: bool,
}

impl From<BooleanFieldConfig> for NumericOptions {
fn from(config: BooleanFieldConfig) -> Self {
let mut options = NumericOptions::default();
if config.stored {
options = options.set_stored();
}
if config.indexed {
options = options.set_indexed();
}
if config.fast {
options = options.set_fast();
}
options
}
}
7 changes: 4 additions & 3 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod datetime;
pub mod boolean;
pub mod number;
pub mod text;

Expand All @@ -9,9 +10,7 @@ use serde::{Deserialize, Serialize};
use tokio::fs::read_to_string;

use self::{
datetime::DateTimeFieldConfig,
number::NumberFieldConfig,
text::{IndexedTextFieldType, TextFieldConfig},
boolean::BooleanFieldConfig, datetime::DateTimeFieldConfig, number::NumberFieldConfig, text::{IndexedTextFieldType, TextFieldConfig}
};

const VERSION: u32 = 1;
Expand All @@ -29,6 +28,7 @@ fn default_true() -> bool {
pub enum FieldType {
Text(TextFieldConfig),
Number(NumberFieldConfig),
Boolean(BooleanFieldConfig),
Datetime(DateTimeFieldConfig),
}

Expand All @@ -44,6 +44,7 @@ impl MappingConfig {
match &self.type_ {
Text(config) => !matches!(config.indexed, IndexedTextFieldType::False),
Number(config) => config.indexed,
Boolean(config) => config.indexed,
Datetime(config) => config.indexed,
}
}
Expand Down

0 comments on commit 3fd2412

Please sign in to comment.