mod string_range;
pub use crate::places_new::autocomplete::response::string_range::StringRange;
mod formattable_text;
pub use crate::places_new::autocomplete::response::formattable_text::FormattableText;
mod place_prediction;
pub use crate::places_new::autocomplete::response::place_prediction::PlacePrediction;
mod structured_format;
pub use crate::places_new::autocomplete::response::structured_format::StructuredFormat;
mod suggestion;
pub use crate::places_new::autocomplete::response::suggestion::Suggestion;
mod query_prediction;
pub use crate::places_new::autocomplete::response::query_prediction::QueryPrediction;
#[derive(
// std
Clone,
Debug,
Default,
Eq,
PartialEq,
// serde
serde::Deserialize,
serde::Serialize,
// getset
getset::Getters,
getset::MutGetters,
getset::Setters,
)]
pub struct Response {
#[serde(default)]
#[getset(get = "pub")]
pub(crate) suggestions: Vec<Suggestion>,
#[serde(default)]
#[getset(get = "pub")]
error: Option<crate::places_new::GoogleApiError>,
}
impl Response {
#[must_use]
pub const fn new(suggestions: Vec<Suggestion>) -> Self {
Self { suggestions, error: None }
}
#[must_use]
pub fn len(&self) -> usize {
self.suggestions.len()
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.suggestions.is_empty()
}
pub fn places(&self) -> impl Iterator<Item = &PlacePrediction> {
self.suggestions.iter().filter_map(|s| s.as_place())
}
pub fn queries(&self) -> impl Iterator<Item = &QueryPrediction> {
self.suggestions.iter().filter_map(|s| s.as_query())
}
#[must_use]
pub fn place_count(&self) -> usize {
self.suggestions.iter().filter(|s| s.is_place()).count()
}
#[must_use]
pub fn query_count(&self) -> usize {
self.suggestions.iter().filter(|s| s.is_query()).count()
}
#[must_use]
pub fn to_html_all(&self, tag: &str) -> Vec<String> {
self.suggestions
.iter()
.map(|suggestion| suggestion.to_html(tag))
.collect()
}
#[must_use]
pub fn to_html_structured_all(&self, main_tag: &str, secondary_tag: &str) -> Vec<String> {
self.suggestions
.iter()
.map(|suggestion| suggestion.to_html_structured(main_tag, secondary_tag))
.collect()
}
#[must_use]
pub fn format_with_all<F>(&self, mut formatter: F) -> Vec<String>
where
F: FnMut(&str, bool) -> String,
{
self.suggestions
.iter()
.map(|s| s.format_with(&mut formatter))
.collect()
}
}
impl std::ops::Deref for Response {
type Target = Vec<Suggestion>;
fn deref(&self) -> &Self::Target {
&self.suggestions
}
}
impl std::ops::DerefMut for Response {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.suggestions
}
}
impl std::ops::Index<usize> for Response {
type Output = Suggestion;
fn index(&self, index: usize) -> &Self::Output {
&self.suggestions[index]
}
}
impl std::ops::IndexMut<usize> for Response {
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
&mut self.suggestions[index]
}
}
impl AsRef<[Suggestion]> for Response {
fn as_ref(&self) -> &[Suggestion] {
&self.suggestions
}
}
impl AsMut<[Suggestion]> for Response {
fn as_mut(&mut self) -> &mut [Suggestion] {
&mut self.suggestions
}
}
impl IntoIterator for Response {
type Item = Suggestion;
type IntoIter = std::vec::IntoIter<Suggestion>;
fn into_iter(self) -> Self::IntoIter {
self.suggestions.into_iter()
}
}
impl<'a> IntoIterator for &'a Response {
type Item = &'a Suggestion;
type IntoIter = std::slice::Iter<'a, Suggestion>;
fn into_iter(self) -> Self::IntoIter {
self.suggestions.iter()
}
}
impl<'a> IntoIterator for &'a mut Response {
type Item = &'a mut Suggestion;
type IntoIter = std::slice::IterMut<'a, Suggestion>;
fn into_iter(self) -> Self::IntoIter {
self.suggestions.iter_mut()
}
}
impl Response {
pub fn iter(&self) -> std::slice::Iter<'_, Suggestion> {
self.suggestions.iter()
}
pub fn iter_mut(&mut self) -> std::slice::IterMut<'_, Suggestion> {
self.suggestions.iter_mut()
}
}
impl std::convert::From<Response> for Result<Response, crate::Error> {
fn from(response: Response) -> Self {
match response.error {
Some(error) => Err(crate::places_new::Error::from(error).into()),
None => Ok(response),
}
}
}