tower_web/extract/
num.rs

1use extract::{Extract, Error, Context, Immediate};
2use util::BufStream;
3
4use atoi::atoi;
5use checked::Checked;
6
7use std::error::Error as E;
8use std::str::FromStr;
9
10macro_rules! num_extract_impls {
11    ($($num:ident),+) => {
12        $(
13            impl<B: BufStream> Extract<B> for $num {
14                type Future = Immediate<$num>;
15
16                fn extract(ctx: &Context) -> Self::Future {
17                    use codegen::Source::*;
18
19                    match ctx.callsite().source() {
20                        Capture(idx) => {
21                            let path = ctx.request().uri().path();
22                            let capture = ctx.captures().get(*idx, path);
23
24                            $num::from_str(capture).map_err(|err| {
25                                Error::invalid_argument(&err.description())
26                            }).into()
27                        }
28                        Header(header_name) => {
29                            let value = match ctx.request().headers().get(header_name) {
30                                Some(value) => value,
31                                None => {
32                                    return Immediate::err(Error::missing_argument());
33                                }
34                            };
35
36                            match atoi(value.as_bytes()) {
37                                Some(Checked(Some(s))) => Immediate::ok(s),
38                                _ => Immediate::err(Error::invalid_argument(&"invalid integer")),
39                            }
40                        }
41                        QueryString => {
42                            unimplemented!();
43                        }
44                        Body => {
45                            unimplemented!();
46                        }
47                        Unknown => {
48                            unimplemented!();
49                        }
50                    }
51                }
52            }
53        )+
54    }
55}
56
57// i128,u128 don't work because "no implementation for `checked::Checked<i128> * checked::Checked<i128>"
58num_extract_impls!(u8, u16, u32, u64, i8, i16, i32, i64);