1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
use std::{
collections::HashMap,
ffi::{CStr, CString},
ops::DerefMut,
};
use init::{
initialize_libpostal, initialize_libpostal_language_classifier,
initialize_libpostal_parser,
};
use libpostal_sys::{
libpostal_address_parser_response_destroy, libpostal_expand_address,
libpostal_expansion_array_destroy, libpostal_get_address_parser_default_options,
libpostal_get_default_options, libpostal_parse_address, size_t, GLOBAL_LOCK,
};
mod errors;
mod init;
mod probe;
pub use self::errors::Error;
pub type Result<T, E = Error> = std::result::Result<T, E>;
#[derive(Debug, Default)]
pub struct ParseAddressOptions {}
pub fn parse_address(
addr: &str,
_opt: &ParseAddressOptions,
) -> Result<HashMap<String, String>> {
let mut initialization_state = GLOBAL_LOCK.lock().expect("mutex poisoned");
unsafe { initialize_libpostal(initialization_state.deref_mut()) }?;
unsafe { initialize_libpostal_parser(initialization_state.deref_mut()) }?;
let addr = CString::new(addr).map_err(|_| Error::NullByteInString {
string: addr.to_owned(),
})?;
let parse_options = unsafe { libpostal_get_address_parser_default_options() };
let parsed =
unsafe { libpostal_parse_address(addr.as_ptr() as *mut _, parse_options) };
let num_components = unsafe { (*parsed).num_components } as usize;
let mut result = HashMap::with_capacity(num_components);
for i in 0..num_components {
let (label, component) = unsafe {
(
CStr::from_ptr(*(*parsed).labels.add(i))
.to_str()
.expect("label contained invalid UTF-8"),
CStr::from_ptr(*(*parsed).components.add(i))
.to_str()
.expect("component contained invalid UTF-8"),
)
};
result.insert(label.to_owned(), component.to_owned());
}
unsafe { libpostal_address_parser_response_destroy(parsed) };
Ok(result)
}
#[derive(Debug, Default)]
pub struct ExpandAddressOptions {}
pub fn expand_address(addr: &str, _opt: &ExpandAddressOptions) -> Result<Vec<String>> {
let mut initialization_state = GLOBAL_LOCK.lock().expect("mutex poisoned");
unsafe { initialize_libpostal(initialization_state.deref_mut()) }?;
unsafe {
initialize_libpostal_language_classifier(initialization_state.deref_mut())
}?;
let addr = CString::new(addr).map_err(|_| Error::NullByteInString {
string: addr.to_owned(),
})?;
let expand_options = unsafe { libpostal_get_default_options() };
let mut num_expansions: size_t = 0;
let expansions = unsafe {
libpostal_expand_address(
addr.as_ptr() as *mut _,
expand_options,
&mut num_expansions,
)
};
let mut result = Vec::with_capacity(num_expansions as usize);
for i in 0..num_expansions {
let expansion = unsafe {
CStr::from_ptr(*expansions.offset(i as isize))
.to_str()
.expect("expansion contained invalid UTF-8")
};
result.push(expansion.to_owned());
}
unsafe { libpostal_expansion_array_destroy(expansions, num_expansions) };
Ok(result)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[ignore]
fn parse_address_returns_components() {
let addr = "781 Franklin Ave Crown Heights Brooklyn NYC NY 11216 USA";
let opt = ParseAddressOptions::default();
let parsed = parse_address(addr, &opt).unwrap();
assert_eq!(parsed.get("state"), Some(&"ny".to_owned()));
}
#[test]
#[ignore]
fn expand_address_returns_candidates() {
let addr = "Quatre-vingt-douze Ave des Champs-Élysées";
let opt = ExpandAddressOptions::default();
let expanded = expand_address(addr, &opt).unwrap();
assert!(expanded[0].contains("92"));
}
}