This repository has been archived by the owner on Jan 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(codegen): add custom attribute to modify the signature of handlers
- Loading branch information
1 parent
f3da082
commit ed92eb7
Showing
10 changed files
with
354 additions
and
3 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
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,14 @@ | ||
[package] | ||
name = "example-codegen" | ||
version = "0.0.0" | ||
authors = ["Yusuke Sasaki <[email protected]>"] | ||
publish = false | ||
|
||
[[bin]] | ||
name = "codegen" | ||
path = "src/main.rs" | ||
doc = false | ||
|
||
[dependencies] | ||
tsukuyomi = { path = "../..", features = ["codegen"] } | ||
futures-await = "0.1.1" |
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,38 @@ | ||
#![feature(proc_macro)] | ||
#![feature(proc_macro_non_items, generators)] // for futures-await | ||
|
||
extern crate futures_await as futures; | ||
extern crate tsukuyomi; | ||
|
||
use futures::prelude::{await, Future}; | ||
use tsukuyomi::prelude::handler; | ||
use tsukuyomi::{App, Error, Input, Handler}; | ||
|
||
#[handler] | ||
fn ready_handler() -> &'static str { | ||
"Hello, Tsukuyomi.\n" | ||
} | ||
|
||
#[handler(async)] | ||
fn async_handler(input: &mut Input) -> impl Future<Item = String, Error = Error> + Send + 'static { | ||
input.body_mut().read_all().convert_to() | ||
} | ||
|
||
#[handler(await)] | ||
fn await_handler() -> tsukuyomi::Result<String> { | ||
let read_all = Input::with_current(|input| input.body_mut().read_all()); | ||
let data: String = await!(read_all.convert_to())?; | ||
Ok(format!("Received: {}", data)) | ||
} | ||
|
||
fn main() -> tsukuyomi::AppResult<()> { | ||
let app = App::builder() | ||
.mount("/", |m| { | ||
m.get("/ready").handle(Handler::new(ready_handler)); | ||
m.post("/async").handle(Handler::new(async_handler)); | ||
m.post("/await").handle(Handler::new(await_handler)); | ||
}) | ||
.finish()?; | ||
|
||
tsukuyomi::run(app) | ||
} |
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,21 @@ | ||
[package] | ||
name = "tsukuyomi-codegen" | ||
version = "0.1.0" | ||
authors = ["Yusuke Sasaki <[email protected]>"] | ||
publish = false | ||
|
||
[lib] | ||
proc-macro = true | ||
|
||
[dependencies] | ||
proc-macro2 = "0.4.6" | ||
syn = { version = "0.14.4", features = ["full", "extra-traits"] } | ||
quote = "0.6.3" | ||
|
||
[dev-dependencies] | ||
tsukuyomi = { version = "0.2.0", path = ".." } | ||
futures = "0.1.21" | ||
futures-await = "0.1.1" | ||
|
||
[features] | ||
nightly = ["proc-macro2/nightly"] |
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,187 @@ | ||
//! Code generation support for Tsukuyomi. | ||
#![feature(proc_macro, use_extern_macros)] | ||
|
||
extern crate proc_macro; | ||
extern crate proc_macro2; | ||
#[macro_use] | ||
extern crate syn; | ||
extern crate quote; | ||
|
||
use proc_macro::TokenStream; | ||
use proc_macro2::Span; | ||
use quote::*; | ||
|
||
macro_rules! try_quote { | ||
($e:expr) => { | ||
match $e { | ||
Ok(v) => v, | ||
Err(e) => { | ||
use proc_macro2::Span; | ||
use quote::*; | ||
let msg = e.to_string(); | ||
return Into::into(quote_spanned!(Span::call_site() => compile_error!(#msg))); | ||
} | ||
} | ||
}; | ||
} | ||
|
||
macro_rules! bail_quote { | ||
($e:expr) => {{ | ||
use proc_macro2::Span; | ||
use quote::*; | ||
let msg = $e.to_string(); | ||
return Into::into(quote_spanned!(Span::call_site() => compile_error!(#msg))); | ||
}}; | ||
($e:expr, $($args:expr),*) => {{ | ||
bail_quote!(format!($e, $($args),*)) | ||
}} | ||
} | ||
|
||
#[derive(Debug, Copy, Clone, PartialEq)] | ||
enum HandlerMode { | ||
Ready, | ||
Async, | ||
AsyncAwait, | ||
} | ||
|
||
impl std::str::FromStr for HandlerMode { | ||
type Err = String; | ||
|
||
fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
match s.trim() { | ||
"" | "ready" => Ok(HandlerMode::Ready), | ||
"async" => Ok(HandlerMode::Async), | ||
"await" => Ok(HandlerMode::AsyncAwait), | ||
s => Err(format!("invalid mode: `{}'", s)), | ||
} | ||
} | ||
} | ||
|
||
fn detect_mode(attr: &TokenStream, _item: &syn::ItemFn) -> Result<HandlerMode, String> { | ||
attr.to_string().parse() | ||
} | ||
|
||
/// Modifies the signature of a free-standing function to a suitable form for using the handler. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ``` | ||
/// # #![feature(proc_macro, use_extern_macros)] | ||
/// # extern crate tsukuyomi; | ||
/// # extern crate tsukuyomi_codegen; | ||
/// # use tsukuyomi_codegen::handler; | ||
/// #[handler] | ||
/// fn handler() -> &'static str { | ||
/// "Hello" | ||
/// } | ||
/// ``` | ||
/// | ||
/// ``` | ||
/// # #![feature(proc_macro, use_extern_macros)] | ||
/// # extern crate tsukuyomi; | ||
/// # extern crate tsukuyomi_codegen; | ||
/// # use tsukuyomi_codegen::handler; | ||
/// # use tsukuyomi::Input; | ||
/// #[handler] | ||
/// fn handler(input: &mut Input) -> String { | ||
/// format!("path = {:?}", input.uri().path()) | ||
/// } | ||
/// ``` | ||
/// | ||
/// ``` | ||
/// # #![feature(proc_macro, use_extern_macros)] | ||
/// # extern crate tsukuyomi; | ||
/// # extern crate tsukuyomi_codegen; | ||
/// # extern crate futures; | ||
/// # use tsukuyomi_codegen::handler; | ||
/// # use tsukuyomi::{Input, Error}; | ||
/// # use futures::Future; | ||
/// #[handler(async)] | ||
/// fn handler(input: &mut Input) -> impl Future<Item = String, Error = Error> + Send + 'static { | ||
/// input.body_mut().read_all().convert_to() | ||
/// } | ||
/// ``` | ||
/// | ||
/// ``` | ||
/// # #![feature(proc_macro, use_extern_macros, proc_macro_non_items, generators)] | ||
/// # extern crate tsukuyomi; | ||
/// # extern crate tsukuyomi_codegen; | ||
/// # extern crate futures_await as futures; | ||
/// # use tsukuyomi_codegen::handler; | ||
/// # use tsukuyomi::Error; | ||
/// #[handler(await)] | ||
/// fn handler() -> Result<&'static str, Error> { | ||
/// Ok("Hello") | ||
/// } | ||
/// ``` | ||
#[proc_macro_attribute] | ||
pub fn handler(attr: TokenStream, item: TokenStream) -> TokenStream { | ||
let item: syn::ItemFn = try_quote!(syn::parse(item)); | ||
|
||
// FIXME: detect the keyword `async` | ||
let mode = try_quote!(detect_mode(&attr, &item)); | ||
|
||
let num_args = item.decl.inputs.iter().count(); | ||
if num_args > 1 { | ||
bail_quote!("Too many arguments"); | ||
} | ||
if mode == HandlerMode::AsyncAwait && num_args != 0 { | ||
bail_quote!("The number of arguments in #[async] handler must be zero."); | ||
} | ||
|
||
let mut inner = item.clone(); | ||
inner.ident = syn::Ident::new("inner", inner.ident.span()); | ||
if mode == HandlerMode::AsyncAwait { | ||
inner.attrs.push(parse_quote!(#[async])); | ||
} | ||
|
||
let mut new_item = item.clone(); | ||
let input_ident: syn::Ident = if num_args == 0 && mode != HandlerMode::Ready { | ||
syn::Ident::new("_input", Span::call_site()) | ||
} else { | ||
syn::Ident::new("input", Span::call_site()) | ||
}; | ||
new_item.decl.inputs = Some(syn::punctuated::Pair::End(parse_quote!( | ||
#input_ident: &mut ::tsukuyomi::input::Input | ||
))).into_iter() | ||
.collect(); | ||
match new_item.decl.output { | ||
syn::ReturnType::Default => bail_quote!("unimplemented"), | ||
syn::ReturnType::Type(_, ref mut ty) => { | ||
*ty = Box::new(parse_quote!(::tsukuyomi::handler::Handle)); | ||
} | ||
} | ||
new_item.block = { | ||
let call: syn::Expr = match num_args { | ||
0 => parse_quote!(inner()), | ||
1 => parse_quote!(inner(input)), | ||
_ => unreachable!(), | ||
}; | ||
|
||
let body: syn::Expr = match mode { | ||
HandlerMode::Ready => parse_quote!({ | ||
::tsukuyomi::handler::Handle::ready( | ||
::tsukuyomi::output::Responder::respond_to(#call, input) | ||
) | ||
}), | ||
HandlerMode::Async | HandlerMode::AsyncAwait => parse_quote!({ | ||
::tsukuyomi::handler::Handle::async_responder(#call) | ||
}), | ||
}; | ||
|
||
let prelude: Option<syn::Stmt> = if mode == HandlerMode::AsyncAwait { | ||
Some(parse_quote!(use futures::prelude::async;)) | ||
} else { | ||
None | ||
}; | ||
|
||
Box::new(parse_quote!({ | ||
#prelude | ||
#inner | ||
#body | ||
})) | ||
}; | ||
|
||
quote!(#new_item).into() | ||
} |
Oops, something went wrong.