Skip to content

Commit fc39524

Browse files
Add the start of a webmachine_derive macro
Add the partially done implementation of a webmachine_derive macro to aid in creating applications that use different types of resources. Update the basic example to demonstrate the macro usage.
1 parent e34c7e7 commit fc39524

File tree

9 files changed

+479
-8
lines changed

9 files changed

+479
-8
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

22
/target/
3+
/webmachine_derive/target/
34
**/*.rs.bk
45
Cargo.lock
56
TAGS

Cargo.toml

+4
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,7 @@ hyper = "0.11.27"
1111
mime = "0.3"
1212
itertools = "0.8.0"
1313
radix_trie = { git = "https://github.com/kellymclaughlin/rust_radix_trie", branch = "prefix-match" }
14+
webmachine_derive = { path = "./webmachine_derive" }
15+
16+
[dev-dependencies]
17+
webmachine_derive = { path = "./webmachine_derive" }

examples/basic.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use hyper::{Body, Method, Request};
22
use mime;
33
use mime::Mime;
44

5+
use webmachine_derive::*;
56
use airship::resource::{Resource, Webmachine};
67
use airship::server;
78

@@ -21,11 +22,18 @@ impl Webmachine for GetResource {
2122
}
2223
}
2324

25+
#[derive(Clone, Webmachine)]
26+
enum MyResources {
27+
Get(GetResource),
28+
Res(Resource)
29+
}
30+
31+
2432
fn main() {
2533
let addr = "127.0.0.1:3000".parse().unwrap();
2634
let routes = vec![
27-
("test </> place", GetResource {}),
28-
("test </> route </> ::name::", GetResource {}),
35+
("test </> place", MyResources::Get(GetResource {})),
36+
("test </> route </> ::name::", MyResources::Res(Resource {})),
2937
];
3038
server::run(addr, &routes);
3139
}

rustfmt.toml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
max_width = 80

src/resource.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ use hyper::header::*;
55
use mime;
66
use mime::Mime;
77

8+
use webmachine_derive::*;
9+
810
pub trait Webmachine {
911
// Whether to allow HTTP POSTs to a missing resource. Default: false.
1012
fn allow_missing_post(&self) -> bool {
@@ -202,12 +204,9 @@ pub trait Webmachine {
202204
}
203205
}
204206

205-
#[derive(Clone)]
207+
#[derive(Clone, Webmachine)]
206208
pub struct Resource;
207209

208-
impl Webmachine for Resource {}
209-
210-
211210
/// Used when processing POST requests so as to handle the outcome of the binary
212211
/// decisions between handling a POST as a create request and whether to
213212
/// redirect after the POST is done. Credit for this idea goes to Richard

src/route.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,10 @@ pub enum RouteLeaf<R: Webmachine> {
8282
pub struct RoutingSpec<'a, R: Webmachine>(pub Vec<(&'a str, R)>);
8383
pub struct RoutingTrie<R: Webmachine>(pub Trie<String, RouteLeaf<R>>);
8484

85-
impl<'a, R> From<RoutingSpec<'a, R>> for RoutingTrie<R> where R: Webmachine {
85+
impl<'a, R> From<RoutingSpec<'a, R>> for RoutingTrie<R>
86+
where
87+
R: Webmachine
88+
{
8689
fn from(spec: RoutingSpec<R>) -> Self {
8790
// Convert the route string into a vector of `Route`s
8891
let routes: Vec<(Route, R)> =

src/server.rs

-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use crate::route;
1111
use crate::route::{RoutingSpec, RoutingTrie};
1212
use crate::types::RequestState;
1313

14-
// #[derive(Clone)]
1514
struct Airship<R>
1615
where
1716
R: Webmachine + Clone

webmachine_derive/Cargo.toml

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[package]
2+
name = "webmachine_derive"
3+
version = "0.1.0"
4+
authors = ["Kelly McLaughlin <[email protected]>"]
5+
edition = "2018"
6+
7+
[lib]
8+
proc-macro = true
9+
10+
[dependencies]
11+
syn = "1.0.5"
12+
quote = "1.0.2"
13+
14+
[dependencies.proc-macro2]
15+
version = "1.0"
16+
default-features = false

0 commit comments

Comments
 (0)