fips_codes/lib.rs
1#![allow(unused_parens)]
2/// Data sourced from <https://transition.fcc.gov/oet/info/maps/census/fips/fips.txt>
3mod data;
4
5/// ```
6/// use fips_codes::state_by_id;
7/// assert_eq!(state_by_id(75), None);
8/// assert_eq!(state_by_id(39), Some("Ohio"));
9/// ```
10#[inline]
11pub fn state_by_id(id: u8) -> Option<&'static str> {
12 county_by_ids(id, 0)
13}
14
15/// ```
16/// use fips_codes::county_by_ids;
17/// assert_eq!(county_by_ids(9, 15), Some("Windham County"));
18/// assert_eq!(county_by_ids(9, 2), None);
19/// assert_eq!(county_by_ids(0, 0), None);
20/// ```
21#[inline]
22pub fn county_by_ids(state_id: u8, county_id: u16) -> Option<&'static str> {
23 let counties = data::COUNTIES.get(state_id.checked_sub(1)? as usize).copied()??;
24 counties
25 .get(county_id as usize)
26 .copied()
27 .flatten()
28}
29
30/// ```
31/// use fips_codes::state_and_county_by_ids;
32/// assert_eq!(state_and_county_by_ids(18, 137), Some(("Indiana", "Ripley County")));
33/// assert_eq!(state_and_county_by_ids(18, 138), None);
34/// assert_eq!(state_and_county_by_ids(255, 1000), None);
35/// ```
36#[inline]
37pub fn state_and_county_by_ids(state_id: u8, county_id: u16) -> Option<(&'static str, &'static str)> {
38 let counties = data::COUNTIES.get(state_id.checked_sub(1)? as usize).copied()??;
39 let state = counties.get(0).copied().flatten()?;
40 let county = counties
41 .get(county_id as usize)
42 .copied()
43 .flatten()?;
44 Some((state, county))
45}
46
47/// Does not distinguish between "invalid" strings and simply incorrect ones; either way, you will only get None back.
48/// ```
49/// use fips_codes::county_by_fips_id;
50/// assert_eq!(county_by_fips_id("01000"), Some("Alabama"));
51/// assert_eq!(county_by_fips_id("01002"), None);
52/// assert_eq!(county_by_fips_id("01999"), None);
53/// assert_eq!(county_by_fips_id("001000"), None);
54/// assert_eq!(county_by_fips_id("abcde"), None);
55/// ```
56#[inline]
57pub fn county_by_fips_id(id: &str) -> Option<&'static str> {
58 if(id.len() != 5) {
59 return None;
60 }
61
62 county_by_ids(id.get(0..=1)?.parse().ok()?, id.get(2..=4)?.parse().ok()?)
63}
64
65/// Does not distinguish between "invalid" strings and simply incorrect ones; either way, you will only get None back.
66/// ```
67/// use fips_codes::state_and_county_by_fips_id;
68/// assert_eq!(state_and_county_by_fips_id("53033"), Some(("Washington", "King County")));
69/// assert_eq!(state_and_county_by_fips_id("53010"), None);
70/// assert_eq!(state_and_county_by_fips_id("530100"), None);
71/// assert_eq!(state_and_county_by_fips_id("53-10"), None);
72/// ```
73#[inline]
74pub fn state_and_county_by_fips_id(id: &str) -> Option<(&'static str, &'static str)> {
75 if(id.len() != 5) {
76 return None;
77 }
78
79 state_and_county_by_ids(id.get(0..=1)?.parse().ok()?, id.get(2..=4)?.parse().ok()?)
80}
81
82/// ```
83/// use std::collections::HashMap;
84/// use fips_codes::counties_by_state_fips_id;
85/// let counties = counties_by_state_fips_id(46).unwrap();
86/// let map = counties.collect::<HashMap<_, _>>();
87/// assert_eq!(map.get(&0), Some(&"South Dakota"));
88/// assert_eq!(map.get(&2), None);
89/// assert_eq!(map.get(&7), Some(&"Bennett County"));
90/// ```
91#[inline]
92pub fn counties_by_state_fips_id(state_id: u8) -> Option<impl Iterator<Item = (u16, &'static str)>> {
93 let result = data::COUNTIES
94 .get(state_id.checked_sub(1)? as usize)
95 .copied()??
96 .iter()
97 .copied()
98 .enumerate()
99 .filter_map(|(i, county)| Some((i as u16, county?)));
100 Some(result)
101}
102