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
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
use crate::{Component, Place};
use failure::Fail;
use failure::{format_err, Error};
use itertools::Itertools;
use regex::{Regex, RegexBuilder};
use std::collections::HashMap;
use std::str::FromStr;
use strum::IntoEnumIterator;

const MULTILINE_TEMPLATE_NAME: &'static str = "multi_line";
const SHORT_ADDR_TEMPLATE_NAME: &'static str = "short_addr";

/// Represents a Regex and the value to replace the regex matches with
#[derive(Debug, Clone)]
pub(crate) struct Replacement {
    pub regex: regex::Regex,
    pub replacement_value: String,
}

/// Replacement rule
/// a Replacement can be on all fields, or only one of them
#[derive(Debug, Clone)]
pub(crate) enum ReplaceRule {
    All(Replacement),
    Component((Component, Replacement)),
}

#[derive(Debug, Hash, Eq, PartialEq, Clone)]
pub struct CountryCode(String); // TODO small string

impl FromStr for CountryCode {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        if s.len() == 2 {
            if s == "UK" {
                Ok(CountryCode("GB".to_owned()))
            } else {
                Ok(CountryCode(s.to_uppercase()))
            }
        } else {
            Err(format_err!(
                "{} is not a valid ISO3166-1:alpha2 country code",
                s,
            ))
        }
    }
}

impl CountryCode {
    pub fn as_str(&self) -> &str {
        self.0.as_str()
    }
}

