Skip to main content

Crate geo_kit

Crate geo_kit 

Source
Expand description

§geo-kit

Typed newtypes for validated geo primitives — replaces hand-rolled is_valid_* checks with parse-once, use-everywhere strong types.

TypeValidation
UkPostcodeUK postcode ^[A-Z]{1,2}[0-9][A-Z0-9]? [0-9][A-Z]{2}$, space optional, uppercase normalized
UsZipCodeUS ZIP ^\d{5}(-\d{4})?$
PostcodeGeneric postcode (UK or US)
CountryCodeISO 3166-1 alpha-2 ^[A-Z]{2}$, with country_name() mapping
CoordsLatitude -90..=90, longitude -180..=180, finite
AddressValidated address with non-empty lines, postcode, country, optional coords

§Example

use geo_kit::{UkPostcode, CountryCode, Coords, Address};

let pc = UkPostcode::parse("SW1A 1AA").expect("valid");
assert_eq!(pc.as_str(), "SW1A 1AA");

let cc = CountryCode::parse("GB").expect("valid");
assert_eq!(cc.country_name(), "United Kingdom");

let coords = Coords::new(51.5074, -0.1278).expect("valid");
assert!(coords.lat > 51.0);

let addr = Address::new(
    "10 Downing St".to_string(),
    None,
    "London".to_string(),
    None,
    pc,
    cc,
).expect("valid");
assert_eq!(addr.city(), "London");

All postcode/country types implement TryFrom<String>, FromStr, Display, Deref<Target=str>, AsRef<str>, and optional serde transparent (de)serialization.

Re-exports§

pub use address::Address;
pub use address::AddressBuilder;
pub use address::PostcodeChoice;
pub use coords::Coords;
pub use coords::is_valid_coords;
pub use country::CountryCode;
pub use country::is_valid_country_code;
pub use error::GeoError;
pub use postcode::Postcode;
pub use postcode::UkPostcode;
pub use postcode::UsZipCode;
pub use postcode::is_valid_uk_postcode;
pub use postcode::is_valid_us_zip;

Modules§

address
Address newtype with validated components.
coords
Geographic coordinates newtype.
country
ISO 3166-1 alpha-2 country code newtype.
error
Error types for geo-kit.
postcode
Postcode newtypes — UK and US.