Crates.io | jpostcode_rs |
lib.rs | jpostcode_rs |
version | |
source | src |
created_at | 2024-12-02 16:41:15.061483 |
updated_at | 2024-12-03 10:12:32.12117 |
description | Japanese postal code lookup library in Rust, powered by jpostcode-data |
homepage | |
repository | https://github.com/nwiizo/jpostcode_rs |
max_upload_size | |
id | 1468897 |
Cargo.toml error: | TOML parse error at line 25, column 1 | 25 | 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 |
Japanese postal code lookup library in Rust ๐พ
use jpostcode_rs::{lookup_address, lookup_addresses, search_by_address, ToJson};
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Lookup addresses for a postal code ๐
let addresses = lookup_address("0280052")?;
for addr in addresses {
println!("{}", addr.formatted()); // "ใ0280052 ๅฒฉๆ็ไน
ๆ
ๅธๆฌ็บ"
println!("{}", addr.formatted_with_kana()); // With kana reading
}
// Prefix-based lookup (returns all matching addresses) ๐๏ธ
let prefix_matches = lookup_addresses("028")?;
for addr in prefix_matches {
println!("{}", addr.formatted());
}
// Search by address text (supports both Japanese and kana) ๐
let results = search_by_address("ๆญๅน");
for addr in results {
println!("Found: {}", addr.full_address());
}
// JSON serialization ๐
let addresses = lookup_address("0280052")?;
let json = addresses.to_json()?;
println!("{}", json);
// Available address fields
if let Some(addr) = addresses.first() {
println!("Postcode: {}", addr.postcode);
println!("Prefecture: {} ({})", addr.prefecture, addr.prefecture_kana);
println!("City: {} ({})", addr.city, addr.city_kana);
println!("Town: {} ({})", addr.town, addr.town_kana);
}
Ok(())
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let addresses = lookup_address("0280052")?;
if let Some(addr) = addresses.first() {
// Basic format
println!("{}", addr.formatted());
// Output: ใ0280052 ๅฒฉๆ็ไน
ๆ
ๅธ็บ
// Format with kana
println!("{}", addr.formatted_with_kana());
// Output:
// ใ0280052
// ๅฒฉๆ็ไน
ๆ
ๅธๆฌ็บ
// ใคใฏใใฑใณใฏใธใทใใณใใงใฆ
// Individual components
println!("{}", addr.full_address()); // ๅฒฉๆ็ไน
ๆ
ๅธๆฌ็บ
println!("{}", addr.full_address_kana()); // ใคใฏใใฑใณใฏใธใทใใณใใงใฆ
}
Ok(())
}
The library provides two types of errors:
JPostError::InvalidFormat
- When postal code format is invalidJPostError::NotFound
- When no addresses are found for the given postal codefn main() {
match lookup_address("invalid") {
Ok(addresses) => {
for addr in addresses {
println!("{}", addr.formatted());
}
}
Err(JPostError::InvalidFormat) => {
eprintln!("Invalid postal code format");
}
Err(JPostError::NotFound) => {
eprintln!("No addresses found");
}
}
}
Data from jpostcode-data ๐๏ธ
MIT License