1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
//! Methods for retrieving swagger-related information from an HTTP request.
use hyper::Request;

/// A macro for joining together two or more RequestParsers to create a struct that implements
/// RequestParser with a function parse_operation_id that matches hyper requests against the different
/// RequestParsers in turn until it gets a match (or returns an error if none match)
///
/// The order in which the request parsers are passed to the macro specifies the order in which the request
/// is tested against them. If there is any possibility of two RequestParsers matching the same request
/// this should not be used.
#[macro_export]
macro_rules! request_parser_joiner {
    ($name:ident ,$($T:ty), *) => {
        struct $name;

        impl <B> RequestParser<B> for $name
            where $($T: RequestParser<B>, )*
        {
            fn parse_operation_id(request: &hyper::Request<B>) -> Result<&'static str, ()> {
                __impl_request_parser_joiner!(request, $($T), *)
            }
        }
    };
}

/// This macro should only be used by the request_parser_joiner macro
#[macro_export]
#[doc(hidden)]
macro_rules! __impl_request_parser_joiner {
    ($argname:expr, $head:ty) => {<$head as RequestParser<B>>::parse_operation_id(&$argname)};
    ($argname:expr, $head:ty, $( $tail:ty), *) => {
        match <$head as RequestParser<B>>::parse_operation_id(&$argname) {
                Ok(s) => Ok(s),
                Err(_) => __impl_request_parser_joiner!($argname, $( $tail), *),
        }
    };
}

/// A trait for retrieving swagger-related information from a request.
///
/// This allows other middlewares to retrieve API-related information from a request that
/// may not have been handled by the autogenerated API code yet.   For example, a statistics
/// tracking service may wish to use this to count requests per-operation.
///
/// The trait is automatically implemented by swagger-codegen.
pub trait RequestParser<B> {
    /// Retrieve the Swagger operation identifier that matches this request.
    ///
    /// Returns `Err(())` if this request does not match any known operation on this API.
    fn parse_operation_id(req: &Request<B>) -> Result<&'static str, ()>;
}

#[cfg(test)]
mod context_tests {
    use super::*;
    use hyper::{Body, Uri};
    use std::str::FromStr;

    struct TestParser1;

    impl RequestParser<Body> for TestParser1 {
        fn parse_operation_id(request: &hyper::Request<Body>) -> Result<&'static str, ()> {
            match request.uri().path() {
                "/test/t11" => Ok("t11"),
                "/test/t12" => Ok("t12"),
                _ => Err(()),
            }
        }
    }

    struct TestParser2;

    impl RequestParser<Body> for TestParser2 {
        fn parse_operation_id(request: &hyper::Request<Body>) -> Result<&'static str, ()> {
            match request.uri().path() {
                "/test/t21" => Ok("t21"),
                "/test/t22" => Ok("t22"),
                _ => Err(()),
            }
        }
    }

    #[test]
    fn test_macros() {
        let uri = Uri::from_str(&"https://www.rust-lang.org/test/t11").unwrap();
        let req1: Request<Body> = Request::get(uri).body(Body::empty()).unwrap();

        let uri = Uri::from_str(&"https://www.rust-lang.org/test/t22").unwrap();
        let req2: Request<Body> = Request::get(uri).body(Body::empty()).unwrap();

        let uri = Uri::from_str(&"https://www.rust-lang.org/test/t33").unwrap();
        let req3: Request<Body> = Request::get(uri).body(Body::empty()).unwrap();

        request_parser_joiner!(JoinedReqParser, TestParser1, TestParser2);

        assert_eq!(JoinedReqParser::parse_operation_id(&req1), Ok("t11"));
        assert_eq!(JoinedReqParser::parse_operation_id(&req2), Ok("t22"));
        assert_eq!(JoinedReqParser::parse_operation_id(&req3), Err(()));
    }
}