fips-codes 0.1.1

Static, zero-dependency mapping of FIPS IDs to U.S. state and county names
Documentation
#![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)
}