Skip to main content

geocoder_core/text/
token.rs

1/// `addok.helpers.text.Token`: a normalized word with the metadata the
2/// pipeline and the collectors rely on.
3#[derive(Debug, Clone, PartialEq)]
4pub struct Token {
5    pub value: String,
6    /// Positions, one per level of subdivision (a synonym expanding into
7    /// several words appends the sub-position).
8    pub positions: Vec<usize>,
9    pub is_first: bool,
10    pub is_last: bool,
11    pub kind: Option<TokenKind>,
12}
13
14#[derive(Debug, Clone, Copy, PartialEq, Eq)]
15pub enum TokenKind {
16    Housenumber,
17}
18
19impl Token {
20    pub fn new(value: String, position: usize) -> Self {
21        Self {
22            value,
23            positions: vec![position],
24            is_first: position == 0,
25            is_last: false,
26            kind: None,
27        }
28    }
29
30    /// `Token.update`: a new value keeping the metadata. `is_first` is
31    /// recomputed from the first position, like addok does.
32    pub fn with_value(&self, value: String) -> Self {
33        Self {
34            value,
35            ..self.clone()
36        }
37    }
38
39    /// `Token.update(value, position=p)`: the sub-position is appended.
40    pub fn with_value_at(&self, value: String, sub_position: usize) -> Self {
41        let mut token = self.with_value(value);
42        token.positions.push(sub_position);
43        token.is_first = token.positions[0] == 0;
44        token
45    }
46
47    pub fn is_housenumber(&self) -> bool {
48        self.kind == Some(TokenKind::Housenumber)
49    }
50}
51
52impl std::ops::Deref for Token {
53    type Target = str;
54    fn deref(&self) -> &str {
55        &self.value
56    }
57}