use serde::de::DeserializeOwned;
use topcoat_core::context::Cx;
use crate::context::uri;
pub trait QueryParams {
type Output<'cx>;
#[doc(hidden)]
fn query_params(cx: &Cx, _: QueryParamsSealed) -> Self::Output<'_>;
}
#[inline]
#[must_use]
pub fn query_params<T: QueryParams>(cx: &Cx) -> T::Output<'_> {
T::query_params(cx, QueryParamsSealed::new())
}
pub type QueryParamsError = serde_path_to_error::Error<serde_urlencoded::de::Error>;
pub fn parse_query_params<T: DeserializeOwned>(cx: &Cx) -> Result<T, QueryParamsError> {
let query = uri(cx).query().unwrap_or("");
let deserializer =
serde_urlencoded::Deserializer::new(form_urlencoded::parse(query.as_bytes()));
serde_path_to_error::deserialize(deserializer)
}
#[doc(hidden)]
#[derive(Debug)]
pub struct QueryParamsSealed(());
impl QueryParamsSealed {
pub(crate) fn new() -> Self {
Self(())
}
}