#address #addresses #street #parser #parse

us_address_parser

A Rust crate that parses U.S. street addresses.

5 releases

new 0.1.4 Sep 12, 2024
0.1.3 Sep 12, 2024
0.1.2 Sep 12, 2024
0.1.1 Sep 12, 2024
0.1.0 Sep 11, 2024

#447 in Template engine

Download history 332/week @ 2024-09-10

332 downloads per month

MIT license

49KB
1.5K SLoC

US Address Parser

A Rust Crate that parses United States street addresses.

How To Use

To get started, simply import the AddressParsing trait to your file, and add the parse_addr() method to the String or &str you want to parse. It will return the Address struct, which contains; the street number, street name, street type, unit type, and unit number. Each of these fields are Option<String>.

use us_address_parser::AddressParsing;

fn main() {
    let addresses: Vec<&str> = vec![
        "123 Main Ave",
        "456 Maple Blvd Apt 122",
        "789 3rd St Lot B"
    ];

    for addr in addresses {
        println!("{}", &addr);

        let address = addr.parse_addr();
        
        println!("Street_no:{:?}, dir:{:?}, street_name:{:?}, street_type:{:?}, unit_type:{:?}, unit_no:{:?}\n==========", 
            address.street_no, address.direction, address.street_name,
            address.street_type, address.unit_type, address.unit_no
        );
    }
}
Result:
123 Main Ave
Street_no:Some("123"), dir:None, street_name:Some("MAIN"), street_type:Some("AVE"), unit_type:None, unit_no:None
==========
456 Maple Blvd Apt 122
Street_no:Some("456"), dir:None, street_name:Some("MAPLE"), street_type:Some("BLVD"), unit_type:Some("APT"), unit_no:Some("122")
==========
789 3rd St Lot B
Street_no:Some("789"), dir:None, street_name:Some("3RD"), street_type:Some("ST"), unit_type:Some("LOT"), unit_no:Some("B")
==========

Implementing A Custom Type

The AddressParsing trait is already implemented for strings,

struct CustomerInfo {
    id: usize,
    customer_name: String,
    customer_address: String
}

impl AddressParsing for CustomerInfo {
    fn parse_addr(&self) -> Address {
        let address = self.customer_address;
        us_address_parser::string_to_address(address)
    }
}

The string_to_address() function does what it's name implies, converts a String to an Address.

Dependencies

~2.2–3MB
~54K SLoC