mirror of https://github.com/rwf2/Rocket.git
Update codegen for 2018-04-06 nightly.
This commit is contained in:
parent
e860b0e261
commit
5b4a35d508
|
@ -8,7 +8,7 @@ use yansi::Color::{Red, Yellow, Blue, White};
|
||||||
use version_check::{supports_features, is_min_version, is_min_date};
|
use version_check::{supports_features, is_min_version, is_min_date};
|
||||||
|
|
||||||
// Specifies the minimum nightly version needed to compile Rocket's codegen.
|
// Specifies the minimum nightly version needed to compile Rocket's codegen.
|
||||||
const MIN_DATE: &'static str = "2018-04-03";
|
const MIN_DATE: &'static str = "2018-04-06";
|
||||||
const MIN_VERSION: &'static str = "1.27.0-nightly";
|
const MIN_VERSION: &'static str = "1.27.0-nightly";
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
|
|
@ -16,7 +16,7 @@ pub use self::uri::{uri, uri_internal};
|
||||||
pub fn prefix_path(prefix: &str, path: &mut Path) {
|
pub fn prefix_path(prefix: &str, path: &mut Path) {
|
||||||
let last = path.segments.len() - 1;
|
let last = path.segments.len() - 1;
|
||||||
let last_seg = &mut path.segments[last];
|
let last_seg = &mut path.segments[last];
|
||||||
last_seg.identifier = last_seg.identifier.prepend(prefix);
|
last_seg.ident = last_seg.ident.prepend(prefix);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
|
|
|
@ -1,19 +1,19 @@
|
||||||
use std::fmt::Display;
|
use std::fmt::Display;
|
||||||
|
|
||||||
use URI_INFO_MACRO_PREFIX;
|
|
||||||
use super::prefix_path;
|
|
||||||
use utils::{SpanExt, IdentExt, split_idents, ExprExt};
|
|
||||||
|
|
||||||
use parser::{UriParams, InternalUriParams, Validation};
|
|
||||||
|
|
||||||
use syntax::codemap::Span;
|
use syntax::codemap::Span;
|
||||||
use syntax::ext::base::{DummyResult, ExtCtxt, MacEager, MacResult};
|
use syntax::ext::base::{DummyResult, ExtCtxt, MacEager, MacResult};
|
||||||
use syntax::tokenstream::{TokenStream, TokenTree};
|
use syntax::tokenstream::{TokenStream, TokenTree};
|
||||||
use syntax::ast::{self, Ident};
|
use syntax::ast::{self, Ident};
|
||||||
|
use syntax::symbol::Symbol;
|
||||||
use syntax::parse::PResult;
|
use syntax::parse::PResult;
|
||||||
use syntax::ext::build::AstBuilder;
|
use syntax::ext::build::AstBuilder;
|
||||||
use syntax::ptr::P;
|
use syntax::ptr::P;
|
||||||
|
|
||||||
|
use URI_INFO_MACRO_PREFIX;
|
||||||
|
use super::prefix_path;
|
||||||
|
use utils::{IdentExt, split_idents, ExprExt};
|
||||||
|
use parser::{UriParams, InternalUriParams, Validation};
|
||||||
|
|
||||||
pub fn uri(
|
pub fn uri(
|
||||||
ecx: &mut ExtCtxt,
|
ecx: &mut ExtCtxt,
|
||||||
sp: Span,
|
sp: Span,
|
||||||
|
@ -125,14 +125,15 @@ pub fn uri_internal(
|
||||||
|
|
||||||
// Now the user's parameters.
|
// Now the user's parameters.
|
||||||
// Building <$T as ::rocket::http::uri::FromUriParam<_>>::from_uri_param($e).
|
// Building <$T as ::rocket::http::uri::FromUriParam<_>>::from_uri_param($e).
|
||||||
for (i, &(ident, ref ty)) in internal.fn_args.iter().enumerate() {
|
for (i, &(mut ident, ref ty)) in internal.fn_args.iter().enumerate() {
|
||||||
let (span, mut expr) = (exprs[i].span, exprs[i].clone());
|
let (span, mut expr) = (exprs[i].span, exprs[i].clone());
|
||||||
|
ident.span = span;
|
||||||
|
|
||||||
// path for call: <T as FromUriParam<_>>::from_uri_param
|
// path for call: <T as FromUriParam<_>>::from_uri_param
|
||||||
let idents = split_idents("rocket::http::uri::FromUriParam");
|
let idents = split_idents("rocket::http::uri::FromUriParam");
|
||||||
let generics = vec![ecx.ty(span, ast::TyKind::Infer)];
|
let generics = vec![ecx.ty(span, ast::TyKind::Infer)];
|
||||||
let trait_path = ecx.path_all(span, true, idents, vec![], generics, vec![]);
|
let trait_path = ecx.path_all(span, true, idents, vec![], generics, vec![]);
|
||||||
let method = span.wrap(Ident::from_str("from_uri_param"));
|
let method = Ident::new(Symbol::intern("from_uri_param"), span);
|
||||||
let (qself, path) = ecx.qpath(ty.clone(), trait_path, method);
|
let (qself, path) = ecx.qpath(ty.clone(), trait_path, method);
|
||||||
|
|
||||||
// replace &expr with [let tmp = expr; &tmp] so that borrows of
|
// replace &expr with [let tmp = expr; &tmp] so that borrows of
|
||||||
|
@ -142,7 +143,7 @@ pub fn uri_internal(
|
||||||
if let ast::ExprKind::AddrOf(_, inner) = cloned_expr.node {
|
if let ast::ExprKind::AddrOf(_, inner) = cloned_expr.node {
|
||||||
// Only reassign temporary expressions, not locations.
|
// Only reassign temporary expressions, not locations.
|
||||||
if !inner.is_location() {
|
if !inner.is_location() {
|
||||||
let tmp_ident = ident.node.append("_tmp");
|
let tmp_ident = ident.append("_tmp");
|
||||||
let tmp_stmt = ecx.stmt_let(span, false, tmp_ident, inner);
|
let tmp_stmt = ecx.stmt_let(span, false, tmp_ident, inner);
|
||||||
argument_stmts.push(tmp_stmt);
|
argument_stmts.push(tmp_stmt);
|
||||||
expr = ecx.expr_ident(span, tmp_ident);
|
expr = ecx.expr_ident(span, tmp_ident);
|
||||||
|
@ -152,7 +153,7 @@ pub fn uri_internal(
|
||||||
// generating: let $ident = path($expr);
|
// generating: let $ident = path($expr);
|
||||||
let path_expr = ecx.expr_qpath(span, qself, path);
|
let path_expr = ecx.expr_qpath(span, qself, path);
|
||||||
let call = ecx.expr_call(span, path_expr, vec![expr]);
|
let call = ecx.expr_call(span, path_expr, vec![expr]);
|
||||||
let stmt = ecx.stmt_let(span, false, ident.node, call);
|
let stmt = ecx.stmt_let(span, false, ident, call);
|
||||||
debug!("Emitting URI typecheck statement: {:?}", stmt);
|
debug!("Emitting URI typecheck statement: {:?}", stmt);
|
||||||
argument_stmts.push(stmt);
|
argument_stmts.push(stmt);
|
||||||
|
|
||||||
|
@ -163,8 +164,11 @@ pub fn uri_internal(
|
||||||
format_assign_tokens.push(tokens);
|
format_assign_tokens.push(tokens);
|
||||||
}
|
}
|
||||||
|
|
||||||
MacEager::expr(quote_expr!(ecx, {
|
let expr = quote_expr!(ecx, {
|
||||||
$argument_stmts
|
$argument_stmts
|
||||||
::rocket::http::uri::Uri::from(format!($fmt_string, $format_assign_tokens))
|
::rocket::http::uri::Uri::from(format!($fmt_string, $format_assign_tokens))
|
||||||
}))
|
});
|
||||||
|
|
||||||
|
debug!("Emitting URI expression: {:?}", expr);
|
||||||
|
MacEager::expr(expr)
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,7 @@ use syntax::ast::*;
|
||||||
use syntax::ext::base::{ExtCtxt, Annotatable};
|
use syntax::ext::base::{ExtCtxt, Annotatable};
|
||||||
use syntax::codemap::{Span, Spanned, dummy_spanned};
|
use syntax::codemap::{Span, Spanned, dummy_spanned};
|
||||||
|
|
||||||
use utils::{span, MetaItemExt, SpanExt, is_valid_ident};
|
use utils::{MetaItemExt, SpanExt, span, is_valid_ident};
|
||||||
use super::Function;
|
use super::Function;
|
||||||
use super::keyvalue::KVSpanned;
|
use super::keyvalue::KVSpanned;
|
||||||
use super::uri::validate_uri;
|
use super::uri::validate_uri;
|
||||||
|
@ -169,13 +169,13 @@ fn parse_method(ecx: &ExtCtxt, meta_item: &NestedMetaItem) -> Spanned<Method> {
|
||||||
`HEAD`, `PATCH`, `OPTIONS`";
|
`HEAD`, `PATCH`, `OPTIONS`";
|
||||||
|
|
||||||
if let Some(word) = meta_item.word() {
|
if let Some(word) = meta_item.word() {
|
||||||
if let Ok(method) = Method::from_str(&word.name().as_str()) {
|
if let Ok(method) = Method::from_str(&word.ident.name.as_str()) {
|
||||||
if is_valid_method(method) {
|
if is_valid_method(method) {
|
||||||
return span(method, word.span());
|
return span(method, word.span());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let msg = format!("'{}' is not a valid method", word.name());
|
let msg = format!("'{}' is not a valid method", word.ident);
|
||||||
ecx.struct_span_err(word.span, &msg).help(valid_methods).emit();
|
ecx.struct_span_err(word.span, &msg).help(valid_methods).emit();
|
||||||
return default_method;
|
return default_method;
|
||||||
}
|
}
|
||||||
|
|
|
@ -35,7 +35,7 @@ pub struct UriParams {
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct InternalUriParams {
|
pub struct InternalUriParams {
|
||||||
pub uri: Spanned<String>,
|
pub uri: Spanned<String>,
|
||||||
pub fn_args: Vec<(Spanned<ast::Ident>, P<ast::Ty>)>,
|
pub fn_args: Vec<(ast::Ident, P<ast::Ty>)>,
|
||||||
pub uri_params: UriParams,
|
pub uri_params: UriParams,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -208,7 +208,7 @@ impl InternalUriParams {
|
||||||
|
|
||||||
pub fn fn_args_str(&self) -> String {
|
pub fn fn_args_str(&self) -> String {
|
||||||
self.fn_args.iter()
|
self.fn_args.iter()
|
||||||
.map(|&(ident, ref ty)| format!("{}: {}", ident.node, ty_to_string(&ty)))
|
.map(|&(ident, ref ty)| format!("{}: {}", ident.name, ty_to_string(&ty)))
|
||||||
.collect::<Vec<_>>()
|
.collect::<Vec<_>>()
|
||||||
.join(", ")
|
.join(", ")
|
||||||
}
|
}
|
||||||
|
@ -225,7 +225,7 @@ impl InternalUriParams {
|
||||||
Some(Spanned { node: Args::Unnamed(ref args), .. }) => unnamed(args),
|
Some(Spanned { node: Args::Unnamed(ref args), .. }) => unnamed(args),
|
||||||
Some(Spanned { node: Args::Named(ref args), .. }) => {
|
Some(Spanned { node: Args::Named(ref args), .. }) => {
|
||||||
let mut params: IndexMap<Name, Option<P<Expr>>> = self.fn_args.iter()
|
let mut params: IndexMap<Name, Option<P<Expr>>> = self.fn_args.iter()
|
||||||
.map(|&(ident, _)| (ident.node.name, None))
|
.map(|&(ident, _)| (ident.name, None))
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
let (mut extra, mut dup) = (vec![], vec![]);
|
let (mut extra, mut dup) = (vec![], vec![]);
|
||||||
|
|
|
@ -15,7 +15,7 @@ pub trait ArgExt {
|
||||||
impl ArgExt for Arg {
|
impl ArgExt for Arg {
|
||||||
fn ident(&self) -> Option<&Ident> {
|
fn ident(&self) -> Option<&Ident> {
|
||||||
match self.pat.node {
|
match self.pat.node {
|
||||||
PatKind::Ident(_, ref ident, _) => Some(&ident.node),
|
PatKind::Ident(_, ref ident, _) => Some(&ident),
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
use std::fmt::Display;
|
use std::fmt::Display;
|
||||||
use syntax::ast::Ident;
|
use syntax::ast::Ident;
|
||||||
|
use syntax::symbol::Symbol;
|
||||||
|
|
||||||
pub trait IdentExt {
|
pub trait IdentExt {
|
||||||
fn prepend<T: Display>(&self, other: T) -> Ident;
|
fn prepend<T: Display>(&self, other: T) -> Ident;
|
||||||
|
@ -9,11 +10,11 @@ pub trait IdentExt {
|
||||||
impl IdentExt for Ident {
|
impl IdentExt for Ident {
|
||||||
fn prepend<T: Display>(&self, other: T) -> Ident {
|
fn prepend<T: Display>(&self, other: T) -> Ident {
|
||||||
let new_ident = format!("{}{}", other, self.name);
|
let new_ident = format!("{}{}", other, self.name);
|
||||||
Ident::from_str(new_ident.as_str())
|
Ident::new(Symbol::intern(&new_ident), self.span)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn append<T: Display>(&self, other: T) -> Ident {
|
fn append<T: Display>(&self, other: T) -> Ident {
|
||||||
let new_ident = format!("{}{}", self.name, other);
|
let new_ident = format!("{}{}", self.name, other);
|
||||||
Ident::from_str(new_ident.as_str())
|
Ident::new(Symbol::intern(&new_ident), self.span)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,7 +10,7 @@ pub trait MetaItemExt {
|
||||||
impl MetaItemExt for NestedMetaItem {
|
impl MetaItemExt for NestedMetaItem {
|
||||||
fn name_value(&self) -> Option<(&Symbol, &Lit)> {
|
fn name_value(&self) -> Option<(&Symbol, &Lit)> {
|
||||||
self.meta_item().and_then(|mi| match mi.node {
|
self.meta_item().and_then(|mi| match mi.node {
|
||||||
MetaItemKind::NameValue(ref l) => Some((&mi.name, l)),
|
MetaItemKind::NameValue(ref l) => Some((&mi.ident.name, l)),
|
||||||
_ => None,
|
_ => None,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +1,8 @@
|
||||||
use super::SpanExt;
|
|
||||||
|
|
||||||
use syntax::parse::parser::{PathStyle, Parser};
|
use syntax::parse::parser::{PathStyle, Parser};
|
||||||
use syntax::parse::PResult;
|
use syntax::parse::PResult;
|
||||||
use syntax::ast::{self, Path, StrStyle, Ident};
|
use syntax::ast::{self, Path, StrStyle, Ident};
|
||||||
use syntax::parse::token::Token::{Eof, Comma};
|
use syntax::parse::token::Token::{Eof, Comma};
|
||||||
use syntax::parse::common::SeqSep;
|
use syntax::parse::common::SeqSep;
|
||||||
use syntax::codemap::Spanned;
|
|
||||||
use syntax::symbol::Symbol;
|
use syntax::symbol::Symbol;
|
||||||
|
|
||||||
pub trait ParserExt<'a> {
|
pub trait ParserExt<'a> {
|
||||||
|
@ -16,7 +13,7 @@ pub trait ParserExt<'a> {
|
||||||
fn parse_str_lit(&mut self) -> PResult<'a, (Symbol, StrStyle)>;
|
fn parse_str_lit(&mut self) -> PResult<'a, (Symbol, StrStyle)>;
|
||||||
|
|
||||||
// Like `parse_ident` but also looks for an `ident` in a `Pat`.
|
// Like `parse_ident` but also looks for an `ident` in a `Pat`.
|
||||||
fn parse_ident_inc_pat(&mut self) -> PResult<'a, Spanned<Ident>>;
|
fn parse_ident_inc_pat(&mut self) -> PResult<'a, Ident>;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> ParserExt<'a> for Parser<'a> {
|
impl<'a> ParserExt<'a> for Parser<'a> {
|
||||||
|
@ -43,9 +40,8 @@ impl<'a> ParserExt<'a> for Parser<'a> {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_ident_inc_pat(&mut self) -> PResult<'a, Spanned<Ident>> {
|
fn parse_ident_inc_pat(&mut self) -> PResult<'a, Ident> {
|
||||||
self.parse_ident()
|
self.parse_ident()
|
||||||
.map(|ident| self.prev_span.wrap(ident))
|
|
||||||
.or_else(|mut e| {
|
.or_else(|mut e| {
|
||||||
let pat = self.parse_pat().map_err(|i| { e.cancel(); i })?;
|
let pat = self.parse_pat().map_err(|i| { e.cancel(); i })?;
|
||||||
let ident = match pat.node {
|
let ident = match pat.node {
|
||||||
|
|
|
@ -39,21 +39,21 @@ impl<'a, 'r> FromRequest<'a, 'r> for User {
|
||||||
fn login(mut cookies: Cookies, login: Form<Login>) -> Flash<Redirect> {
|
fn login(mut cookies: Cookies, login: Form<Login>) -> Flash<Redirect> {
|
||||||
if login.get().username == "Sergio" && login.get().password == "password" {
|
if login.get().username == "Sergio" && login.get().password == "password" {
|
||||||
cookies.add_private(Cookie::new("user_id", 1.to_string()));
|
cookies.add_private(Cookie::new("user_id", 1.to_string()));
|
||||||
Flash::success(Redirect::to("/"), "Successfully logged in.")
|
Flash::success(Redirect::to(uri!(index)), "Successfully logged in.")
|
||||||
} else {
|
} else {
|
||||||
Flash::error(Redirect::to("/login"), "Invalid username/password.")
|
Flash::error(Redirect::to(uri!(login_page)), "Invalid username/password.")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[post("/logout")]
|
#[post("/logout")]
|
||||||
fn logout(mut cookies: Cookies) -> Flash<Redirect> {
|
fn logout(mut cookies: Cookies) -> Flash<Redirect> {
|
||||||
cookies.remove_private(Cookie::named("user_id"));
|
cookies.remove_private(Cookie::named("user_id"));
|
||||||
Flash::success(Redirect::to("/login"), "Successfully logged out.")
|
Flash::success(Redirect::to(uri!(login_page)), "Successfully logged out.")
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/login")]
|
#[get("/login")]
|
||||||
fn login_user(_user: User) -> Redirect {
|
fn login_user(_user: User) -> Redirect {
|
||||||
Redirect::to("/")
|
Redirect::to(uri!(index))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/login", rank = 2)]
|
#[get("/login", rank = 2)]
|
||||||
|
@ -75,7 +75,7 @@ fn user_index(user: User) -> Template {
|
||||||
|
|
||||||
#[get("/", rank = 2)]
|
#[get("/", rank = 2)]
|
||||||
fn index() -> Redirect {
|
fn index() -> Redirect {
|
||||||
Redirect::to("/login")
|
Redirect::to(uri!(login_page))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn rocket() -> rocket::Rocket {
|
fn rocket() -> rocket::Rocket {
|
||||||
|
|
|
@ -22,7 +22,7 @@ struct TemplateContext {
|
||||||
|
|
||||||
#[get("/")]
|
#[get("/")]
|
||||||
fn index() -> Redirect {
|
fn index() -> Redirect {
|
||||||
Redirect::to("/hello/Unknown")
|
Redirect::to(uri!(get: name = "Unknown"))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/hello/<name>")]
|
#[get("/hello/<name>")]
|
||||||
|
|
Loading…
Reference in New Issue