#![allow(clippy::ref_option, reason = "for the getset crate")]
use crate::places_new::Place;
#[derive(
// std
Clone,
Debug,
Default,
// serde
serde::Deserialize,
serde::Serialize,
// getset
getset::Getters,
getset::MutGetters,
getset::Setters,
)]
#[serde(rename_all = "camelCase")]
pub struct Response {
#[serde(default)]
#[getset(get = "pub")]
places: Vec<Place>,
#[serde(default)]
#[getset(get = "pub")]
error: Option<crate::places_new::GoogleApiError>,
}
impl Response {
#[must_use]
pub const fn new(places: Vec<Place>) -> Self {
Self {
places,
error: None,
}
}
#[must_use]
pub const fn empty() -> Self {
Self {
places: Vec::new(),
error: None,
}
}
#[must_use]
pub fn len(&self) -> usize {
self.places.len()
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.places.is_empty()
}
#[must_use]
pub const fn has_error(&self) -> bool {
self.error.is_some()
}
}
impl std::ops::Deref for Response {
type Target = Vec<Place>;
fn deref(&self) -> &Self::Target {
&self.places
}
}
impl std::ops::DerefMut for Response {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.places
}
}
impl std::ops::Index<usize> for Response {
type Output = Place;
fn index(&self, index: usize) -> &Self::Output {
&self.places[index]
}
}
impl std::ops::IndexMut<usize> for Response {
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
&mut self.places[index]
}
}
impl AsRef<[Place]> for Response {
fn as_ref(&self) -> &[Place] {
&self.places
}
}
impl AsMut<[Place]> for Response {
fn as_mut(&mut self) -> &mut [Place] {
&mut self.places
}
}
impl IntoIterator for Response {
type Item = Place;
type IntoIter = std::vec::IntoIter<Place>;
fn into_iter(self) -> Self::IntoIter {
self.places.into_iter()
}
}
impl<'a> IntoIterator for &'a Response {
type Item = &'a Place;
type IntoIter = std::slice::Iter<'a, Place>;
fn into_iter(self) -> Self::IntoIter {
self.places.iter()
}
}
impl<'a> IntoIterator for &'a mut Response {
type Item = &'a mut Place;
type IntoIter = std::slice::IterMut<'a, Place>;
fn into_iter(self) -> Self::IntoIter {
self.places.iter_mut()
}
}
impl Response {
pub fn iter(&self) -> std::slice::Iter<'_, Place> {
self.places.iter()
}
pub fn iter_mut(&mut self) -> std::slice::IterMut<'_, Place> {
self.places.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),
}
}
}