Struct rustpostal::expand::NormalizeOptions
source · [−]pub struct NormalizeOptions { /* private fields */ }
Expand description
Normalization options.
Options are required to expand a postal address to its normalized variations. They are created by defining optionally language-codes for normalization (e.g. ‘en’), and then gradually adding more options.
A expand
method is implemented to use the options for normalizing an address.
Examples
use std::ffi::NulError;
use rustpostal::expand::{AddressComponents, StringOptions, NormalizeOptions};
fn main() -> Result<(), NulError> {
let languages = ["en", "gb"];
let mut options = NormalizeOptions::new(Some(languages.iter()))?;
assert_eq!(options.languages().unwrap().collect::<Vec<&str>>(), Vec::from(languages));
let s_options = StringOptions::TRANSLITERATE | StringOptions::LOWERCASE;
let components = AddressComponents::NAME | AddressComponents::LEVEL;
options.add_string_option(s_options);
assert!(options.string_options().as_ref().unwrap().contains(s_options));
options.add_address_component(components);
assert!(options.address_components().as_ref().unwrap().contains(components));
Ok(())
}
Implementations
sourceimpl NormalizeOptions
impl NormalizeOptions
sourcepub fn new<'a, 'b, T>(
languages: Option<T>
) -> Result<NormalizeOptions, NulError> where
'a: 'b,
T: Iterator<Item = &'b &'a str>,
pub fn new<'a, 'b, T>(
languages: Option<T>
) -> Result<NormalizeOptions, NulError> where
'a: 'b,
T: Iterator<Item = &'b &'a str>,
Create new instance with default options.
languages
override the respective option field, if given.
sourcepub fn add_string_option(&mut self, option: StringOptions)
pub fn add_string_option(&mut self, option: StringOptions)
Add string option.
sourcepub fn add_address_component(&mut self, component: AddressComponents)
pub fn add_address_component(&mut self, component: AddressComponents)
Add address component option.
sourcepub fn languages(&self) -> Option<impl Iterator<Item = &str>>
pub fn languages(&self) -> Option<impl Iterator<Item = &str>>
Return current languages.
Examples
use std::ffi::NulError;
use rustpostal::expand::NormalizeOptions;
fn main() -> Result<(), NulError> {
let options = NormalizeOptions::default();
assert!(options.languages().is_none());
let languages = ["en", "gb"];
let options = NormalizeOptions::new(Some(languages.iter()))?;
assert_eq!(options.languages().unwrap().collect::<Vec<&str>>(), Vec::from(languages));
Ok(())
}
sourcepub fn address_components(&self) -> Option<&AddressComponents>
pub fn address_components(&self) -> Option<&AddressComponents>
Return current address components.
use rustpostal::expand::{NormalizeOptions, AddressComponents};
let mut options = NormalizeOptions::default();
assert_eq!(options.address_components(), None);
options.add_address_component(AddressComponents::NAME);
assert_eq!(options.address_components(), Some(&AddressComponents::NAME));
sourcepub fn string_options(&self) -> Option<&StringOptions>
pub fn string_options(&self) -> Option<&StringOptions>
Return current string options.
use rustpostal::expand::{NormalizeOptions, StringOptions};
let mut options = NormalizeOptions::default();
assert_eq!(options.string_options(), None);
options.add_string_option(StringOptions::TRANSLITERATE);
assert_eq!(options.string_options(), Some(&StringOptions::TRANSLITERATE));
sourcepub fn expand(&mut self, address: &str) -> Result<NormalizedAddress, NulError>
pub fn expand(&mut self, address: &str) -> Result<NormalizedAddress, NulError>
Expand address into normalized variations using libpostal.
Examples
use std::ffi::NulError;
use rustpostal::LibModules;
use rustpostal::expand::NormalizeOptions;
use rustpostal::error::RuntimeError;
fn main() -> Result<(), RuntimeError> {
let postal_module = LibModules::Expand;
postal_module.setup()?;
let mut options = NormalizeOptions::default();
let address = "St Johns Centre, Rope Walk, Bedford, Bedfordshire, MK42 0XE, United Kingdom";
let expanded = options.expand(address)?;
for variation in &expanded {
assert!(variation.ends_with("kingdom"))
}
Ok(())
}
Errors
The method will return an error if the supplied address
contains an internal null byte. The error is represented by
NulError
.
Trait Implementations
sourceimpl Clone for NormalizeOptions
impl Clone for NormalizeOptions
sourcefn clone(&self) -> NormalizeOptions
fn clone(&self) -> NormalizeOptions
Returns a copy of the value. Read more
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
sourceimpl Debug for NormalizeOptions
impl Debug for NormalizeOptions
sourceimpl Default for NormalizeOptions
impl Default for NormalizeOptions
sourcefn default() -> NormalizeOptions
fn default() -> NormalizeOptions
Returns the “default value” for a type. Read more
sourceimpl Hash for NormalizeOptions
impl Hash for NormalizeOptions
sourceimpl Ord for NormalizeOptions
impl Ord for NormalizeOptions
sourceimpl PartialEq<NormalizeOptions> for NormalizeOptions
impl PartialEq<NormalizeOptions> for NormalizeOptions
sourcefn eq(&self, other: &NormalizeOptions) -> bool
fn eq(&self, other: &NormalizeOptions) -> bool
This method tests for self
and other
values to be equal, and is used
by ==
. Read more
sourcefn ne(&self, other: &NormalizeOptions) -> bool
fn ne(&self, other: &NormalizeOptions) -> bool
This method tests for !=
.
sourceimpl PartialOrd<NormalizeOptions> for NormalizeOptions
impl PartialOrd<NormalizeOptions> for NormalizeOptions
sourcefn partial_cmp(&self, other: &NormalizeOptions) -> Option<Ordering>
fn partial_cmp(&self, other: &NormalizeOptions) -> Option<Ordering>
This method returns an ordering between self
and other
values if one exists. Read more
1.0.0 · sourcefn lt(&self, other: &Rhs) -> bool
fn lt(&self, other: &Rhs) -> bool
This method tests less than (for self
and other
) and is used by the <
operator. Read more
1.0.0 · sourcefn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
impl Eq for NormalizeOptions
impl StructuralEq for NormalizeOptions
impl StructuralPartialEq for NormalizeOptions
Auto Trait Implementations
impl RefUnwindSafe for NormalizeOptions
impl Send for NormalizeOptions
impl Sync for NormalizeOptions
impl Unpin for NormalizeOptions
impl UnwindSafe for NormalizeOptions
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more