zip_codes_plus/lib.rs
1//! A lookup table for all primary U.S. ZIP codes.
2//!
3//! # Examples
4//!
5//! ```
6//! assert_eq!(zip_codes_plus::map().get("10465").unwrap().state, "NY");
7//! assert_eq!(zip_codes_plus::map().get("53186").unwrap().city, "Waukesha");
8//! assert_eq!(
9//! zip_codes_plus::by_city("Westwood, NJ").unwrap()
10//! .iter()
11//! .map(|r| r.zip_code)
12//! .collect::<Vec<&str>>(),
13//! vec!["07675"]
14//! );
15//! assert_eq!(
16//! zip_codes_plus::by_city("Whippany, NJ").unwrap()
17//! .iter()
18//! .map(|r| r.zip_code)
19//! .collect::<Vec<&str>>(),
20//! vec!["07983", "07999", "07981"]
21//! );
22//! assert!(
23//! zip_codes_plus::by_city("Nowhere, LA").is_none()
24//! );
25//! ```
26
27pub struct Record {
28 /// The 5-digit ZIP code.
29 pub zip_code: &'static str,
30 /// The ZIP code classification.
31 pub zip_code_type: Type,
32 /// The city to which the ZIP code belongs (all uppercase).
33 pub city: &'static str,
34 /// The state to which the ZIP code belongs (two letter abbreviation).
35 pub state: &'static str,
36 /// Latitude and longitude
37 pub coordinates: Option<(f64, f64)>,
38 /// If `true`, the ZIP code is historical; if `false`, the ZIP code is current.
39 pub is_decommissioned: bool,
40 /// The number of individual tax returns filed as of 2008.
41 pub tax_returns_filed: Option<u64>,
42 /// The estimated population of the area of the ZIP code.
43 pub estimated_population: Option<u64>,
44 /// Total yearly wages of the population.
45 pub total_wages: Option<u64>,
46}
47
48/// The classification of a ZIP code.
49#[derive(Clone, Debug)]
50pub enum Type {
51 /// Typical ZIP code (does not fall under any other type).
52 Standard,
53 /// Used only for PO Boxes at a given facility, not for any other type of delivery.
54 PoBox,
55 /// Assigned to a single high-volume address.
56 Unique,
57 /// Used to route mail for the U.S. military.
58 Military,
59}
60
61include!(concat!(env!("OUT_DIR"), "/codegen.rs"));
62
63pub type Map = phf::Map<&'static str, Record>;
64
65/// Returns a reference to a static lookup table for all primary U.S. ZIP codes.
66#[inline]
67pub fn map() -> &'static Map {
68 &ZIP_CODES
69}
70
71/// Returns a `Vec<&Record>` based on a "City, State"
72#[inline]
73pub fn by_city(city: &str) -> Option<Vec<&Record>> {
74 Some(
75 CITY_MAP
76 .get(city)?
77 .iter()
78 .map(|x| ZIP_CODES.get(*x).unwrap())
79 .collect(),
80 )
81}