attribute_search_engine/index/mod.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
use crate::{Query, Result, SearchEngineError};
use std::{collections::HashSet, str::FromStr};
mod btree_range;
mod hashmap;
mod prefix;
pub use btree_range::*;
pub use hashmap::*;
pub use prefix::*;
/// This trait describes the minimum features an Index must support to be
/// usable as a SearchIndex, for example in a [SearchEngine](crate::engine::SearchEngine).
pub trait SearchIndex<P> {
/// Perform a search on an index.
///
/// This function returns an HashSet of all matching results.
/// It may not accept all enum values of [Query] but only
/// a small subset. If a [Query] is not supported,
/// this function returns [UnsupportedQuery](crate::error::SearchEngineError::UnsupportedQuery).
///
/// If the strings in the [Query] cannot be parsed to
/// the expected payload type, this function returns
/// [MismatchedQueryType](crate::error::SearchEngineError::MismatchedQueryType).
fn search(&self, query: &Query) -> Result<HashSet<P>>;
}
/// Tries to parse a string into a payload value.
///
/// This is an internal function. If it fails it returns
/// [MismatchedQueryType](crate::error::SearchEngineError::MismatchedQueryType)
/// to signal failure.
fn string_to_payload_type<T: FromStr>(value: &str) -> Result<T> {
value
.parse()
.map_err(|_| SearchEngineError::MismatchedQueryType)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_string_to_payload_type() {
assert_eq!(string_to_payload_type("1234"), Ok(1234i32));
assert_eq!(string_to_payload_type("123456"), Ok(123456usize));
assert_eq!(string_to_payload_type("true"), Ok(true));
assert_eq!(
string_to_payload_type::<usize>("Hello"),
Err(SearchEngineError::MismatchedQueryType)
);
}
}