#[cfg(any(feature = "feat-integrate-axum", feature = "feat-integrate-tower"))]
pub mod integration;
use std::{
borrow::{Borrow, Cow},
collections::HashMap,
hash::Hash,
sync::Arc,
};
use macro_toolset::wrapper;
#[deprecated(
since = "0.6.0",
note = "Renamed and deprecated, use [`Query`] instead."
)]
pub type Queries<'q> = Query<'q>;
#[deprecated(
since = "0.6.0",
note = "Renamed and deprecated, use [`OwnedQuery`] instead."
)]
pub type OwnedQueries = OwnedQuery;
wrapper! {
#[derive(Debug, Clone)]
pub Query<'q>(HashMap<Cow<'q, str>, Cow<'q, str>, foldhash::fast::RandomState>)
}
impl<'q> Query<'q> {
#[cfg(feature = "feat-integrate-http")]
#[inline]
pub fn parse_uri(uri: &'q http::Uri) -> Option<Self> {
uri.query().map(Self::parse)
}
#[inline]
pub fn parse(query: &'q str) -> Self {
use fluent_uri::encoding::{encoder::IQuery, EStr};
Self {
inner: EStr::<IQuery>::new(query)
.unwrap_or({
#[cfg(feature = "feat-tracing")]
tracing::warn!("Failed to parse `{query}`");
EStr::EMPTY
})
.split('&')
.map(|pair| {
pair.split_once('=').unwrap_or({
#[cfg(feature = "feat-tracing")]
tracing::warn!("Failed to split query pair: {:?}", pair);
(pair, EStr::EMPTY)
})
})
.map(|(k, v)| {
(
k.decode().into_string_lossy(),
v.decode().into_string_lossy(),
)
})
.collect::<HashMap<_, _, _>>(),
}
}
}
wrapper! {
#[derive(Debug, Clone)]
pub OwnedQuery(Arc<HashMap<Arc<str>, Arc<str>, foldhash::fast::RandomState>>)
}
impl OwnedQuery {
#[cfg(feature = "feat-integrate-http")]
#[inline]
pub fn parse_uri(uri: &http::Uri) -> Option<Self> {
uri.query().map(Self::parse)
}
#[allow(clippy::multiple_bound_locations)]
#[inline]
pub fn get<Q: ?Sized>(&self, k: &Q) -> Option<&str>
where
Arc<str>: Borrow<Q>,
Q: Hash + Eq,
{
self.inner.get(k).map(|v| &**v)
}
#[inline]
pub fn parse(query: &str) -> Self {
use fluent_uri::encoding::{encoder::IQuery, EStr};
Self {
inner: EStr::<IQuery>::new(query)
.unwrap_or({
#[cfg(feature = "feat-tracing")]
tracing::warn!("Failed to parse `{query}`");
EStr::EMPTY
})
.split('&')
.map(|pair| {
pair.split_once('=').unwrap_or({
#[cfg(feature = "feat-tracing")]
tracing::warn!("Failed to split query pair: {:?}", pair);
(pair, EStr::EMPTY)
})
})
.map(|(k, v)| {
(
k.decode().into_string_lossy().into(),
v.decode().into_string_lossy().into(),
)
})
.collect::<HashMap<_, _, _>>()
.into(),
}
}
}