impl std::fmt::Display for CountryCode {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

/// Represents a new field to add the a place
#[derive(Debug, Clone)]
pub(crate) struct NewComponent {
    pub component: Component,
    pub new_value: String,
}

/// The template handle the handlerbar template used to format a [`Place`](struct.Place.html)
#[derive(Debug, Default)]
pub(crate) struct Template {
    /// Moustache template
    pub handlebar_handler: handlebars::Handlebars,
    place_template: String, // used only to clone the template
}

// Compute a string with only the formatting rule for a short address
// (basicaly only the housenumber and the road)
// it's not very elegant, but it works to only find the line with the housenumber
fn compute_short_addr_template(place_template: &str) -> Option<String> {
    place_template
        .split("\n")
        .find(|l| l.contains("house_number"))
        .map(|l| l.trim().to_owned())
}

impl Template {
    pub fn new(place_template: &str) -> Self {
        let mut template_engine = crate::handlebar_helper::new_template_engine();
        template_engine
            .register_template_string(MULTILINE_TEMPLATE_NAME, place_template)
            .expect("impossible to build multi line template");

        if let Some(short_addr_template) = compute_short_addr_template(place_template) {
            template_engine
                .register_template_string(SHORT_ADDR_TEMPLATE_NAME, &short_addr_template)
                .expect("impossible to build short addr template");
        }

        Template {
            place_template: place_template.to_owned(),
            handlebar_handler: template_engine,
        }
    }
}

impl Clone for Template {
    fn clone(&self) -> Self {
        Self::new(self.place_template.as_str())
    }
}

/// The `Rules` contains all the rules used to cleanup the placees
/// Some of those rules are used as preformating rules (before changing the [`Place`](struct.Place.html)
/// to a text with the handlebar template)
/// And some of those rules are used as postformating rules, on the formatted text
#[derive(Debug, Default, Clone)]
pub(crate) struct Rules {
    pub replace: Vec<ReplaceRule>,
    pub postformat_replace: Vec<Replacement>,
    pub change_country: Option<String>,
    pub change_country_code: Option<String>,
    /// Override the country
    pub add_component: Option<NewComponent>,
}

#[derive(Debug)]
pub(crate) struct Templates {
    pub default_template: Template,
    pub fallback_template: Template,
    pub templates_by_country: HashMap<CountryCode, Template>,
    pub rules_by_country: HashMap<CountryCode, Rules>,
    pub fallback_templates_by_country: HashMap<CountryCode, Template>,
    pub fallback_rules: Rules,
}

/// This [`Formatter`](struct.Formatter.html) holds all the configuration needed to format a [`Place`](struct.Place.html)
/// to a nice text.
///
/// The main method is the `format` method, that takes a [`Place`](struct.Place.html)
/// or something that can be converted to a [`Place`](struct.Place.html) and return a result with the formatted `String`
///
/// ```
/// # #[macro_use] extern crate maplit;
/// # fn main() {
///    use address_formatter::Component::*;
///    let formatter = address_formatter::Formatter::default();
///
///    let addr: address_formatter::Place = hashmap!(
///        City => "Toulouse",
///        Country => "France",
///        CountryCode => "FR",
///        County => "Toulouse",
///        HouseNumber => "17",
///        Neighbourhood => "Lafourguette",
///        Postcode => "31000",
///        Road => "Rue du Médecin-Colonel Calbairac",
///        State => "Midi-Pyrénées",
///        Suburb => "Toulouse Ouest",
///    ).into();
///
///    assert_eq!(
///        formatter.format(addr).unwrap(),
///        r#"17 Rue du Médecin-Colonel Calbairac
///31000 Toulouse
///France
///"#
///        .to_owned()
///    )
/// # }
///
/// ```
pub struct Formatter {
    pub(crate) templates: Templates,
    pub(crate) county_codes: HashMap<(CountryCode, String), String>,
    pub(crate) state_codes: HashMap<(CountryCode, String), String>,
    // country_to_lang: Vec<>,
    // abbreviations: Vec<>,
    // valid_replacement_components: Vec<>
}

/// This configuration changes the [`Formatter`](struct.Formatter.html) behavior
#[derive(Default, Debug)]
pub struct Configuration {
    /// force the use of a give country (so the [`Place`](struct.Place.html) country_code is not used)
    pub country_code: Option<String>,
    /// use abbreviation in the formated text (like "Avenue" to "Av.")
    pub abbreviate: Option<bool>,
}

impl Default for Formatter {
    /// Default constructor
    fn default() -> Self {
        crate::read_configuration::read_configuration()
    }
}

impl Formatter {
    /// make a human readable text from a [`Place`](struct.Place.html)
    /// ```
    /// # #[macro_use] extern crate maplit;
    /// # fn main() {
    ///    use address_formatter::Component::*;
    ///    let formatter = address_formatter::Formatter::default();
    ///
    ///    let addr: address_formatter::Place = hashmap!(
    ///        City => "Toulouse",
    ///        Country => "France",
    ///        CountryCode => "FR",
    ///        County => "Toulouse",
    ///        HouseNumber => "17",
    ///        Neighbourhood => "Lafourguette",
    ///        Postcode => "31000",
    ///        Road => "Rue du Médecin-Colonel Calbairac",
    ///        State => "Midi-Pyrénées",
    ///        Suburb => "Toulouse Ouest",
    ///    ).into();
    ///
    ///    assert_eq!(
    ///        formatter.format(addr).unwrap(),
    ///        r#"17 Rue du Médecin-Colonel Calbairac
    ///31000 Toulouse
    ///France
    ///"#
    ///        .to_owned()
    ///    )
    /// # }
    /// ```
    pub fn format(&self, into_addr: impl Into<Place>) -> Result<String, Error> {
        self.format_with_config(into_addr.into(), Configuration::default())
    }

    /// make a human readable text from a [`Place`](struct.Place.html)
    /// Same as the [`format`](struct.Formatter.html#method.format) method,
    /// but with a [`Configuration`](address_formatter::formatter::Configuration) object
    pub fn format_with_config(
        &self,
        into_addr: impl Into<Place>,
        conf: Configuration,
    ) -> Result<String, Error> {
        let mut addr = into_addr.into();
        let country_code = self.find_country_code(&mut addr, conf);

        sanity_clean_place(&mut addr);

        let template = self.find_template(&addr, &country_code);
        let rules = country_code
            .as_ref()
            .and_then(|c| self.templates.rules_by_country.get(c))
            .unwrap_or_else(|| &self.templates.fallback_rules);

        self.preformat(&rules, &mut addr);

        let text = template
            .handlebar_handler
            .render(MULTILINE_TEMPLATE_NAME, &addr)
            .map_err(|e| e.context("impossible to render template"))?;

        let text = cleanup_rendered(&text, &rules);

        Ok(text)
    }

