Crates.io | postal-code |
lib.rs | postal-code |
version | |
source | src |
created_at | 2024-12-11 20:18:04.72824 |
updated_at | 2024-12-11 20:18:04.72824 |
description | A robust, production-grade Rust library for validating international postal codes. |
homepage | |
repository | https://github.com/klebs6/klebs-general |
max_upload_size | |
id | 1480504 |
Cargo.toml error: | TOML parse error at line 18, column 1 | 18 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
postal-code is a robust, production-grade Rust library for validating and manipulating international postal codes. It supports a variety of countries, including the USA, Canada, UK, France, Germany, and Italy, with the flexibility to add more.
This crate offers:
Country
integration from an external country
crate.derive_builder
) for safer, more ergonomic construction of PostalCode
and PostalCodeCollection
instances.PostalCodeConstructionError
type.Key Features:
Country
enum variant, ensuring that once created, it is guaranteed to be valid for that country.PostalCodeConstructionError
) avoid stringly-typed error handling.Usage Example:
use postal_code::PostalCode;
use country::Country;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let us_code = PostalCode::new(Country::USA, "12345")?;
println!("Valid US postal code: {}", us_code.code());
let ca_code = PostalCode::new(Country::Canada, "K1A0B1")?;
println!("Valid Canadian postal code: {}", ca_code.code());
// Attempting an invalid UK code:
let uk_code = PostalCode::new(Country::UK, "SW1A1AAZ");
match uk_code {
Ok(code) => println!("Valid UK code: {}", code.code()),
Err(e) => eprintln!("Invalid postal code: {:?}", e),
}
Ok(())
}
Testing:
Run the test suite with:
cargo test
This will run an extensive battery of tests, including correctness checks for various countries, invalid inputs, and builder patterns.
Contributing:
Contributions are welcome! Please open issues or submit pull requests on the project’s GitHub repository.
License:
postal-code
is licensed under the MIT license. See LICENSE for details.