Skip to main content

geocoder_core/profile/
default.rs

1//! Core addok behaviour, used as is by [`DefaultProfile`] and as building
2//! blocks by country profiles.
3
4use super::{Boost, FieldSpec, Profile};
5use crate::doc::{Document, first_str};
6use crate::text::{Token, TokenKind, is_digit};
7
8/// addok's default `FIELDS`.
9pub const FIELDS: &[FieldSpec] = &[
10    FieldSpec {
11        key: "name",
12        boost: Boost::Const(4.0),
13        required: true,
14    },
15    FieldSpec::new("street"),
16    FieldSpec {
17        key: "postcode",
18        boost: Boost::ByType {
19            doc_type: "municipality",
20            matched: 1.2,
21            otherwise: 1.0,
22        },
23        required: false,
24    },
25    FieldSpec::new("city"),
26    FieldSpec::new("context"),
27];
28
29/// addok's default `FILTERS`.
30pub const FILTERS: &[&str] = &["type", "postcode"];
31
32/// Profile with no language-specific processing.
33pub struct DefaultProfile;
34
35impl Profile for DefaultProfile {
36    fn name(&self) -> &'static str {
37        "default"
38    }
39
40    fn version(&self) -> u32 {
41        1
42    }
43}
44
45/// `addok.helpers.text.flag_housenumber`: a leading number.
46pub fn flag_housenumber(mut tokens: Vec<Token>) -> Vec<Token> {
47    for token in &mut tokens {
48        if token.is_first && is_digit(token) {
49            token.kind = Some(TokenKind::Housenumber);
50        }
51    }
52    tokens
53}
54
55/// `addok.helpers.results.make_labels`.
56pub fn make_labels(doc: &Document, housenumber: Option<&str>) -> Vec<String> {
57    let mut labels = crate::doc::str_values(doc, "name");
58    let Some(first) = labels.first().cloned() else {
59        return labels;
60    };
61    let mut label = first;
62    if let Some(city) = first_str(doc, "city").filter(|c| !c.is_empty())
63        && city != label
64    {
65        if let Some(postcode) = first_str(doc, "postcode").filter(|p| !p.is_empty()) {
66            label = format!("{label} {postcode}");
67        }
68        label = format!("{label} {city}");
69        labels.insert(0, label.clone());
70    }
71    if let Some(housenumber) = housenumber.filter(|h| !h.is_empty()) {
72        labels.insert(0, format!("{housenumber} {label}"));
73    }
74    labels
75}