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