use crate::places_new::{AuthorAttribution, PhotoInfo, Place};
use url::Url;
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct PhotoRequest {
pub name: String,
pub width_px: Option<u32>,
pub height_px: Option<u32>,
pub author_attributions: Vec<AuthorAttribution>,
pub flag_content_uri: Option<Url>,
pub google_maps_uri: Option<Url>,
}
impl PhotoRequest {
pub fn new(name: impl Into<String>) -> Self {
Self::from(name.into())
}
}
impl From<String> for PhotoRequest {
fn from(name: String) -> Self {
Self {
name,
width_px: None,
height_px: None,
author_attributions: Vec::new(),
flag_content_uri: None,
google_maps_uri: None,
}
}
}
impl From<&str> for PhotoRequest {
fn from(name: &str) -> Self {
Self::from(name.to_string())
}
}
impl From<&String> for PhotoRequest {
fn from(name: &String) -> Self {
Self::from(name.clone())
}
}
impl<'a> From<std::borrow::Cow<'a, str>> for PhotoRequest {
fn from(name: std::borrow::Cow<'a, str>) -> Self {
Self::from(name.to_string())
}
}
impl From<Box<str>> for PhotoRequest {
fn from(name: Box<str>) -> Self {
Self::from(name.to_string())
}
}
impl std::fmt::Display for PhotoRequest {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "PhotoRequest {{ name: {}", self.name)?;
if let (Some(w), Some(h)) = (self.width_px, self.height_px) {
write!(f, ", dimensions: {w}×{h}")?;
}
if !self.author_attributions.is_empty() {
write!(f, ", attributions: {}", self.author_attributions.len())?;
}
write!(f, " }}")
}
}
impl From<PhotoInfo> for PhotoRequest {
fn from(photo_info: PhotoInfo) -> Self {
Self {
name: photo_info.name,
width_px: Some(photo_info.width_px),
height_px: Some(photo_info.height_px),
author_attributions: photo_info.author_attributions,
flag_content_uri: photo_info.flag_content_uri,
google_maps_uri: photo_info.google_maps_uri,
}
}
}
impl From<&PhotoInfo> for PhotoRequest {
fn from(photo_info: &PhotoInfo) -> Self {
Self {
name: photo_info.name.clone(),
width_px: Some(photo_info.width_px),
height_px: Some(photo_info.height_px),
author_attributions: photo_info.author_attributions.clone(),
flag_content_uri: photo_info.flag_content_uri.clone(),
google_maps_uri: photo_info.google_maps_uri.clone(),
}
}
}
impl TryFrom<&Place> for PhotoRequest {
type Error = crate::places_new::place_photos::Error;
fn try_from(place: &Place) -> Result<Self, Self::Error> {
place
.photos()
.first()
.map(Self::from)
.ok_or_else(|| crate::places_new::place_photos::Error::PlaceHasNoPhotos {
place_id: place.id().clone(),
span: place
.id()
.as_ref()
.map_or_else(|| (0..0).into(), |place_id| (0..place_id.len()).into())
})
}
}
impl TryFrom<Place> for PhotoRequest {
type Error = crate::places_new::place_photos::Error;
fn try_from(place: Place) -> Result<Self, Self::Error> {
place
.photos()
.first()
.map(Self::from)
.ok_or_else(|| crate::places_new::place_photos::Error::PlaceHasNoPhotos {
place_id: place.id().clone(),
span: place
.id()
.as_ref()
.map_or_else(|| (0..0).into(), |place_id| (0..place_id.len()).into())
})
}
}