use std::string::ToString;
use chrono::{NaiveDate, NaiveDateTime};
use crate::{CollectionItemType, GameType, ItemDomain, ItemSubType, ItemType, WishlistPriority};
pub(crate) type QueryParam<'a> = (&'a str, String);
pub(crate) trait IntoQueryParam {
fn into_query_param(self, key: &str) -> QueryParam<'_>;
}
impl IntoQueryParam for bool {
fn into_query_param(self, key: &str) -> QueryParam<'_> {
match self {
true => (key, "1".to_owned()),
false => (key, "0".to_owned()),
}
}
}
impl IntoQueryParam for &str {
fn into_query_param(self, key: &str) -> QueryParam<'_> {
(key, self.to_owned())
}
}
impl IntoQueryParam for u64 {
fn into_query_param(self, key: &str) -> QueryParam<'_> {
(key, self.to_string())
}
}
impl IntoQueryParam for f32 {
fn into_query_param(self, key: &str) -> QueryParam<'_> {
(key, self.to_string())
}
}
impl IntoQueryParam for &NaiveDate {
fn into_query_param(self, key: &str) -> QueryParam<'_> {
(key, self.format("%Y-%m-%d").to_string())
}
}
impl IntoQueryParam for &NaiveDateTime {
fn into_query_param(self, key: &str) -> QueryParam<'_> {
(key, self.format("%Y-%m-%d %H:%M:%S").to_string())
}
}
impl IntoQueryParam for &ItemType {
fn into_query_param(self, key: &str) -> QueryParam<'_> {
(key, self.to_string())
}
}
impl IntoQueryParam for &ItemSubType {
fn into_query_param(self, key: &str) -> QueryParam<'_> {
(key, self.to_string())
}
}
impl IntoQueryParam for &CollectionItemType {
fn into_query_param(self, key: &str) -> QueryParam<'_> {
(key, self.to_string())
}
}
impl IntoQueryParam for &GameType {
fn into_query_param(self, key: &str) -> QueryParam<'_> {
(key, self.to_string())
}
}
impl IntoQueryParam for &WishlistPriority {
fn into_query_param(self, key: &str) -> QueryParam<'_> {
match self {
WishlistPriority::DontBuyThis => (key, "5".to_owned()),
WishlistPriority::ThinkingAboutIt => (key, "4".to_owned()),
WishlistPriority::LikeToHave => (key, "3".to_owned()),
WishlistPriority::LoveToHave => (key, "2".to_owned()),
WishlistPriority::MustHave => (key, "1".to_owned()),
}
}
}
impl IntoQueryParam for &ItemDomain {
fn into_query_param(self, key: &str) -> QueryParam<'_> {
match self {
ItemDomain::Family => (key, "family".to_owned()),
ItemDomain::Item => (key, "thing".to_owned()),
}
}
}
impl<Stringable: ToString> IntoQueryParam for &Vec<Stringable> {
fn into_query_param(self, key: &str) -> QueryParam<'_> {
let value_list = self
.iter()
.map(ToString::to_string)
.collect::<Vec<String>>()
.join(",");
(key, value_list)
}
}
impl<Stringable: ToString> IntoQueryParam for &[Stringable] {
fn into_query_param(self, key: &str) -> QueryParam<'_> {
let value_list = self
.iter()
.map(ToString::to_string)
.collect::<Vec<String>>()
.join(",");
(key, value_list)
}
}