4 releases
new 0.1.3 | Dec 3, 2024 |
---|---|
0.1.2 | Dec 3, 2024 |
0.1.1 | Dec 2, 2024 |
0.1.0 | Dec 2, 2024 |
#916 in Parser implementations
91 downloads per month
4MB
219 lines
jpostcode_rs ๐ฎ
Japanese postal code lookup library in Rust ๐พ
Features ๐
- Fast postal code lookup with pre-compiled address data โก
- Multiple address support for single postal code ๐๏ธ
- Prefix-based address lookup ๐
- Address search (Japanese and kana) ๐ค
- JSON serialization support ๐
- Multiple address format styles ๐
- No runtime data loading required ๐ฆ
Usage ๐ป
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(())
}
Address Formats ๐
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(())
}
Error Handling ๐จ
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 code
fn 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 Source ๐
Data from jpostcode-data ๐๏ธ
License ๐
MIT License