use std::borrow::Cow;
use chrono::{DateTime, NaiveDate, Utc};
use url::Url;
use crate::api::BodyError;
pub trait ParamValue<'a> {
#[allow(clippy::wrong_self_convention)]
fn as_value(&self) -> Cow<'a, str>;
}
impl ParamValue<'static> for bool {
fn as_value(&self) -> Cow<'static, str> {
if *self {
"true".into()
} else {
"false".into()
}
}
}
impl<'a> ParamValue<'a> for &'a str {
fn as_value(&self) -> Cow<'a, str> {
(*self).into()
}
}
impl ParamValue<'static> for String {
fn as_value(&self) -> Cow<'static, str> {
self.clone().into()
}
}
impl<'a> ParamValue<'a> for &'a String {
fn as_value(&self) -> Cow<'a, str> {
(*self).into()
}
}
impl<'a> ParamValue<'a> for Cow<'a, str> {
fn as_value(&self) -> Cow<'a, str> {
self.clone()
}
}
impl<'a, 'b: 'a> ParamValue<'a> for &'b Cow<'a, str> {
fn as_value(&self) -> Cow<'a, str> {
(*self).clone()
}
}
impl ParamValue<'static> for u64 {
fn as_value(&self) -> Cow<'static, str> {
format!("{}", self).into()
}
}
impl ParamValue<'static> for f64 {
fn as_value(&self) -> Cow<'static, str> {
format!("{}", self).into()
}
}
impl ParamValue<'static> for DateTime<Utc> {
fn as_value(&self) -> Cow<'static, str> {
self.to_rfc3339_opts(chrono::SecondsFormat::Secs, true)
.into()
}
}
impl ParamValue<'static> for NaiveDate {
fn as_value(&self) -> Cow<'static, str> {
format!("{}", self.format("%Y-%m-%d")).into()
}
}
#[derive(Debug, Default, Clone)]
pub struct FormParams<'a> {
params: Vec<(Cow<'a, str>, Cow<'a, str>)>,
}
impl<'a> FormParams<'a> {
pub fn push<'b, K, V>(&mut self, key: K, value: V) -> &mut Self
where
K: Into<Cow<'a, str>>,
V: ParamValue<'b>,
'b: 'a,
{
self.params.push((key.into(), value.as_value()));
self
}
pub fn push_opt<'b, K, V>(&mut self, key: K, value: Option<V>) -> &mut Self
where
K: Into<Cow<'a, str>>,
V: ParamValue<'b>,
'b: 'a,
{
if let Some(value) = value {
self.params.push((key.into(), value.as_value()));
}
self
}
pub fn extend<'b, I, K, V>(&mut self, iter: I) -> &mut Self
where
I: Iterator<Item = (K, V)>,
K: Into<Cow<'a, str>>,
V: ParamValue<'b>,
'b: 'a,
{
self.params
.extend(iter.map(|(key, value)| (key.into(), value.as_value())));
self
}
pub fn into_body(self) -> Result<Option<(&'static str, Vec<u8>)>, BodyError> {
let body = serde_urlencoded::to_string(self.params)?;
Ok(Some((
"application/x-www-form-urlencoded",
body.into_bytes(),
)))
}
}
#[derive(Debug, Default, Clone)]
pub struct QueryParams<'a> {
params: Vec<(Cow<'a, str>, Cow<'a, str>)>,
}
impl<'a> QueryParams<'a> {
pub fn push<'b, K, V>(&mut self, key: K, value: V) -> &mut Self
where
K: Into<Cow<'a, str>>,
V: ParamValue<'b>,
'b: 'a,
{
self.params.push((key.into(), value.as_value()));
self
}
pub fn push_opt<'b, K, V>(&mut self, key: K, value: Option<V>) -> &mut Self
where
K: Into<Cow<'a, str>>,
V: ParamValue<'b>,
'b: 'a,
{
if let Some(value) = value {
self.params.push((key.into(), value.as_value()));
}
self
}
pub fn extend<'b, I, K, V>(&mut self, iter: I) -> &mut Self
where
I: Iterator<Item = (K, V)>,
K: Into<Cow<'a, str>>,
V: ParamValue<'b>,
'b: 'a,
{
self.params
.extend(iter.map(|(key, value)| (key.into(), value.as_value())));
self
}
pub fn add_to_url(&self, url: &mut Url) {
let mut pairs = url.query_pairs_mut();
pairs.extend_pairs(self.params.iter());
}
}
#[cfg(test)]
mod tests {
use crate::api::ParamValue;
#[test]
fn bool_str() {
let items = &[(true, "true"), (false, "false")];
for (i, s) in items {
assert_eq!((*i).as_value(), *s);
}
}
}