Skip to content

Commit c1877c2

Browse files
committed
restore web proc macros
1 parent 95d1b4a commit c1877c2

File tree

10 files changed

+139
-147
lines changed

10 files changed

+139
-147
lines changed

Cargo.toml

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ members = [
66
"ntex-rt",
77
"ntex-rt-macros",
88
"ntex-service",
9+
"ntex-macros",
910
]
1011

11-
#[patch.crates-io]
12-
#ntex = { path = "ntex" }
13-
#ntex-codec = { path = "ntex-codec" }
14-
#ntex-router = { path = "ntex-router" }
15-
#ntex-rt = { path = "ntex-rt" }
16-
#ntex-rt-macros = { path = "ntex-rt-macros" }
17-
#ntex-service = { path = "ntex-service" }
12+
[patch.crates-io]
13+
ntex = { path = "ntex" }
14+
ntex-codec = { path = "ntex-codec" }
15+
ntex-router = { path = "ntex-router" }
16+
ntex-rt = { path = "ntex-rt" }
17+
ntex-rt-macros = { path = "ntex-rt-macros" }
18+
ntex-service = { path = "ntex-service" }
19+
ntex-macros = { path = "ntex-macros" }

ntex-macros/CHANGES.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Changes
22

3-
## [0.1.0] - 2020-xx-xx
3+
## [0.1.0] - 2020-04-10
44

5-
* Fork
5+
* Fork to ntex namespace

ntex-macros/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@ syn = { version = "^1", features = ["full", "parsing"] }
1616
proc-macro2 = "^1"
1717

1818
[dev-dependencies]
19-
ntex = { path = "../ntex/" }
20-
futures = { version = "0.3.1" }
19+
ntex = "0.1.6"
20+
futures = "0.3.4"

