#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
#![allow(improper_ctypes)]
#![cfg_attr(test, allow(deref_nullptr))]
use std::sync::{Arc, Mutex};
use lazy_static::lazy_static;
#[derive(Debug)]
#[non_exhaustive]
pub struct InitializationState {
pub initialized: bool,
pub parser_initialized: bool,
pub language_classifier_initialized: bool,
}
lazy_static! {
pub static ref GLOBAL_LOCK: Arc<Mutex<InitializationState>> = Arc::new(Mutex::new(InitializationState {
initialized: false,
parser_initialized: false,
language_classifier_initialized: false,
}));
}
include!("bindings.rs");
#[cfg(test)]
mod tests {
use std::ffi::{CStr, CString};
use super::*;
#[test]
#[ignore]
fn smoke_test() {
unsafe {
let datadir = CString::new("/usr/local/share/libpostal")
.expect("CString::new failed");
if !libpostal_setup_datadir(datadir.as_ptr() as *mut _) {
panic!("can't set up libpostal");
}
if !libpostal_setup_parser_datadir(datadir.as_ptr() as *mut _) {
panic!("can't set up libpostal parser")
}
if !libpostal_setup_language_classifier_datadir(datadir.as_ptr() as *mut _)
{
panic!("can't set up libpostal classifier")
}
let address = CString::new(
"781 Franklin Ave Crown Heights Brooklyn NYC NY 11216 USA",
)
.expect("CString::new failed");
let parse_options = libpostal_get_address_parser_default_options();
let parsed =
libpostal_parse_address(address.as_ptr() as *mut _, parse_options);
for i in 0..(*parsed).num_components {
let label = CStr::from_ptr(*(*parsed).labels.offset(i as isize))
.to_str()
.expect("label contained invalid UTF-8");
let component =
CStr::from_ptr(*(*parsed).components.offset(i as isize))
.to_str()
.expect("component contained invalid UTF-8");
println!("{}={}", label, component);
}
libpostal_address_parser_response_destroy(parsed);
let street_address =
CString::new("Quatre-vingt-douze Ave des Champs-Élysées")
.expect("CString::new failed");
let normalization_options = libpostal_get_default_options();
let mut num_expansions: size_t = 0;
let expansions = libpostal_expand_address(
street_address.as_ptr() as *mut _,
normalization_options,
&mut num_expansions,
);
for i in 0..num_expansions {
let expansion = CStr::from_ptr(*expansions.offset(i as isize))
.to_str()
.expect("expansion contained invalid UTF-8");
println!("expansion {}: {}", i, expansion);
}
libpostal_expansion_array_destroy(expansions, num_expansions);
libpostal_teardown();
libpostal_teardown_parser();
libpostal_teardown_language_classifier();
}
}
}