-
Notifications
You must be signed in to change notification settings - Fork 53
/
resource.rs
113 lines (88 loc) · 2.31 KB
/
resource.rs
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
#![recursion_limit="256"]
use futures;
use tower_web::{Serialize, Response, impl_web};
use std::fmt::Display;
mod support;
use crate::support::*;
#[derive(Clone, Debug)]
struct TestResource;
#[derive(Clone, Debug)]
struct HelloWorld<S>(S);
#[derive(Clone, Debug)]
struct WhereHelloWorld<S>(S);
#[derive(Clone, Debug)]
struct EmptyGeneric<S>(S);
#[derive(Clone, Debug)]
struct EmptyWhere<S>(S);
#[derive(Response)]
struct Inner<T>(T);
#[derive(Serialize)]
struct GeneratedResource<T>(T);
#[derive(Serialize)]
struct ResponseFuture {
msg: &'static str,
}
impl_web! {
impl TestResource {
#[get("/impl_future")]
#[content_type("plain")]
fn impl_future(&self) -> impl Future<Item = &'static str, Error = ()> {
use futures::IntoFuture;
Ok("impl_future").into_future()
}
#[get("/inner")]
#[content_type("json")]
fn inner(&self) -> Result<Inner<GeneratedResource<ResponseFuture>>, ()> {
Ok(Inner(GeneratedResource(ResponseFuture {
msg: "hello",
})))
}
}
impl<S: Display> HelloWorld<S> {
#[get("/")]
fn hello(&self) -> Result<String, ()> {
let r = self.0.to_string();
Ok(r)
}
}
impl<S> WhereHelloWorld<S>
where
S: Display,
{
#[get("/")]
fn hello(&self) -> Result<String, ()> {
let r = self.0.to_string();
Ok(r)
}
}
// Just make sure this compiles
impl<S> EmptyGeneric<S> {
}
impl<S> EmptyWhere<S> where S: Display {
}
}
#[test]
fn impl_future() {
let mut web = service(TestResource);
let response = web.call_unwrap(get!("/impl_future"));
assert_ok!(response);
assert_body!(response, "impl_future");
}
#[test]
fn generic() {
let mut web = service(HelloWorld("hello"));
let response = web.call_unwrap(get!("/"));
assert_ok!(response);
assert_body!(response, "hello");
let mut web = service(WhereHelloWorld("hello"));
let response = web.call_unwrap(get!("/"));
assert_ok!(response);
assert_body!(response, "hello");
}
#[test]
fn inner_type() {
let mut web = service(TestResource);
let response = web.call_unwrap(get!("/inner"));
assert_ok!(response);
assert_body!(response, r#"{"msg":"hello"}"#);
}