ntex-macros/src/lib.rs

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
11
#![recursion_limit = "512"]
22
//! web macros module
33
//!
4-
//! Generators for routes and scopes
4+
//! Generators for routes
55
//!
66
//! ## Route
77
//!
88
//! Macros:
99
//!
10-
//! - [web_get](attr.get.html)
11-
//! - [web_post](attr.post.html)
12-
//! - [web_put](attr.put.html)
13-
//! - [web_delete](attr.delete.html)
14-
//! - [web_head](attr.head.html)
15-
//! - [web_connect](attr.connect.html)
16-
//! - [web_options](attr.options.html)
17-
//! - [web_trace](attr.trace.html)
18-
//! - [web_patch](attr.patch.html)
10+
//! - [get](attr.web_get.html)
11+
//! - [post](attr.web_post.html)
12+
//! - [put](attr.web_put.html)
13+
//! - [delete](attr.web_delete.html)
14+
//! - [head](attr.web_head.html)
15+
//! - [connect](attr.web_connect.html)
16+
//! - [options](attr.web_options.html)
17+
//! - [trace](attr.web_trace.html)
18+
//! - [patch](attr.web_patch.html)
1919
//!
2020
//! ### Attributes:
2121
//!
2222
//! - `"path"` - Raw literal string with path for which to register handle. Mandatory.
23-
//! - `guard="function_name"` - Registers function as guard using `ntex::web::guard::fn_guard`
23+
//! - `guard = "function_name"` - Registers function as guard using `ntex::web::guard::fn_guard`
24+
//! - `error = "ErrorRenderer"` - Register handler for specified error renderer
2425
//!
2526
//! ## Notes
2627
//!
@@ -30,11 +31,11 @@
3031
//! ## Example:
3132
//!
3233
//! ```rust
33-
//! use ntex::web::{get, HttpResponse};
34+
//! use ntex::web::{get, Error, HttpResponse};
3435
//! use futures::{future, Future};
3536
//!
3637
//! #[get("/test")]
37-
//! async fn async_test() -> Result<HttpResponse, ntex::http::Error> {
38+
//! async fn async_test() -> Result<HttpResponse, Error> {
3839
//! Ok(HttpResponse::Ok().finish())
3940
//! }
4041
//! ```
@@ -53,11 +54,12 @@ use syn::parse_macro_input;
5354
/// ## Attributes:
5455
///
5556
/// - `"path"` - Raw literal string with path for which to register handler. Mandatory.
56-
/// - `guard="function_name"` - Registers function as guard using `ntex::web::guard::fn_guard`
57+
/// - `guard = "function_name"` - Registers function as guard using `ntex::web::guard::fn_guard`
58+
/// - `error = "ErrorRenderer"` - Register handler for different error renderer
5759
#[proc_macro_attribute]
5860
pub fn web_get(args: TokenStream, input: TokenStream) -> TokenStream {
5961
let args = parse_macro_input!(args as syn::AttributeArgs);
60-
let gen = match route::Route::new(args, input, route::GuardType::Get) {
62+
let gen = match route::Route::new(args, input, route::MethodType::Get) {
6163
Ok(gen) => gen,
6264
Err(err) => return err.to_compile_error().into(),
6365
};
@@ -72,7 +74,7 @@ pub fn web_get(args: TokenStream, input: TokenStream) -> TokenStream {
7274
#[proc_macro_attribute]
7375
pub fn web_post(args: TokenStream, input: TokenStream) -> TokenStream {
7476
let args = parse_macro_input!(args as syn::AttributeArgs);
75-
let gen = match route::Route::new(args, input, route::GuardType::Post) {
77+
let gen = match route::Route::new(args, input, route::MethodType::Post) {
7678
Ok(gen) => gen,
7779
Err(err) => return err.to_compile_error().into(),
7880
};
@@ -87,7 +89,7 @@ pub fn web_post(args: TokenStream, input: TokenStream) -> TokenStream {
8789
#[proc_macro_attribute]
8890
pub fn web_put(args: TokenStream, input: TokenStream) -> TokenStream {
8991
let args = parse_macro_input!(args as syn::AttributeArgs);
90-
let gen = match route::Route::new(args, input, route::GuardType::Put) {
92+
let gen = match route::Route::new(args, input, route::MethodType::Put) {
9193
Ok(gen) => gen,
9294
Err(err) => return err.to_compile_error().into(),
9395
};
@@ -102,7 +104,7 @@ pub fn web_put(args: TokenStream, input: TokenStream) -> TokenStream {
102104
#[proc_macro_attribute]
103105
pub fn web_delete(args: TokenStream, input: TokenStream) -> TokenStream {
104106
let args = parse_macro_input!(args as syn::AttributeArgs);
105-
let gen = match route::Route::new(args, input, route::GuardType::Delete) {
107+
let gen = match route::Route::new(args, input, route::MethodType::Delete) {
106108
Ok(gen) => gen,
107109
Err(err) => return err.to_compile_error().into(),
108110
};
@@ -117,7 +119,7 @@ pub fn web_delete(args: TokenStream, input: TokenStream) -> TokenStream {
117119
#[proc_macro_attribute]
118120
pub fn web_head(args: TokenStream, input: TokenStream) -> TokenStream {
119121
let args = parse_macro_input!(args as syn::AttributeArgs);
120-
let gen = match route::Route::new(args, input, route::GuardType::Head) {
122+
let gen = match route::Route::new(args, input, route::MethodType::Head) {
121123
Ok(gen) => gen,
122124
Err(err) => return err.to_compile_error().into(),
123125
};
@@ -132,7 +134,7 @@ pub fn web_head(args: TokenStream, input: TokenStream) -> TokenStream {
132134
#[proc_macro_attribute]
133135
pub fn web_connect(args: TokenStream, input: TokenStream) -> TokenStream {
134136
let args = parse_macro_input!(args as syn::AttributeArgs);
135-
let gen = match route::Route::new(args, input, route::GuardType::Connect) {
137+
let gen = match route::Route::new(args, input, route::MethodType::Connect) {
136138
Ok(gen) => gen,
137139
Err(err) => return err.to_compile_error().into(),
138140
};
@@ -147,7 +149,7 @@ pub fn web_connect(args: TokenStream, input: TokenStream) -> TokenStream {
147149
#[proc_macro_attribute]
148150
pub fn web_options(args: TokenStream, input: TokenStream) -> TokenStream {
149151
let args = parse_macro_input!(args as syn::AttributeArgs);
150-
let gen = match route::Route::new(args, input, route::GuardType::Options) {
152+
let gen = match route::Route::new(args, input, route::MethodType::Options) {
151153
Ok(gen) => gen,
152154
Err(err) => return err.to_compile_error().into(),
153155
};
@@ -162,7 +164,7 @@ pub fn web_options(args: TokenStream, input: TokenStream) -> TokenStream {
162164
#[proc_macro_attribute]
163165
pub fn web_trace(args: TokenStream, input: TokenStream) -> TokenStream {
164166
let args = parse_macro_input!(args as syn::AttributeArgs);
165-
let gen = match route::Route::new(args, input, route::GuardType::Trace) {
167+
let gen = match route::Route::new(args, input, route::MethodType::Trace) {
166168
Ok(gen) => gen,
167169
Err(err) => return err.to_compile_error().into(),
168170
};
@@ -177,7 +179,7 @@ pub fn web_trace(args: TokenStream, input: TokenStream) -> TokenStream {
177179
#[proc_macro_attribute]
178180
pub fn web_patch(args: TokenStream, input: TokenStream) -> TokenStream {
179181
let args = parse_macro_input!(args as syn::AttributeArgs);
180-
let gen = match route::Route::new(args, input, route::GuardType::Patch) {
182+
let gen = match route::Route::new(args, input, route::MethodType::Patch) {
181183
Ok(gen) => gen,
182184
Err(err) => return err.to_compile_error().into(),
183185
};

ntex-macros/src/route.rs

Lines changed: 38 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use proc_macro::TokenStream;
22
use proc_macro2::{Span, TokenStream as TokenStream2};
33
use quote::{quote, ToTokens, TokenStreamExt};
4-
use syn::{AttributeArgs, Ident, NestedMeta};
4+
use syn::{AttributeArgs, Ident, NestedMeta, Path};
55

66
#[derive(PartialEq)]
7-
pub enum GuardType {
7+
pub enum MethodType {
88
Get,
99
Post,
1010
Put,
@@ -16,23 +16,23 @@ pub enum GuardType {
1616
Patch,
1717
}
1818

19-
impl GuardType {
19+
impl MethodType {
2020
fn as_str(&self) -> &'static str {
2121
match self {
22-
GuardType::Get => "Get",
23-
GuardType::Post => "Post",
24-
GuardType::Put => "Put",
25-
GuardType::Delete => "Delete",
26-
GuardType::Head => "Head",
27-
GuardType::Connect => "Connect",
28-
GuardType::Options => "Options",
29-
GuardType::Trace => "Trace",
30-
GuardType::Patch => "Patch",
22+
MethodType::Get => "Get",
23+
MethodType::Post => "Post",
24+
MethodType::Put => "Put",
25+
MethodType::Delete => "Delete",
26+
MethodType::Head => "Head",
27+
MethodType::Connect => "Connect",
28+
MethodType::Options => "Options",
29+
MethodType::Trace => "Trace",
30+
MethodType::Patch => "Patch",
3131
}
3232
}
3333
}
3434

35-
impl ToTokens for GuardType {
35+
impl ToTokens for MethodType {
3636
fn to_tokens(&self, stream: &mut TokenStream2) {
3737
let ident = self.as_str();
3838
let ident = Ident::new(ident, Span::call_site());
@@ -43,12 +43,14 @@ impl ToTokens for GuardType {
4343
struct Args {
4444
path: syn::LitStr,
4545
guards: Vec<Ident>,
46+
error: Path,
4647
}
4748

4849
impl Args {
4950
fn new(args: AttributeArgs) -> syn::Result<Self> {
5051
let mut path = None;
5152
let mut guards = Vec::new();
53+
let mut error: Option<Path> = None;
5254
for arg in args {
5355
match arg {
5456
NestedMeta::Lit(syn::Lit::Str(lit)) => match path {
@@ -72,10 +74,19 @@ impl Args {
7274
"Attribute guard expects literal string!",
7375
));
7476
}
77+
} else if nv.path.is_ident("error") {
78+
if let syn::Lit::Str(lit) = nv.lit {
79+
error = Some(syn::parse_str(&lit.value())?);
80+
} else {
81+
return Err(syn::Error::new_spanned(
82+
nv.lit,
83+
"Attribute error expects type path!",
84+
));
85+
}
7586
} else {
7687
return Err(syn::Error::new_spanned(
7788
nv.path,
78-
"Unknown attribute key is specified. Allowed: guard",
89+
"Unknown attribute key is specified. Allowed: guard or error",
7990
));
8091
}
8192
}
@@ -87,6 +98,8 @@ impl Args {
8798
Ok(Args {
8899
path: path.unwrap(),
89100
guards,
101+
error: error
102+
.unwrap_or_else(|| syn::parse_str("ntex::web::DefaultError").unwrap()),
90103
})
91104
}
92105
}
@@ -95,21 +108,21 @@ pub struct Route {
95108
name: syn::Ident,
96109
args: Args,
97110
ast: syn::ItemFn,
98-
guard: GuardType,
111+
method: MethodType,
99112
}
100113

101114
impl Route {
102115
pub fn new(
103116
args: AttributeArgs,
104117
input: TokenStream,
105-
guard: GuardType,
118+
method: MethodType,
106119
) -> syn::Result<Self> {
107120
if args.is_empty() {
108121
return Err(syn::Error::new(
109122
Span::call_site(),
110123
format!(
111124
r#"invalid server definition, expected #[{}("<some path>")]"#,
112-
guard.as_str().to_ascii_lowercase()
125+
method.as_str().to_ascii_lowercase()
113126
),
114127
));
115128
}
@@ -121,64 +134,35 @@ impl Route {
121134
name,
122135
args,
123136
ast,
124-
guard,
137+
method,
125138
})
126139
}
127140

128141
pub fn generate(&self) -> TokenStream {
129142
let name = &self.name;
130143
let resource_name = name.to_string();
131-
let guard = &self.guard;
132144
let ast = &self.ast;
133145
let path = &self.args.path;
134146
let extra_guards = &self.args.guards;
135-
136-
let args: Vec<_> = ast
137-
.sig
138-
.inputs
139-
.iter()
140-
.filter(|item| match item {
141-
syn::FnArg::Receiver(_) => false,
142-
syn::FnArg::Typed(_) => true,
143-
})
144-
.map(|item| match item {
145-
syn::FnArg::Receiver(_) => panic!(),
146-
syn::FnArg::Typed(ref pt) => pt.ty.clone(),
147-
})
148-
.collect();
149-
150-
let assert_responder: syn::Path = syn::parse_str(
151-
format!(
152-
"ntex::web::dev::__assert_handler{}",
153-
if args.is_empty() {
154-
"".to_string()
155-
} else {
156-
format!("{}", args.len())
157-
}
158-
)
159-
.as_str(),
160-
)
161-
.unwrap();
147+
let error = &self.args.error;
148+
let method = &self.method;
162149

163150
let stream = quote! {
164151
#[allow(non_camel_case_types)]
165152
pub struct #name;
166153

167-
impl<__E: 'static> ntex::web::dev::HttpServiceFactory<__E> for #name
168-
where __E: ntex::web::error::ErrorRenderer
154+
impl ntex::web::dev::WebServiceFactory<#error> for #name
169155
{
170-
fn register(self, __config: &mut ntex::web::dev::AppService<__E>) {
171-
#(ntex::web::dev::__assert_extractor::<__E, #args>();)*
172-
156+
fn register(self, __config: &mut ntex::web::dev::WebServiceConfig<#error>) {
173157
#ast
174158

175159
let __resource = ntex::web::Resource::new(#path)
176160
.name(#resource_name)
177-
.guard(ntex::web::guard::#guard())
161+
.guard(ntex::web::guard::#method())
178162
#(.guard(ntex::web::guard::fn_guard(#extra_guards)))*
179-
.to(#assert_responder(#name));
163+
.to(#name);
180164

181-
ntex::web::dev::HttpServiceFactory::register(__resource, __config)
165+
ntex::web::dev::WebServiceFactory::register(__resource, __config)
182166
}
183167
}
184168
};

0 commit comments

Comments
 (0)