[][src]Crate address_formatter

Universal international address formatter in Rust - data from https://github.com/OpenCageData/address-formatting

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

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

   use address_formatter::Component::*;
   let formatter = address_formatter::Formatter::default();

   // create an Address from a HashMap.
   // We could also have created an Address by settings all its fields
   let addr: address_formatter::Address = 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()
   )

If your data are less stuctured, you can use the AddressBuilder to build an Address

   let formatter = address_formatter::Formatter::default();
   let addr_builder = address_formatter::AddressBuilder::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_address(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

Address

An Address is a structured way to represent a postal address.Address

AddressBuilder

Build Address from a less structured input (like addresses from Nominatim)

Configuration

This configuration changes the Formatter behavior

Formatter

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

Enums

Component

A Component is a field of an Address