usa
A Rust library providing typed enums and utilities for working with United States regions, including states, territories, and the federal district. The crate offers:
- Strongly typed representations of U.S. states, territories, and the District of Columbia.
- Simple conversion from names or abbreviations (e.g.,
"CA","California","Puerto Rico","DC") into typed enums. - Easy retrieval of official two-letter USPS abbreviations for states, territories, and the District of Columbia.
- Optional serialization and deserialization behavior via Serde, allowing you to choose between full names or abbreviations through a feature flag.
This crate is useful for applications dealing with geospatial data, address normalization, or any domain where validated U.S. region inputs are required.
Features
serde_abbreviation(requiresserde): When enabled, serialization and deserialization use the official two-letter abbreviation instead of the full name.
You can enable these features in your Cargo.toml as follows:
[]
= "0.1.0"
or, to use abbreviations for serialization:
[]
= { = "0.1.0", = ["serde_abbreviation"] }
Examples
Parsing Regions from Strings
You can parse states, territories, and the federal district from various string representations:
use TryFrom;
use ;
assert_eq!;
assert_eq!;
assert_eq!;
// Case-insensitivity and multiple recognized forms are supported:
assert_eq!;
assert_eq!;
assert_eq!;
Obtaining Abbreviations
Every region implements the Abbreviation trait to easily retrieve its official two-letter code:
use ;
let california = UnitedState;
assert_eq!;
let puerto_rico = USTerritory;
assert_eq!;
let dc = USFederalDistrict;
assert_eq!;
Listing All Regions
You can list all states, territories, or regions at once:
use ;
// All states:
let all_states = all_states;
assert_eq!;
// All territories:
let all_territories = all_territories;
assert_eq!;
// All regions (states + territories + DC):
let all_regions = all_regions;
assert_eq!; // 50 states + 5 territories + 1 federal district
Serialization and Deserialization with Serde
By default (with serde but without serde_abbreviation), serialization uses the region's full name. With serde_abbreviation, it uses the abbreviation. For example, with serde_abbreviation enabled:
use ;
use serde_json;
let state = California;
let json = to_string.unwrap;
assert_eq!; // With `serde_abbreviation`, abbreviations are used.
// Similarly, for territories and USRegion:
let region = USTerritory;
let json_region = to_string.unwrap;
assert_eq!;
When deserializing, the crate tries to match either a name or abbreviation. For example:
let state: UnitedState = from_str.unwrap;
assert_eq!;
let territory: USTerritory = from_str.unwrap;
assert_eq!;
(Deserialization rules depend on the serde_abbreviation feature. Without it, it expects names; with it, it expects abbreviations—but since it supports multiple variants, it will often match regardless.)
Error Handling
If parsing fails, a BadInput error is returned, indicating that the provided string does not correspond to any known region.
use TryFrom;
use ;
let result = try_from;
assert!;
if let Err = result
Contributing
Contributions are welcome! Please open an issue or submit a pull request if you have ideas for improvements or additional features.
License
This project is licensed under the MIT license. See LICENSE for details.