#![deny(
rustdoc::broken_intra_doc_links,
rustdoc::invalid_rust_codeblocks,
missing_docs,
rustdoc::missing_crate_level_docs
)]
mod errors;
mod formats;
pub mod prelude;
mod search;
mod status;
mod types;
use reqwest::Client;
use url::Url;
use crate::{errors::Result, search::SearchQueryBuilder, status::StatusFormat};
static USERAGENT: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"));
#[derive(Debug, Clone)]
pub struct Nominatim {
client: Client,
base_url: Url,
email: String,
}
impl Nominatim {
pub fn new(base_url: Url, email: impl ToString) -> Result<Self> {
Ok(Self {
client: Client::builder().user_agent(USERAGENT).build()?,
base_url,
email: email.to_string(),
})
}
pub fn parse(base_url: &str, email: impl ToString) -> Result<Self> {
Self::new(base_url.parse()?, email)
}
fn search_url(&self) -> Result<Url> {
Ok(self.base_url.join("search")?)
}
pub fn search<Q>(&self, query: Q) -> SearchQueryBuilder<'_, Q> {
SearchQueryBuilder::new(query, self, &self.email)
}
fn status_url(&self) -> Result<Url> {
Ok(self.base_url.join("status")?)
}
pub async fn status<T: StatusFormat>(&self) -> Result<T> {
T::status(self, &self.email).await
}
}
#[cfg(test)]
mod tests {
use std::env;
use std::sync::LazyLock;
use rust_decimal::dec;
use crate::prelude::*;
static CLIENT: LazyLock<Nominatim> = LazyLock::new(|| {
let base_url_str =
env::var("NOMINATIM_BASE_URL").unwrap_or_else(|_| "http://localhost:8080".into());
let base_url = base_url_str
.as_str()
.parse()
.expect("couldn't parse nominatim base url to Url type");
Nominatim::new(base_url, "test@me.com").expect("couldn't initialize")
});
#[tokio::test]
async fn status_text() -> Result<()> {
let client = &*CLIENT;
let res: String = client.status().await?;
assert_eq!(res, "OK");
Ok(())
}
#[tokio::test]
async fn status_json() -> Result<()> {
let client = &*CLIENT;
let res: StatusJson = client.status().await?;
assert_eq!(res.status(), 0);
assert_eq!(res.message(), "OK");
assert!(res.data_updated().is_some());
assert!(res.software_version().is_some());
assert!(res.database_version().is_some());
Ok(())
}
#[tokio::test]
async fn search_str_json_basic() -> Result<()> {
let client = &*CLIENT;
let res = client.search("cafe").send::<JsonPlaceFormat>().await?;
assert!(!res.is_empty());
Ok(())
}
#[tokio::test]
async fn search_str_json_parametrized() -> Result<()> {
let client = &*CLIENT;
let res = client
.search("cafe")
.limit(5)
.address_details(true)
.extra_tags(true)
.name_details(true)
.entrances(true)
.country_codes([CountryCode::US, CountryCode::GB])
.layer(vec![Layer::Poi])
.exclude_place_ids(vec![123])
.viewbox((
Point::new(dec!(40.92702), dec!(-74.33417)),
Point::new(dec!(40.59833), dec!(-73.47312)),
))
.bounded(true)
.send::<JsonPlaceFormat>()
.await?;
assert!(res.len() <= 5);
assert!(!res.is_empty());
for place in res {
assert!(place.address().is_some(), "{place:?}");
assert!(!place.namedetails().is_empty());
}
Ok(())
}
#[tokio::test]
async fn search_string_json_basic() -> Result<()> {
let client = &*CLIENT;
let res = client
.search(String::from("cafe"))
.send::<JsonPlaceFormat>()
.await?;
assert!(!res.is_empty());
Ok(())
}
#[tokio::test]
async fn search_string_json_parametrized() -> Result<()> {
let client = &*CLIENT;
let res = client
.search(String::from("cafe"))
.limit(5)
.address_details(true)
.extra_tags(true)
.name_details(true)
.entrances(true)
.country_codes([CountryCode::US, CountryCode::GB])
.layer(vec![Layer::Poi])
.exclude_place_ids(vec![123])
.viewbox((
Point::new(dec!(40.92702), dec!(-74.33417)),
Point::new(dec!(40.59833), dec!(-73.47312)),
))
.bounded(true)
.send::<JsonPlaceFormat>()
.await?;
assert!(res.len() <= 5);
assert!(!res.is_empty());
for place in res {
assert!(place.address().is_some(), "{place:?}");
assert!(!place.namedetails().is_empty());
}
Ok(())
}
#[tokio::test]
async fn search_structured_json_basic() -> Result<()> {
let client = &*CLIENT;
let res = client
.search(
StructuredQuery::default()
.amenity("grand central")
.street("east 42nd")
.city("New york")
.county("new york")
.state("ny")
.country(CountryCode::US)
.postalcode(10017),
)
.send::<JsonPlaceFormat>()
.await?;
assert!(!res.is_empty());
Ok(())
}
#[tokio::test]
async fn search_structured_json_parameterized() -> Result<()> {
let client = &*CLIENT;
let res = client
.search(
StructuredQuery::default()
.amenity("grand central")
.street("east 42nd")
.city("New york")
.county("new york")
.state("ny")
.country(CountryCode::US)
.postalcode(10017),
)
.limit(1)
.address_details(true)
.extra_tags(true)
.name_details(true)
.entrances(true)
.country_codes([CountryCode::US, CountryCode::GB])
.layer(vec![Layer::Poi])
.exclude_place_ids(vec![123])
.viewbox((
Point::new(dec!(40.92702), dec!(-74.33417)),
Point::new(dec!(40.59833), dec!(-73.47312)),
))
.bounded(true)
.send::<JsonPlaceFormat>()
.await?;
assert_eq!(res.len(), 1);
for place in res {
assert!(place.address().is_some(), "{place:?}");
assert!(!place.extratags().is_empty());
assert!(!place.namedetails().is_empty());
}
Ok(())
}
#[tokio::test]
async fn search_string_jsonv2_basic() -> Result<()> {
let client = &*CLIENT;
let res = client
.search(String::from("cafe"))
.send::<JsonV2PlaceFormat>()
.await?;
assert!(!res.is_empty());
Ok(())
}
#[tokio::test]
async fn search_string_jsonv2_parametrized() -> Result<()> {
let client = &*CLIENT;
let res = client
.search(String::from("cafe"))
.limit(5)
.address_details(true)
.extra_tags(true)
.name_details(true)
.entrances(true)
.country_codes([CountryCode::US, CountryCode::GB])
.layer(vec![Layer::Poi])
.exclude_place_ids(vec![123])
.viewbox((
Point::new(dec!(40.92702), dec!(-74.33417)),
Point::new(dec!(40.59833), dec!(-73.47312)),
))
.bounded(true)
.send::<JsonV2PlaceFormat>()
.await?;
assert!(res.len() <= 5);
assert!(!res.is_empty());
for place in res {
assert!(place.address().is_some(), "{place:?}");
assert!(!place.namedetails().is_empty());
}
Ok(())
}
#[tokio::test]
async fn search_structured_jsonv2_basic() -> Result<()> {
let client = &*CLIENT;
let res = client
.search(
StructuredQuery::default()
.amenity("grand central")
.street("east 42nd")
.city("New york")
.county("new york")
.state("ny")
.country(CountryCode::US)
.postalcode(10017),
)
.send::<JsonV2PlaceFormat>()
.await?;
assert!(!res.is_empty());
Ok(())
}
#[tokio::test]
async fn search_structured_jsonv2_parameterized() -> Result<()> {
let client = &*CLIENT;
let res = client
.search(
StructuredQuery::default()
.amenity("grand central")
.street("east 42nd")
.city("New york")
.county("new york")
.state("ny")
.country(CountryCode::US)
.postalcode(10017),
)
.limit(1)
.address_details(true)
.extra_tags(true)
.name_details(true)
.entrances(true)
.country_codes([CountryCode::US, CountryCode::GB])
.layer(vec![Layer::Poi])
.exclude_place_ids(vec![123])
.viewbox((
Point::new(dec!(40.92702), dec!(-74.33417)),
Point::new(dec!(40.59833), dec!(-73.47312)),
))
.bounded(true)
.send::<JsonV2PlaceFormat>()
.await?;
assert_eq!(res.len(), 1);
for place in res {
assert!(place.address().is_some(), "{place:?}");
assert!(!place.extratags().is_empty());
assert!(!place.namedetails().is_empty());
}
Ok(())
}
}