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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
//! Extracts query string parameters into type safe structs

use std;

use std::error::Error;
use std::str::FromStr;
use std::string::ParseError;
use std::str::ParseBoolError;
use std::num::{ParseIntError, ParseFloatError};

use hyper::Response;

use state::State;
use http::FormUrlDecoded;
use router::response::extender::StaticResponseExtender;

/// Extracts the `Request` query string into `State`. On failure is capable of extending `Response`
/// to indicate why the extraction process failed.
///
/// This functionality can be simply derived for application structs via `QueryStringExtractor`,
/// which will attempt to populate the associated struct. Combine with the derive
/// `StaticResponseExtender` to have invalid query string data result in "400 Bad Request".
///
/// Custom responses can be created by using the `QueryStringExtractor` derive and then
/// implementing `StaticResponseExtender` independently.
pub trait QueryStringExtractor: StaticResponseExtender {
    /// Populates the struct with data from the `Request` query string and adds it to `State`
    fn extract(state: &mut State, query: Option<&str>) -> Result<(), String>;
}

/// A `QueryStringExtractor` that does not extract/store any data.
///
/// Useful in purely static routes and within documentation.
#[derive(Debug)]
pub struct NoopQueryStringExtractor;
impl QueryStringExtractor for NoopQueryStringExtractor {
    fn extract(_state: &mut State, _query: Option<&str>) -> Result<(), String> {
        Ok(())
    }
}

impl StaticResponseExtender for NoopQueryStringExtractor {
    fn extend(_state: &mut State, _res: &mut Response) {}
}

#[derive(Debug)]
/// Represents an error in coverting a key=value pair from a `Request` query string into a
/// type safe value.
pub struct FromQueryStringError {
    description: String,
}

impl std::fmt::Display for FromQueryStringError {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "Error decoding query string: {}", self.description)
    }
}

impl Error for FromQueryStringError {
    fn description(&self) -> &str {
        &self.description
    }
}

/// Converts string data received as part of a `Request` query string to type safe values for
/// usage by `Middleware` and `Handlers`.
pub trait FromQueryString {
    /// Converts a key=value pair from `Request` query string into a type safe value.
    ///
    /// # Panic
    /// If the input data is not of the expected format or size a panic will occur.
    fn from_query_string(&str, &[FormUrlDecoded]) -> Result<Self, FromQueryStringError>
    where
        Self: Sized;
}

impl<T> FromQueryString for Option<T>
where
    T: FromQueryString,
{
    fn from_query_string(
        key: &str,
        values: &[FormUrlDecoded],
    ) -> Result<Self, FromQueryStringError> {
        if values.len() == 0 {
            Ok(None)
        } else {
            match T::from_query_string(key, values) {
                Ok(v) => Ok(Some(v)),
                Err(v) => Err(v),
            }
        }
    }
}

impl<T> FromQueryString for Vec<T>
where
    T: FromQueryString,
{
    fn from_query_string(
        key: &str,
        values: &[FormUrlDecoded],
    ) -> Result<Self, FromQueryStringError> {
        values
            .windows(1)
            .map(|value| T::from_query_string(key, value))
            .collect()
    }
}

impl From<ParseIntError> for FromQueryStringError {
    fn from(err: ParseIntError) -> FromQueryStringError {
        FromQueryStringError { description: err.description().to_string() }
    }
}

impl From<ParseFloatError> for FromQueryStringError {
    fn from(err: ParseFloatError) -> FromQueryStringError {
        FromQueryStringError { description: err.description().to_string() }
    }
}

impl From<ParseBoolError> for FromQueryStringError {
    fn from(err: ParseBoolError) -> FromQueryStringError {
        FromQueryStringError { description: err.description().to_string() }
    }
}

impl From<ParseError> for FromQueryStringError {
    fn from(err: ParseError) -> FromQueryStringError {
        FromQueryStringError { description: err.description().to_string() }
    }
}

macro_rules! fstr {
    ($($t:ident),*) => { $(
        impl FromQueryString for $t {
            fn from_query_string(_key: &str, values: &[FormUrlDecoded])
                -> Result<Self, FromQueryStringError> {
                if values.len() == 1 {
                    Ok($t::from_str(values[0].val())?)
                } else {
                    Err(FromQueryStringError {
                            description: String::from("Invalid number of values")
                    })
                }
            }
        }
    )+}
}

fstr!(
    String,
    bool,
    f32,
    f64,
    isize,
    i8,
    i16,
    i32,
    i64,
    usize,
    u8,
    u16,
    u32,
    u64
);