forked from actix/actix-web
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added proc-macros for route registration
- Loading branch information
Showing
9 changed files
with
221 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
[package] | ||
name = "actix-web-codegen" | ||
description = "Actix web codegen macros" | ||
version = "0.1.0" | ||
authors = ["Nikolay Kim <[email protected]>"] | ||
license = "MIT/Apache-2.0" | ||
edition = "2018" | ||
workspace = ".." | ||
|
||
[lib] | ||
proc-macro = true | ||
|
||
[dependencies] | ||
quote = "0.6" | ||
syn = { version = "0.15", features = ["full", "parsing"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
#![recursion_limit = "512"] | ||
|
||
extern crate proc_macro; | ||
|
||
use proc_macro::TokenStream; | ||
use quote::quote; | ||
use syn::parse_macro_input; | ||
|
||
/// #[get("path")] attribute | ||
#[proc_macro_attribute] | ||
pub fn get(args: TokenStream, input: TokenStream) -> TokenStream { | ||
let args = parse_macro_input!(args as syn::AttributeArgs); | ||
if args.is_empty() { | ||
panic!("invalid server definition, expected: #[get(\"some path\")]"); | ||
} | ||
|
||
// path | ||
let path = match args[0] { | ||
syn::NestedMeta::Literal(syn::Lit::Str(ref fname)) => { | ||
let fname = quote!(#fname).to_string(); | ||
fname.as_str()[1..fname.len() - 1].to_owned() | ||
} | ||
_ => panic!("resource path"), | ||
}; | ||
|
||
let ast: syn::ItemFn = syn::parse(input).unwrap(); | ||
let name = ast.ident.clone(); | ||
|
||
(quote! { | ||
#[allow(non_camel_case_types)] | ||
struct #name; | ||
|
||
impl<P: 'static> actix_web::dev::HttpServiceFactory<P> for #name { | ||
fn register(self, config: &mut actix_web::dev::AppConfig<P>) { | ||
#ast | ||
actix_web::dev::HttpServiceFactory::register( | ||
actix_web::Resource::new(#path) | ||
.route(actix_web::web::get().to(#name)), config); | ||
} | ||
} | ||
}) | ||
.into() | ||
} | ||
|
||
/// #[post("path")] attribute | ||
#[proc_macro_attribute] | ||
pub fn post(args: TokenStream, input: TokenStream) -> TokenStream { | ||
let args = parse_macro_input!(args as syn::AttributeArgs); | ||
if args.is_empty() { | ||
panic!("invalid server definition, expected: #[get(\"some path\")]"); | ||
} | ||
|
||
// path | ||
let path = match args[0] { | ||
syn::NestedMeta::Literal(syn::Lit::Str(ref fname)) => { | ||
let fname = quote!(#fname).to_string(); | ||
fname.as_str()[1..fname.len() - 1].to_owned() | ||
} | ||
_ => panic!("resource path"), | ||
}; | ||
|
||
let ast: syn::ItemFn = syn::parse(input).unwrap(); | ||
let name = ast.ident.clone(); | ||
|
||
(quote! { | ||
#[allow(non_camel_case_types)] | ||
struct #name; | ||
|
||
impl<P: 'static> actix_web::dev::HttpServiceFactory<P> for #name { | ||
fn register(self, config: &mut actix_web::dev::AppConfig<P>) { | ||
#ast | ||
actix_web::dev::HttpServiceFactory::register( | ||
actix_web::Resource::new(#path) | ||
.route(actix_web::web::post().to(#name)), config); | ||
} | ||
} | ||
}) | ||
.into() | ||
} | ||
|
||
/// #[put("path")] attribute | ||
#[proc_macro_attribute] | ||
pub fn put(args: TokenStream, input: TokenStream) -> TokenStream { | ||
let args = parse_macro_input!(args as syn::AttributeArgs); | ||
if args.is_empty() { | ||
panic!("invalid server definition, expected: #[get(\"some path\")]"); | ||
} | ||
|
||
// path | ||
let path = match args[0] { | ||
syn::NestedMeta::Literal(syn::Lit::Str(ref fname)) => { | ||
let fname = quote!(#fname).to_string(); | ||
fname.as_str()[1..fname.len() - 1].to_owned() | ||
} | ||
_ => panic!("resource path"), | ||
}; | ||
|
||
let ast: syn::ItemFn = syn::parse(input).unwrap(); | ||
let name = ast.ident.clone(); | ||
|
||
(quote! { | ||
#[allow(non_camel_case_types)] | ||
struct #name; | ||
|
||
impl<P: 'static> actix_web::dev::HttpServiceFactory<P> for #name { | ||
fn register(self, config: &mut actix_web::dev::AppConfig<P>) { | ||
#ast | ||
actix_web::dev::HttpServiceFactory::register( | ||
actix_web::Resource::new(#path) | ||
.route(actix_web::web::put().to(#name)), config); | ||
} | ||
} | ||
}) | ||
.into() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
use std::collections::HashSet; | ||
use std::env; | ||
use std::fs::File; | ||
use std::io::Read; | ||
use std::path::PathBuf; | ||
|
||
use proc_macro2::TokenStream; | ||
use quote::{quote, ToTokens}; | ||
use syn; | ||
|
||
/// Thrift mux server impl | ||
pub struct Server {} | ||
|
||
impl Server { | ||
fn new() -> Server { | ||
Server {} | ||
} | ||
|
||
/// generate servers | ||
pub fn generate(input: TokenStream) { | ||
let mut srv = Server::new(); | ||
let ast: syn::ItemFn = syn::parse2(input).unwrap(); | ||
println!("T: {:?}", ast.ident); | ||
|
||
// quote! { | ||
// #ast | ||
|
||
// #(#servers)* | ||
// } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.