use crate::places_new::autocomplete::response::{FormattableText, StructuredFormat};
#[derive(
// std
Clone,
Debug,
Eq,
PartialEq,
// serde
serde::Deserialize,
serde::Serialize,
// getset
getset::Getters,
getset::MutGetters,
getset::Setters,
)]
#[serde(rename_all = "camelCase")]
pub struct QueryPrediction {
#[getset(set = "pub", get_mut = "pub")]
pub text: FormattableText,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[getset(set = "pub", get_mut = "pub")]
pub structured_format: Option<StructuredFormat>,
}
impl QueryPrediction {
#[must_use]
pub const fn new(text: FormattableText, structured_format: Option<StructuredFormat>) -> Self {
Self {
text,
structured_format,
}
}
#[must_use]
pub const fn new_unstructured(text: FormattableText) -> Self {
Self {
text,
structured_format: None,
}
}
#[must_use]
pub const fn text(&self) -> &FormattableText {
&self.text
}
#[must_use]
pub const fn structured_format(&self) -> Option<&StructuredFormat> {
self.structured_format.as_ref()
}
#[must_use]
pub const fn has_structured_format(&self) -> bool {
self.structured_format.is_some()
}
#[must_use]
pub fn query_string(&self) -> &str {
self.text.text()
}
#[must_use]
pub fn to_html(&self, tag: &str) -> String {
self.text.to_html(tag)
}
#[must_use]
pub fn to_html_structured(&self, main_tag: &str, secondary_tag: &str) -> String {
self.structured_format
.as_ref()
.map_or_else(
|| self.text.to_html(main_tag),
|format| format.combined_html(main_tag, secondary_tag)
)
}
#[must_use]
pub fn format_with<F>(&self, formatter: F) -> String
where
F: FnMut(&str, bool) -> String,
{
self.text.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.structured_format {
Some(format) => format.combined_format_with(main_formatter, secondary_formatter),
None => self.text.format_with(main_formatter),
}
}
}
impl std::fmt::Display for QueryPrediction {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.text)
}
}
impl From<String> for QueryPrediction {
fn from(text: String) -> Self {
Self::new_unstructured(FormattableText::from(text))
}
}
impl From<&str> for QueryPrediction {
fn from(text: &str) -> Self {
Self::new_unstructured(FormattableText::from(text))
}
}
impl From<FormattableText> for QueryPrediction {
fn from(text: FormattableText) -> Self {
Self::new_unstructured(text)
}
}