jpostcode_rs

Crates.iojpostcode_rs
lib.rsjpostcode_rs
version
sourcesrc
created_at2024-12-02 16:41:15.061483
updated_at2024-12-03 10:12:32.12117
descriptionJapanese postal code lookup library in Rust, powered by jpostcode-data
homepage
repositoryhttps://github.com/nwiizo/jpostcode_rs
max_upload_size
id1468897
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`
size0
Shuya Motouchi (nwiizo)

documentation

https://docs.rs/jpostcode_rs

README

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

Commit count: 10

cargo fmt