Crate address_formatter[][src]

Expand description

Universal international place formatter in Rust

This crate is based on the amazing work of OpenCage Data who collected so many international formats of postal placees.

The easiest way to use this crate is to create a Place and format it. The Formatter will try to autodetect the country of the Place (this detection can be overriden with some Configuration) and format the postal place using the opencage rules for this country.

   use address_formatter::Component::*;

   // create a Place from a HashMap.
   // We could also have created a Place by settings all its fields
   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!(
       address_formatter::FORMATTER.format(addr).unwrap(),
       r#"17 Rue du Médecin-Colonel Calbairac
31000 Toulouse
France
"#
       .to_owned()
   )

If your data are less stuctured, you can use the PlaceBuilder to build a Place

   // use can either use the provider singleton address_formatter::FORMATTER or build your own
   let formatter = address_formatter::Formatter::default();
   let addr_builder = address_formatter::PlaceBuilder::default();
   let data = [
       ("building", "Mairie (bureaux administratifs)"),
       ("city", "Papeete"),
       (
           "country",
           "Polynésie française, Îles du Vent (eaux territoriales)",
       ),
       ("country_code", "fr"),
       ("county", "Îles du Vent"),
       ("postcode", "98714"),
       ("road", "Rue des Remparts"),
       ("state", "French Polynesia"),
   ];

   let addr =
       addr_builder.build_place(data.into_iter().map(|(k, v)| (k.clone(), v.to_string())));

   assert_eq!(
       formatter.format(addr).unwrap(),
       r#"Mairie (bureaux administratifs)
Rue des Remparts
98714 Papeete
Polynésie française
"#
       .to_owned()
   )

Structs

This configuration changes the Formatter behavior

Singleton to ease use of the Formatter

This Formatter holds all the configuration needed to format a Place to a nice text.

A Place is a structured way to represent a postal address.

Build Place from a less structured input (like placees from Nominatim)

Enums

A Component is a field of a Place