pub mod be;
mod benelux;
pub mod default;
pub mod fr;
pub mod lu;
use std::sync::Arc;
use crate::doc::Document;
use crate::text::{Synonyms, Token, tokenize};
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize)]
pub struct PostalLine {
pub code: Option<String>,
pub label: String,
}
impl PostalLine {
pub fn line(&self) -> String {
match &self.code {
Some(code) => format!("{code} {}", self.label),
None => self.label.clone(),
}
}
}
#[derive(Debug, Clone)]
pub struct Postal {
pub line: PostalLine,
pub range: std::ops::Range<usize>,
pub locality: String,
}
#[derive(Debug, Clone, Copy)]
pub enum Boost {
Const(f64),
ByType {
doc_type: &'static str,
matched: f64,
otherwise: f64,
},
}
impl Boost {
pub fn value(&self, doc: &Document) -> f64 {
match *self {
Boost::Const(v) => v,
Boost::ByType {
doc_type,
matched,
otherwise,
} => {
if doc.get("type").and_then(|v| v.as_str()) == Some(doc_type) {
matched
} else {
otherwise
}
}
}
}
}
#[derive(Debug, Clone, Copy)]
pub struct FieldSpec {
pub key: &'static str,
pub boost: Boost,
pub required: bool,
}
impl FieldSpec {
pub const fn new(key: &'static str) -> Self {
Self {
key,
boost: Boost::Const(1.0),
required: false,
}
}
}
pub trait Profile: Send + Sync {
fn name(&self) -> &'static str;
fn version(&self) -> u32;
fn fields(&self) -> &[FieldSpec] {
default::FIELDS
}
fn filters(&self) -> &[&'static str] {
default::FILTERS
}
fn fuzzy_key_map(&self) -> Option<crate::search::fuzzy::KeyMap> {
Some(crate::search::fuzzy::AZERTY)
}
fn min_limit(&self) -> usize {
1
}
fn min_score(&self) -> f64 {
crate::settings::MIN_SCORE
}
fn synonyms(&self) -> Option<&Synonyms> {
None
}
fn process_query(&self, query: &str) -> String {
query.to_string()
}
fn postal(&self, _query: &str) -> Option<Postal> {
None
}
fn process_tokens(&self, tokens: Vec<Token>) -> Vec<Token> {
let tokens = default::flag_housenumber(tokens);
match self.synonyms() {
Some(synonyms) => synonyms.apply(tokens),
None => tokens,
}
}
fn make_labels(&self, doc: &Document, housenumber: Option<&str>) -> Vec<String> {
default::make_labels(doc, housenumber)
}
fn display_label(
&self,
_doc: &Document,
_housenumber: Option<&str>,
_query: &str,
) -> Option<String> {
None
}
}
pub fn preprocess(profile: &dyn Profile, text: &str) -> Vec<Token> {
profile.process_tokens(tokenize(text))
}
pub fn preprocess_query(profile: &dyn Profile, query: &str) -> Vec<Token> {
profile.process_tokens(tokenize(&profile.process_query(query)))
}
pub fn by_name(name: &str) -> Option<Arc<dyn Profile>> {
match name {
"default" => Some(Arc::new(default::DefaultProfile)),
"fr" => Some(Arc::new(fr::FrProfile::new())),
"lu" => Some(Arc::new(lu::LuProfile)),
"be" => Some(Arc::new(be::BeProfile)),
_ => None,
}
}
pub fn names() -> &'static [&'static str] {
&["default", "fr", "lu", "be"]
}