type setting struct

This commit is contained in:
Marin Postma 2021-05-10 17:30:09 +02:00
parent 998d5ead34
commit 706643dfed
No known key found for this signature in database
GPG key ID: D5241F0C0C865F30
11 changed files with 47 additions and 29 deletions

View file

@ -1,4 +1,4 @@
use std::collections::{BTreeSet, HashSet};
use std::{collections::{BTreeSet, HashSet}, marker::PhantomData};
use std::ops::Deref;
use std::sync::Arc;
@ -8,7 +8,7 @@ use serde_json::{Map, Value};
use crate::helpers::EnvSizer;
pub use search::{SearchQuery, SearchResult, DEFAULT_SEARCH_LIMIT};
pub use updates::{Facets, Settings};
pub use updates::{Facets, Settings, Checked, Unchecked};
mod search;
mod updates;
@ -27,7 +27,7 @@ impl Deref for Index {
}
impl Index {
pub fn settings(&self) -> anyhow::Result<Settings> {
pub fn settings(&self) -> anyhow::Result<Settings<Checked>> {
let txn = self.read_txn()?;
let displayed_attributes = self
@ -68,6 +68,7 @@ impl Index {
ranking_rules: Some(Some(criteria)),
stop_words: Some(Some(stop_words)),
distinct_attribute: Some(distinct_attribute),
_kind: PhantomData,
})
}

View file

@ -1,6 +1,7 @@
use std::collections::{BTreeSet, HashMap};
use std::io;
use std::num::NonZeroUsize;
use std::marker::PhantomData;
use flate2::read::GzDecoder;
use log::info;
@ -10,10 +11,15 @@ use serde::{de::Deserializer, Deserialize, Serialize};
use super::Index;
use crate::index_controller::UpdateResult;
#[derive(Clone, Default, Debug)]
pub struct Checked;
#[derive(Clone, Default, Debug)]
pub struct Unchecked;
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
#[serde(rename_all = "camelCase")]
pub struct Settings {
pub struct Settings<T> {
#[serde(
default,
deserialize_with = "deserialize_some",
@ -49,21 +55,31 @@ pub struct Settings {
skip_serializing_if = "Option::is_none"
)]
pub distinct_attribute: Option<Option<String>>,
#[serde(skip)]
pub _kind: PhantomData<T>,
}
impl Settings {
pub fn cleared() -> Self {
Self {
impl Settings<Checked> {
pub fn cleared() -> Settings<Checked> {
Settings {
displayed_attributes: Some(None),
searchable_attributes: Some(None),
attributes_for_faceting: Some(None),
ranking_rules: Some(None),
stop_words: Some(None),
distinct_attribute: Some(None),
_kind: PhantomData,
}
}
}
impl Settings<Unchecked> {
pub fn check(self) -> Settings<Checked> {
todo!()
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
#[serde(rename_all = "camelCase")]
@ -137,7 +153,7 @@ impl Index {
pub fn update_settings(
&self,
settings: &Settings,
settings: &Settings<Checked>,
update_builder: UpdateBuilder,
) -> anyhow::Result<UpdateResult> {
// We must use the write transaction of the update here.