#lookup #address #postal #japan #zipcode

jpostcode_rs

Japanese postal code lookup library in Rust, powered by jpostcode-data

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

Download history

91 downloads per month

MIT license

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 invalid
  • JPostError::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

Dependencies