use std::fmt::{Debug, Error, Formatter};
use http::{header::HeaderName, HeaderMap};
use crate::instrumentation::{MakeFmt, MakeIdentity};
use super::{
headers::{HeaderMarker, MakeHeaders},
uri::{GreedyLabel, MakeLabel, MakeQuery, MakeUri, QueryMarker},
};
#[derive(Clone)]
pub struct RequestFmt<Headers, Uri> {
headers: Headers,
uri: Uri,
}
impl<Headers, Uri> Debug for RequestFmt<Headers, Uri> {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
f.debug_struct("RequestFmt").finish_non_exhaustive()
}
}
pub type DefaultRequestFmt = RequestFmt<MakeIdentity, MakeUri<MakeIdentity, MakeIdentity>>;
impl Default for DefaultRequestFmt {
fn default() -> Self {
Self {
headers: MakeIdentity,
uri: MakeUri::default(),
}
}
}
impl DefaultRequestFmt {
pub fn new() -> Self {
Self::default()
}
}
impl<Header, Uri> RequestFmt<Header, Uri> {
pub fn header<F>(self, headers: F) -> RequestFmt<MakeHeaders<F>, Uri>
where
F: Fn(&HeaderName) -> HeaderMarker,
{
RequestFmt {
headers: MakeHeaders(headers),
uri: self.uri,
}
}
}
impl<Header, P, Q> RequestFmt<Header, MakeUri<P, Q>> {
pub fn label<F>(
self,
label_marker: F,
greedy_label: Option<GreedyLabel>,
) -> RequestFmt<Header, MakeUri<MakeLabel<F>, Q>>
where
F: Fn(usize) -> bool,
{
RequestFmt {
headers: self.headers,
uri: MakeUri {
make_path: MakeLabel {
label_marker,
greedy_label,
},
make_query: self.uri.make_query,
},
}
}
pub fn query<F>(self, query: F) -> RequestFmt<Header, MakeUri<P, MakeQuery<F>>>
where
F: Fn(&str) -> QueryMarker,
{
RequestFmt {
headers: self.headers,
uri: MakeUri {
make_path: self.uri.make_path,
make_query: MakeQuery(query),
},
}
}
}
impl<'a, Headers, Uri> MakeFmt<&'a HeaderMap> for RequestFmt<Headers, Uri>
where
Headers: MakeFmt<&'a HeaderMap>,
{
type Target = Headers::Target;
fn make(&self, source: &'a HeaderMap) -> Self::Target {
self.headers.make(source)
}
}
impl<'a, Headers, Uri> MakeFmt<&'a http::Uri> for RequestFmt<Headers, Uri>
where
Uri: MakeFmt<&'a http::Uri>,
{
type Target = Uri::Target;
fn make(&self, source: &'a http::Uri) -> Self::Target {
self.uri.make(source)
}
}