Compare commits

..

No commits in common. "52011912613edd94d99dc6c3b50d0dd8789fff14" and "90f27837b3571a19e3e7d00dd467f9b197a335c8" have entirely different histories.

4 changed files with 85 additions and 115 deletions

View File

@ -96,20 +96,20 @@ where
}
/// Call Deref::deref() on the input and pass it to the next mapper.
pub fn deref<M>(inner: M) -> Deref<M> {
pub fn deref<C>(inner: C) -> Deref<C> {
Deref(inner)
}
/// The `Deref` mapper returned by [deref()](fn.deref.html)
#[derive(Debug)]
pub struct Deref<M>(M);
impl<M, IN> Mapper<IN> for Deref<M>
pub struct Deref<C>(C);
impl<C, IN> Mapper<IN> for Deref<C>
where
M: Mapper<IN::Target>,
C: Mapper<IN::Target>,
IN: std::ops::Deref,
{
type Out = M::Out;
type Out = C::Out;
fn map(&mut self, input: &IN) -> M::Out {
fn map(&mut self, input: &IN) -> C::Out {
self.0.map(input.deref())
}
}
@ -162,14 +162,14 @@ where
}
/// invert the result of the inner mapper.
pub fn not<M>(inner: M) -> Not<M> {
pub fn not<C>(inner: C) -> Not<C> {
Not(inner)
}
/// The `Not` mapper returned by [not()](fn.not.html)
pub struct Not<M>(M);
impl<M, IN> Mapper<IN> for Not<M>
pub struct Not<C>(C);
impl<C, IN> Mapper<IN> for Not<C>
where
M: Mapper<IN, Out = bool>,
C: Mapper<IN, Out = bool>,
IN: ?Sized,
{
type Out = bool;
@ -178,9 +178,9 @@ where
!self.0.map(input)
}
}
impl<M> fmt::Debug for Not<M>
impl<C> fmt::Debug for Not<C>
where
M: fmt::Debug,
C: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Not({:?})", &self.0)
@ -237,20 +237,20 @@ where
}
/// url decode the input and pass the resulting slice of key-value pairs to the next mapper.
pub fn url_decoded<M>(inner: M) -> UrlDecoded<M> {
pub fn url_decoded<C>(inner: C) -> UrlDecoded<C> {
UrlDecoded(inner)
}
/// The `UrlDecoded` mapper returned by [url_decoded()](fn.url_decoded.html)
#[derive(Debug)]
pub struct UrlDecoded<M>(M);
impl<IN, M> Mapper<IN> for UrlDecoded<M>
pub struct UrlDecoded<C>(C);
impl<IN, C> Mapper<IN> for UrlDecoded<C>
where
IN: AsRef<[u8]> + ?Sized,
M: Mapper<[(String, String)]>,
C: Mapper<[(String, String)]>,
{
type Out = M::Out;
type Out = C::Out;
fn map(&mut self, input: &IN) -> M::Out {
fn map(&mut self, input: &IN) -> C::Out {
let decoded: Vec<(String, String)> = url::form_urlencoded::parse(input.as_ref())
.into_owned()
.collect();
@ -263,20 +263,20 @@ where
///
/// If the input can't be decoded a serde_json::Value::Null is passed to the next
/// mapper.
pub fn json_decoded<M>(inner: M) -> JsonDecoded<M> {
pub fn json_decoded<C>(inner: C) -> JsonDecoded<C> {
JsonDecoded(inner)
}
/// The `JsonDecoded` mapper returned by [json_decoded()](fn.json_decoded.html)
#[derive(Debug)]
pub struct JsonDecoded<M>(M);
impl<IN, M> Mapper<IN> for JsonDecoded<M>
pub struct JsonDecoded<C>(C);
impl<IN, C> Mapper<IN> for JsonDecoded<C>
where
IN: AsRef<[u8]> + ?Sized,
M: Mapper<serde_json::Value>,
C: Mapper<serde_json::Value>,
{
type Out = M::Out;
type Out = C::Out;
fn map(&mut self, input: &IN) -> M::Out {
fn map(&mut self, input: &IN) -> C::Out {
let json_value: serde_json::Value =
serde_json::from_slice(input.as_ref()).unwrap_or(serde_json::Value::Null);
self.0.map(&json_value)
@ -284,20 +284,23 @@ where
}
/// lowercase the input and pass it to the next mapper.
pub fn lowercase<M>(inner: M) -> Lowercase<M> {
pub fn lowercase<C>(inner: C) -> Lowercase<C>
where
C: Mapper<[u8]>,
{
Lowercase(inner)
}
/// The `Lowercase` mapper returned by [lowercase()](fn.lowercase.html)
#[derive(Debug)]
pub struct Lowercase<M>(M);
impl<IN, M> Mapper<IN> for Lowercase<M>
pub struct Lowercase<C>(C);
impl<IN, C> Mapper<IN> for Lowercase<C>
where
IN: AsRef<[u8]> + ?Sized,
M: Mapper<[u8]>,
C: Mapper<[u8]>,
{
type Out = M::Out;
type Out = C::Out;
fn map(&mut self, input: &IN) -> M::Out {
fn map(&mut self, input: &IN) -> C::Out {
use bstr::ByteSlice;
self.0.map(&input.as_ref().to_lowercase())
}
@ -325,31 +328,6 @@ impl<F> fmt::Debug for MapFn<F> {
}
}
/// inspect the input and pass it to the next mapper.
///
/// This logs the value as it passes it to the next mapper unchanged. Can be
/// useful when troubleshooting why a matcher may not be working as intended.
pub fn inspect<M>(inner: M) -> Inspect<M> {
Inspect(inner)
}
/// The `Inspect` mapper returned by [inspect()](fn.inspect.html)
#[derive(Debug)]
pub struct Inspect<M>(M);
impl<IN, M> Mapper<IN> for Inspect<M>
where
IN: fmt::Debug + ?Sized,
M: Mapper<IN>,
M::Out: fmt::Debug,
{
type Out = M::Out;
fn map(&mut self, input: &IN) -> M::Out {
let output = self.0.map(input);
log::debug!("{:?}.map({:?}) == {:?}", self.0, input, output);
output
}
}
#[cfg(test)]
mod tests {
use super::*;
@ -467,12 +445,4 @@ mod tests {
assert_eq!(true, c.map(&0));
assert_eq!(false, c.map(&11));
}
#[test]
fn test_inspect() {
let _ = pretty_env_logger::try_init();
let mut c = inspect(lowercase(matches("^foobar$")));
assert_eq!(true, c.map("Foobar"));
assert_eq!(false, c.map("Foobar1"));
}
}

View File

@ -3,74 +3,74 @@
use super::Mapper;
/// Extract the method from the HTTP request and pass it to the next mapper.
pub fn method<M>(inner: M) -> Method<M> {
pub fn method<C>(inner: C) -> Method<C> {
Method(inner)
}
/// The `Method` mapper returned by [method()](fn.method.html)
#[derive(Debug)]
pub struct Method<M>(M);
impl<M, B> Mapper<hyper::Request<B>> for Method<M>
pub struct Method<C>(C);
impl<C, B> Mapper<hyper::Request<B>> for Method<C>
where
M: Mapper<str>,
C: Mapper<str>,
{
type Out = M::Out;
type Out = C::Out;
fn map(&mut self, input: &hyper::Request<B>) -> M::Out {
fn map(&mut self, input: &hyper::Request<B>) -> C::Out {
self.0.map(input.method().as_str())
}
}
/// Extract the path from the HTTP request and pass it to the next mapper.
pub fn path<M>(inner: M) -> Path<M> {
pub fn path<C>(inner: C) -> Path<C> {
Path(inner)
}
/// The `Path` mapper returned by [path()](fn.path.html)
#[derive(Debug)]
pub struct Path<M>(M);
impl<M, B> Mapper<hyper::Request<B>> for Path<M>
pub struct Path<C>(C);
impl<C, B> Mapper<hyper::Request<B>> for Path<C>
where
M: Mapper<str>,
C: Mapper<str>,
{
type Out = M::Out;
type Out = C::Out;
fn map(&mut self, input: &hyper::Request<B>) -> M::Out {
fn map(&mut self, input: &hyper::Request<B>) -> C::Out {
self.0.map(input.uri().path())
}
}
/// Extract the query from the HTTP request and pass it to the next mapper.
pub fn query<M>(inner: M) -> Query<M> {
pub fn query<C>(inner: C) -> Query<C> {
Query(inner)
}
/// The `Query` mapper returned by [query()](fn.query.html)
#[derive(Debug)]
pub struct Query<M>(M);
impl<M, B> Mapper<hyper::Request<B>> for Query<M>
pub struct Query<C>(C);
impl<C, B> Mapper<hyper::Request<B>> for Query<C>
where
M: Mapper<str>,
C: Mapper<str>,
{
type Out = M::Out;
type Out = C::Out;
fn map(&mut self, input: &hyper::Request<B>) -> M::Out {
fn map(&mut self, input: &hyper::Request<B>) -> C::Out {
self.0.map(input.uri().query().unwrap_or(""))
}
}
/// Extract the headers from the HTTP request and pass the sequence to the next
/// mapper.
pub fn headers<M>(inner: M) -> Headers<M> {
pub fn headers<C>(inner: C) -> Headers<C> {
Headers(inner)
}
/// The `Headers` mapper returned by [headers()](fn.headers.html)
#[derive(Debug)]
pub struct Headers<M>(M);
impl<M, B> Mapper<hyper::Request<B>> for Headers<M>
pub struct Headers<C>(C);
impl<C, B> Mapper<hyper::Request<B>> for Headers<C>
where
M: Mapper<[(Vec<u8>, Vec<u8>)]>,
C: Mapper<[(Vec<u8>, Vec<u8>)]>,
{
type Out = M::Out;
type Out = C::Out;
fn map(&mut self, input: &hyper::Request<B>) -> M::Out {
fn map(&mut self, input: &hyper::Request<B>) -> C::Out {
let headers: Vec<(Vec<u8>, Vec<u8>)> = input
.headers()
.iter()
@ -81,20 +81,20 @@ where
}
/// Extract the body from the HTTP request and pass it to the next mapper.
pub fn body<M>(inner: M) -> Body<M> {
pub fn body<C>(inner: C) -> Body<C> {
Body(inner)
}
/// The `Body` mapper returned by [body()](fn.body.html)
#[derive(Debug)]
pub struct Body<M>(M);
impl<M, B> Mapper<hyper::Request<B>> for Body<M>
pub struct Body<C>(C);
impl<C, B> Mapper<hyper::Request<B>> for Body<C>
where
B: ToOwned,
M: Mapper<B::Owned>,
C: Mapper<B::Owned>,
{
type Out = M::Out;
type Out = C::Out;
fn map(&mut self, input: &hyper::Request<B>) -> M::Out {
fn map(&mut self, input: &hyper::Request<B>) -> C::Out {
self.0.map(&input.body().to_owned())
}
}

View File

@ -3,38 +3,38 @@
use super::Mapper;
/// Extract the status code from the HTTP response and pass it to the next mapper.
pub fn status_code<M>(inner: M) -> StatusCode<M> {
pub fn status_code<C>(inner: C) -> StatusCode<C> {
StatusCode(inner)
}
/// The `StatusCode` mapper returned by [status_code()](fn.status_code.html)
#[derive(Debug)]
pub struct StatusCode<M>(M);
impl<M, B> Mapper<hyper::Response<B>> for StatusCode<M>
pub struct StatusCode<C>(C);
impl<C, B> Mapper<hyper::Response<B>> for StatusCode<C>
where
M: Mapper<u16>,
C: Mapper<u16>,
{
type Out = M::Out;
type Out = C::Out;
fn map(&mut self, input: &hyper::Response<B>) -> M::Out {
fn map(&mut self, input: &hyper::Response<B>) -> C::Out {
self.0.map(&input.status().as_u16())
}
}
/// Extract the headers from the HTTP response and pass the sequence to the next
/// mapper.
pub fn headers<M>(inner: M) -> Headers<M> {
pub fn headers<C>(inner: C) -> Headers<C> {
Headers(inner)
}
/// The `Headers` mapper returned by [headers()](fn.headers.html)
#[derive(Debug)]
pub struct Headers<M>(M);
impl<M, B> Mapper<hyper::Response<B>> for Headers<M>
pub struct Headers<C>(C);
impl<C, B> Mapper<hyper::Response<B>> for Headers<C>
where
M: Mapper<[(Vec<u8>, Vec<u8>)]>,
C: Mapper<[(Vec<u8>, Vec<u8>)]>,
{
type Out = M::Out;
type Out = C::Out;
fn map(&mut self, input: &hyper::Response<B>) -> M::Out {
fn map(&mut self, input: &hyper::Response<B>) -> C::Out {
let headers: Vec<(Vec<u8>, Vec<u8>)> = input
.headers()
.iter()
@ -45,19 +45,19 @@ where
}
/// Extract the body from the HTTP response and pass it to the next mapper.
pub fn body<M>(inner: M) -> Body<M> {
pub fn body<C>(inner: C) -> Body<C> {
Body(inner)
}
/// The `Body` mapper returned by [body()](fn.body.html)
#[derive(Debug)]
pub struct Body<M>(M);
impl<M, B> Mapper<hyper::Response<B>> for Body<M>
pub struct Body<C>(C);
impl<C, B> Mapper<hyper::Response<B>> for Body<C>
where
M: Mapper<B>,
C: Mapper<B>,
{
type Out = M::Out;
type Out = C::Out;
fn map(&mut self, input: &hyper::Response<B>) -> M::Out {
fn map(&mut self, input: &hyper::Response<B>) -> C::Out {
self.0.map(input.body())
}
}

View File

@ -4,15 +4,15 @@ use super::Mapper;
/// true if the provided mapper returns true for any of the elements in the
/// sequence.
pub fn contains<M>(inner: M) -> Contains<M> {
pub fn contains<C>(inner: C) -> Contains<C> {
Contains(inner)
}
/// The `Contains` mapper returned by [contains()](fn.contains.html)
#[derive(Debug)]
pub struct Contains<M>(M);
impl<M, E> Mapper<[E]> for Contains<M>
pub struct Contains<C>(C);
impl<C, E> Mapper<[E]> for Contains<C>
where
M: Mapper<E, Out = bool>,
C: Mapper<E, Out = bool>,
{
type Out = bool;