    /// make a human readable short text on 1 line with only the address [`Place`](struct.Place.html)
    /// There is basically only the housenumber and the road
    /// ```
    /// # #[macro_use] extern crate maplit;
    /// # fn main() {
    ///    use address_formatter::Component::*;
    ///    let formatter = address_formatter::Formatter::default();
    ///
    ///    let addr: address_formatter::Place = hashmap!(
    ///        City => "Toulouse",
    ///        Country => "France",
    ///        CountryCode => "FR",
    ///        County => "Toulouse",
    ///        HouseNumber => "17",
    ///        Neighbourhood => "Lafourguette",
    ///        Postcode => "31000",
    ///        Road => "Rue du Médecin-Colonel Calbairac",
    ///        State => "Midi-Pyrénées",
    ///        Suburb => "Toulouse Ouest",
    ///    ).into();
    ///
    ///    assert_eq!(
    ///        formatter.short_addr_format(addr).unwrap(),
    ///        r#"17 Rue du Médecin-Colonel Calbairac"#
    ///        .to_owned()
    ///    )
    /// # }
    /// ```
    pub fn short_addr_format(&self, into_addr: impl Into<Place>) -> Result<String, Error> {
        self.short_addr_format_with_config(into_addr.into(), Configuration::default())
    }

    /// make a human readable short text on 1 line with only the address [`Place`](struct.Place.html)
    /// Same as the [`short_addr_format`](struct.Formatter.html#method.short_addr_format) method,
    /// but with a [`Configuration`](address_formatter::formatter::Configuration) object
    pub fn short_addr_format_with_config(
        &self,
        into_addr: impl Into<Place>,
        conf: Configuration,
    ) -> Result<String, Error> {
        let mut addr = into_addr.into();
        let country_code = self.find_country_code(&mut addr, conf);

        let template = self.find_template(&addr, &country_code);

        let text = template
            .handlebar_handler
            .render(SHORT_ADDR_TEMPLATE_NAME, &addr)
            .map_err(|e| e.context("impossible to render short address template"))?;

        let text = text.trim().to_owned();
        Ok(text)
    }

    fn find_country_code(&self, addr: &mut Place, conf: Configuration) -> Option<CountryCode> {
        let mut country_code = conf
            .country_code
            .or_else(|| addr[Component::CountryCode].clone())
            .and_then(|s| {
                CountryCode::from_str(&s)
                    .map_err(|e| log::info!("impossible to find a country: {}", e))
                    .ok()
            });

        // we hardcode some country code values
        if country_code == CountryCode::from_str("NL").ok() {
            if let Some(state) = addr[Component::State].clone() {
                if state.as_str() == "Curaçao" {
                    country_code = CountryCode::from_str("CW").ok();
                    addr[Component::Country] = Some("Curaçao".to_owned());
                }
                let state = state.to_lowercase();

                if state.as_str() == "sint maarten" {
                    country_code = CountryCode::from_str("SX").ok();
                    addr[Component::Country] = Some("Sint Maarten".to_owned());
                } else if state.as_str() == "aruba" {
                    country_code = CountryCode::from_str("AW").ok();
                    addr[Component::Country] = Some("Aruba".to_owned());
                }
            }
        }

        country_code
    }

