use crate::places_new::autocomplete::response::{PlacePrediction, QueryPrediction};
#[derive(Clone, Debug, Eq, PartialEq, serde::Deserialize, serde::Serialize)]
#[serde(rename_all = "camelCase")]
pub enum Suggestion {
PlacePrediction(PlacePrediction),
QueryPrediction(QueryPrediction),
}
impl Suggestion {
#[must_use]
pub const fn is_place(&self) -> bool {
matches!(self, Self::PlacePrediction(_))
}
#[must_use]
pub const fn is_query(&self) -> bool {
matches!(self, Self::QueryPrediction(_))
}
#[must_use]
pub const fn as_place(&self) -> Option<&PlacePrediction> {
match self {
Self::PlacePrediction(place) => Some(place),
Self::QueryPrediction(_) => None,
}
}
#[must_use]
pub const fn as_query(&self) -> Option<&QueryPrediction> {
match self {
Self::PlacePrediction(_) => None,
Self::QueryPrediction(query) => Some(query),
}
}
#[must_use]
pub fn text(&self) -> &str {
match self {
Self::PlacePrediction(place) => place.text().text(),
Self::QueryPrediction(query) => query.text().text(),
}
}
#[must_use]
pub fn to_html(&self, tag: &str) -> String {
match self {
Self::PlacePrediction(place) => place.to_html(tag),
Self::QueryPrediction(query) => query.to_html(tag),
}
}
#[must_use]
pub fn to_html_structured(&self, main_tag: &str, secondary_tag: &str) -> String {
match self {
Self::PlacePrediction(place) => place.to_html_structured(main_tag, secondary_tag),
Self::QueryPrediction(query) => query.to_html_structured(main_tag, secondary_tag),
}
}
#[must_use]
pub fn place_id(&self) -> Option<&str> {
match self {
Self::PlacePrediction(place) => Some(place.place_id.as_str()),
Self::QueryPrediction(_query) => None,
}
}
#[must_use]
pub fn format_with<F>(&self, formatter: F) -> String
where
F: FnMut(&str, bool) -> String,
{
match self {
Self::PlacePrediction(place) => place.format_with(formatter),
Self::QueryPrediction(query) => query.format_with(formatter),
}
}
#[must_use]
pub fn format_with_structured<F, G>(&self, main_formatter: F, secondary_formatter: G) -> String
where
F: FnMut(&str, bool) -> String,
G: FnMut(&str, bool) -> String,
{
match self {
Self::PlacePrediction(place) => {
place.format_with_structured(main_formatter, secondary_formatter)
}
Self::QueryPrediction(query) => {
query.format_with_structured(main_formatter, secondary_formatter)
}
}
}
}
impl std::fmt::Display for Suggestion {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.text())
}
}
impl From<PlacePrediction> for Suggestion {
fn from(place: PlacePrediction) -> Self {
Self::PlacePrediction(place)
}
}
impl From<QueryPrediction> for Suggestion {
fn from(query: QueryPrediction) -> Self {
Self::QueryPrediction(query)
}
}