    fn find_template<'a>(
        &'a self,
        addr: &Place,
        country_code: &Option<CountryCode>,
    ) -> &'a Template {
        country_code
            .as_ref()
            .and_then(|c| {
                if !has_minimum_place_components(addr) {
                    // if the place does not have the minimum fields, we get its country fallback template
                    // if there is a specific one, else we get the default fallback template
                    self.templates
                        .fallback_templates_by_country
                        .get(&c)
                        .or_else(|| Some(&self.templates.fallback_template))
                } else {
                    self.templates.templates_by_country.get(&c)
                }
            })
            .unwrap_or(&self.templates.default_template)
    }

    fn preformat(&self, rules: &Rules, addr: &mut Place) {
        for r in &rules.replace {
            r.replace_fields(addr);
        }

        // in some cases, we need to add some components
        if let Some(add_component) = &rules.add_component {
            addr[add_component.component] = Some(add_component.new_value.clone());
        }
        if let Some(change_country) = &rules.change_country {
            addr[Component::Country] = Some(change_country.clone());
        }
        if let Some(change_country_code) = &rules.change_country_code {
            addr[Component::CountryCode] = Some(change_country_code.clone());
        }

        // we also try to find the state_code/county_code
        if let Some(country) = addr[Component::CountryCode]
            .as_ref()
            .and_then(|c| CountryCode::from_str(c).ok())
        {
            if addr[Component::StateCode].is_none() {
                // we try to see if we can use the state_code and the reference table 'state_codes.yaml' to find the state
                if let Some(state) = &addr[Component::State] {
                    if let Some(new_state) = self
                        .state_codes
                        .get(&(country.clone(), state.to_string()))
                        .cloned()
                    {
                        addr[Component::StateCode] = Some(new_state);
                    }
                }
            }

            if addr[Component::CountyCode].is_none() {
                // same for county
                if let Some(county) = &addr[Component::County] {
                    if let Some(new_county) = self
                        .county_codes
                        .get(&(country, county.to_string()))
                        .cloned()
                    {
                        addr[Component::County] = Some(new_county);
                    }
                }
            }
        }
    }
}

/// Build [`Place`](struct.Place.html) from a less structured input (like placees from [Nominatim](https://github.com/openstreetmap/Nominatim))
///
/// It applies aliases rules to fill the [`Place`](struct.Place.html)'s fields as good as possible.
pub struct PlaceBuilder {
    pub(crate) component_aliases: HashMap<Component, Vec<String>>,
}

impl Default for PlaceBuilder {
    fn default() -> Self {
        crate::read_configuration::read_place_builder_configuration()
    }
}

impl PlaceBuilder {
    /// Build a [`Place`](struct.Place.html)(crate::Place) from an unstructed source (like Nominatim output)
    pub fn build_place<'a>(&self, values: impl IntoIterator<Item = (&'a str, String)>) -> Place {
        let mut place = Place::default();
        let mut unknown = HashMap::<String, String>::new();
        for (k, v) in values.into_iter() {
            let component = Component::from_str(k).ok();;
            if let Some(component) = component {
                place[component] = Some(v);
            } else {
                unknown.insert(k.to_string(), v);
            }
        }

        // all the unknown fields are added in the 'Attention' field
        if !unknown.is_empty() {
            for (c, aliases) in &self.component_aliases {
                // if the place's component has not been already set, we set it to its first found alias
                for alias in aliases {
                    if let Some(a) = unknown.remove(alias) {
                        if place[*c].is_none() {
                            place[*c] = Some(a);
                        }
                    }
                }
            }
            place[Component::Attention] = Some(unknown.values().join(", "));
        }

        // hardocded cleanup for some bad country data
        if let (Some(state), Some(country)) = (&place[Component::State], &place[Component::Country])
        {
            if country.parse::<usize>().is_ok() {
                place[Component::Country] = Some(state.clone());
                place[Component::State] = None;
            }
        }
        place
    }
}

fn sanity_clean_place(addr: &mut Place) {
    lazy_static::lazy_static! {
        static ref POST_CODE_RANGE: Regex = Regex::new(r#"\d+;\d+"#).unwrap();
        static ref MATCHABLE_POST_CODE_RANGE: Regex = Regex::new(r#"^(\d{5}),\d{5}"#).unwrap();
        static ref IS_URL: Regex= Regex::new(r#"https?://"#).unwrap();

    }
    // cleanup the postcode
    if let Some(post_code) = &addr[Component::Postcode] {
        if post_code.len() > 20 || POST_CODE_RANGE.is_match(post_code) {
            addr[Component::Postcode] = None;
        } else if let Some(r) = MATCHABLE_POST_CODE_RANGE
            .captures(post_code)
            .and_then(|r| r.get(1))
            .map(|c| c.as_str())
        {
            addr[Component::Postcode] = Some(r.to_owned());
        }
    }

    // clean values containing URLs
    for c in Component::iter() {
        if let Some(v) = &addr[c] {
            if IS_URL.is_match(v) {
                addr[c] = None;
            }
        }
    }
}

fn cleanup_rendered(text: &str, rules: &Rules) -> String {
    use itertools::Itertools;
    lazy_static::lazy_static! {
        static ref REPLACEMENTS:  [(Regex, &'static str); 12]= [
            (RegexBuilder::new(r"[},\s]+$").multi_line(true).build().unwrap(), ""),
            (RegexBuilder::new(r"^ - ").multi_line(true).build().unwrap(), ""), // line starting with dash due to a parameter missing
            (RegexBuilder::new(r"^[,\s]+").multi_line(true).build().unwrap(), ""),
            (RegexBuilder::new(r",\s*,").multi_line(true).build().unwrap(), ", "), //multiple commas to one
            (RegexBuilder::new(r"[\t\p{Zs}]+,[\t\p{Zs}]+").multi_line(true).build().unwrap(), ", "), //one horiz whitespace behind comma
            (RegexBuilder::new(r"[\t ][\t ]+").multi_line(true).build().unwrap(), " "), //multiple horiz whitespace to one
            (RegexBuilder::new(r"[\t\p{Zs}]\n").multi_line(true).build().unwrap(), "\n"), //horiz whitespace, newline to newline
            (RegexBuilder::new(r"\n,").multi_line(true).build().unwrap(), "\n"), //newline comma to just newline
            (RegexBuilder::new(r",,+").multi_line(true).build().unwrap(), ","), //multiple commas to one
            (RegexBuilder::new(r",\n").multi_line(true).build().unwrap(), "\n"), //comma newline to just newline
            (RegexBuilder::new(r"\n[\t\p{Zs}]+").multi_line(true).build().unwrap(), "\n"), //newline plus space to newline
            (RegexBuilder::new(r"\n\n+").multi_line(true).build().unwrap(), "\n"), //multiple newline to one
        ];

        static ref FINAL_CLEANUP:  [(Regex, &'static str); 2]= [
            (Regex::new(r"^\s+").unwrap(), ""), //remove leading whitespace
            (Regex::new(r"\s+$").unwrap(), ""), //remove end whitespace
        ];
    }

    let mut res = text.to_owned();

    for (rgx, new_val) in REPLACEMENTS.iter() {
        let rep = rgx.replace_all(&res, *new_val);
        // to improve performance, we update the string only if it was changed by the replace
        match rep {
            std::borrow::Cow::Borrowed(_) => {}
            std::borrow::Cow::Owned(v) => {
                res = v;
            }
        }
    }

    for r in &rules.postformat_replace {
        let rep = r.regex.replace_all(&res, r.replacement_value.as_str());
        match rep {
            std::borrow::Cow::Borrowed(_) => {}
            std::borrow::Cow::Owned(v) => {
                res = v;
            }
        }
    }

    // we also dedup the string
    // we dedup and trim and all the same 'token' in a line
    // and all the same lines too
    let mut res = res
        .split('\n')
        .map(|s| s.split(", ").map(|e| e.trim()).dedup().join(", "))
        .dedup()
        .join("\n");

    for (rgx, new_val) in FINAL_CLEANUP.iter() {
        let rep = rgx.replace(&res, *new_val);
        match rep {
            std::borrow::Cow::Borrowed(_) => {}
            std::borrow::Cow::Owned(v) => {
                res = v;
            }
        }
    }

    let res = res.trim();
    format!("{}\n", res) //add final newline
}

fn has_minimum_place_components(addr: &Place) -> bool {
    // if there are neither 'road' nor 'postcode', we consider that there are not enough data
    // and use the fallback template
    addr[Component::Road].is_some() || addr[Component::Postcode].is_some()
}

impl ReplaceRule {
    fn replace_fields(&self, addr: &mut Place) {
        match self {
            ReplaceRule::All(replace_rule) => {
                for c in Component::iter() {
                    if let Some(v) = &addr[c] {
                        addr[c] = Some(
                            replace_rule
                                .regex
                                .replace(&v, replace_rule.replacement_value.as_str())
                                .to_string(),
                        );
                    }
                }
            }
            ReplaceRule::Component((c, replace_rule)) => {
                if let Some(v) = &addr[*c] {
                    addr[*c] = Some(
                        replace_rule
                            .regex
                            .replace(&v, replace_rule.replacement_value.as_str())
                            .to_string(),
                    );
                }
            }
        }
    }
}