Skip to content

Commit

Permalink
Migrate codegen to Rust 2018.
Browse files Browse the repository at this point in the history
  • Loading branch information
jebrosen authored and SergioBenitez committed Jun 25, 2019
1 parent 34cb1c1 commit be784a7
Show file tree
Hide file tree
Showing 22 changed files with 110 additions and 115 deletions.
1 change: 1 addition & 0 deletions core/codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ readme = "../../README.md"
keywords = ["rocket", "web", "framework", "code", "generation"]
license = "MIT/Apache-2.0"
build = "build.rs"
edition = "2018"

[lib]
proc-macro = true
Expand Down
3 changes: 0 additions & 3 deletions core/codegen/build.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
//! Ensures Rocket isn't compiled with an incompatible version of Rust.
extern crate yansi;
extern crate version_check;

use yansi::{Paint, Color::{Red, Yellow, Blue}};

// Specifies the minimum nightly version needed to compile Rocket.
Expand Down
8 changes: 4 additions & 4 deletions core/codegen/src/attribute/catch.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use proc_macro::{TokenStream, Span};
use devise::{syn, Spanned, Result, FromMeta};
use proc_macro2::TokenStream as TokenStream2;
use crate::proc_macro2::TokenStream as TokenStream2;

use http_codegen::Status;
use syn_ext::{syn_to_diag, IdentExt, ReturnTypeExt};
use crate::http_codegen::Status;
use crate::syn_ext::{syn_to_diag, IdentExt, ReturnTypeExt};
use self::syn::{Attribute, parse::Parser};
use {CATCH_FN_PREFIX, CATCH_STRUCT_PREFIX};
use crate::{CATCH_FN_PREFIX, CATCH_STRUCT_PREFIX};

/// The raw, parsed `#[catch(code)]` attribute.
#[derive(Debug, FromMeta)]
Expand Down
16 changes: 8 additions & 8 deletions core/codegen/src/attribute/route.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use proc_macro::{TokenStream, Span};
use proc_macro2::TokenStream as TokenStream2;
use crate::proc_macro2::TokenStream as TokenStream2;
use devise::{syn, Spanned, SpanWrapped, Result, FromMeta, ext::TypeExt};
use indexmap::IndexSet;

use proc_macro_ext::{Diagnostics, StringLit};
use syn_ext::{syn_to_diag, IdentExt};
use crate::proc_macro_ext::{Diagnostics, StringLit};
use crate::syn_ext::{syn_to_diag, IdentExt};
use self::syn::{Attribute, parse::Parser};

use http_codegen::{Method, MediaType, RoutePath, DataSegment, Optional};
use attribute::segments::{Source, Kind, Segment};
use {ROUTE_FN_PREFIX, ROUTE_STRUCT_PREFIX, URI_MACRO_PREFIX, ROCKET_PARAM_PREFIX};
use crate::http_codegen::{Method, MediaType, RoutePath, DataSegment, Optional};
use crate::attribute::segments::{Source, Kind, Segment};
use crate::{ROUTE_FN_PREFIX, ROUTE_STRUCT_PREFIX, URI_MACRO_PREFIX, ROCKET_PARAM_PREFIX};

/// The raw, parsed `#[route]` attribute.
#[derive(Debug, FromMeta)]
Expand Down Expand Up @@ -425,7 +425,7 @@ fn complete_route(args: TokenStream2, input: TokenStream) -> Result<TokenStream>
}

fn incomplete_route(
method: ::http::Method,
method: crate::http::Method,
args: TokenStream2,
input: TokenStream
) -> Result<TokenStream> {
Expand Down Expand Up @@ -459,7 +459,7 @@ fn incomplete_route(
codegen_route(parse_route(attribute, function)?)
}

pub fn route_attribute<M: Into<Option<::http::Method>>>(
pub fn route_attribute<M: Into<Option<crate::http::Method>>>(
method: M,
args: TokenStream,
input: TokenStream
Expand Down
18 changes: 9 additions & 9 deletions core/codegen/src/attribute/segments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ use std::hash::{Hash, Hasher};
use devise::syn;
use proc_macro::{Span, Diagnostic};

use http::uri::{UriPart, Path};
use http::route::RouteSegment;
use proc_macro_ext::{Diagnostics, StringLit, PResult, DResult};
use crate::http::uri::{UriPart, Path};
use crate::http::route::RouteSegment;
use crate::proc_macro_ext::{Diagnostics, StringLit, PResult, DResult};

crate use http::route::{Error, Kind, Source};
crate use crate::http::route::{Error, Kind, Source};

#[derive(Debug, Clone)]
crate struct Segment {
Expand All @@ -19,7 +19,7 @@ crate struct Segment {
}

impl Segment {
fn from<P: UriPart>(segment: RouteSegment<P>, span: Span) -> Segment {
fn from<P: UriPart>(segment: RouteSegment<'_, P>, span: Span) -> Segment {
let source = match P::DELIMITER {
'/' => Source::Path,
'&' => Source::Query,
Expand All @@ -31,7 +31,7 @@ impl Segment {
}
}

impl<'a> From<&'a syn::Ident> for Segment {
impl From<&syn::Ident> for Segment {
fn from(ident: &syn::Ident) -> Segment {
Segment {
kind: Kind::Static,
Expand Down Expand Up @@ -76,7 +76,7 @@ fn into_diagnostic(
segment: &str, // The segment that failed.
source: &str, // The haystack where `segment` can be found.
span: Span, // The `Span` of `Source`.
error: &Error, // The error.
error: &Error<'_>, // The error.
) -> Diagnostic {
let seg_span = subspan(segment, source, span);
match error {
Expand Down Expand Up @@ -116,7 +116,7 @@ fn into_diagnostic(
}

crate fn parse_data_segment(segment: &str, span: Span) -> PResult<Segment> {
<RouteSegment<Path>>::parse_one(segment)
<RouteSegment<'_, Path>>::parse_one(segment)
.map(|segment| {
let mut seg = Segment::from(segment, span);
seg.source = Source::Data;
Expand All @@ -133,7 +133,7 @@ crate fn parse_segments<P: UriPart>(
let mut segments = vec![];
let mut diags = Diagnostics::new();

for result in <RouteSegment<P>>::parse_many(string) {
for result in <RouteSegment<'_, P>>::parse_many(string) {
if let Err((segment_string, error)) = result {
diags.push(into_diagnostic(segment_string, string, span, &error));
if let Error::Trailing(..) = error {
Expand Down
6 changes: 3 additions & 3 deletions core/codegen/src/bang/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use proc_macro::TokenStream;
use proc_macro2::TokenStream as TokenStream2;
use crate::proc_macro2::TokenStream as TokenStream2;

use devise::{syn, Spanned, Result};
use self::syn::{Path, punctuated::Punctuated, parse::Parser, token::Comma};
use syn_ext::{IdentExt, syn_to_diag};
use {ROUTE_STRUCT_PREFIX, CATCH_STRUCT_PREFIX};
use crate::syn_ext::{IdentExt, syn_to_diag};
use crate::{ROUTE_STRUCT_PREFIX, CATCH_STRUCT_PREFIX};

mod uri;
mod uri_parsing;
Expand Down
22 changes: 11 additions & 11 deletions core/codegen/src/bang/uri.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
use std::fmt::Display;
use proc_macro::TokenStream;
use proc_macro2::TokenStream as TokenStream2;
use crate::proc_macro2::TokenStream as TokenStream2;

use devise::{syn, Result};
use devise::syn::{Expr, Ident, Type, spanned::Spanned};
use http::{uri::{Origin, Path, Query}, ext::IntoOwned};
use http::route::{RouteSegment, Kind, Source};
use crate::http::{uri::{Origin, Path, Query}, ext::IntoOwned};
use crate::http::route::{RouteSegment, Kind, Source};

use http_codegen::Optional;
use syn_ext::{IdentExt, syn_to_diag};
use bang::{prefix_last_segment, uri_parsing::*};
use crate::http_codegen::Optional;
use crate::syn_ext::{IdentExt, syn_to_diag};
use crate::bang::{prefix_last_segment, uri_parsing::*};

use URI_MACRO_PREFIX;
use crate::URI_MACRO_PREFIX;

macro_rules! p {
(@go $num:expr, $singular:expr, $plural:expr) => (
Expand Down Expand Up @@ -122,7 +122,7 @@ fn add_binding(to: &mut Vec<TokenStream2>, ident: &Ident, ty: &Type, expr: &Expr
}

fn explode_path<'a, I: Iterator<Item = (&'a Ident, &'a Type, &'a Expr)>>(
uri: &Origin,
uri: &Origin<'_>,
bindings: &mut Vec<TokenStream2>,
mut items: I
) -> TokenStream2 {
Expand All @@ -132,7 +132,7 @@ fn explode_path<'a, I: Iterator<Item = (&'a Ident, &'a Type, &'a Expr)>>(
}

let uri_display = quote!(#uri_mod::UriDisplay<#uri_mod::Path>);
let dyn_exprs = <RouteSegment<Path>>::parse(uri).map(|segment| {
let dyn_exprs = <RouteSegment<'_, Path>>::parse(uri).map(|segment| {
let segment = segment.expect("segment okay; prechecked on parse");
match segment.kind {
Kind::Static => {
Expand All @@ -151,7 +151,7 @@ fn explode_path<'a, I: Iterator<Item = (&'a Ident, &'a Type, &'a Expr)>>(
}

fn explode_query<'a, I: Iterator<Item = (&'a Ident, &'a Type, &'a ArgExpr)>>(
uri: &Origin,
uri: &Origin<'_>,
bindings: &mut Vec<TokenStream2>,
mut items: I
) -> Option<TokenStream2> {
Expand All @@ -162,7 +162,7 @@ fn explode_query<'a, I: Iterator<Item = (&'a Ident, &'a Type, &'a ArgExpr)>>(

let query_arg = quote!(#uri_mod::UriQueryArgument);
let uri_display = quote!(#uri_mod::UriDisplay<#uri_mod::Query>);
let dyn_exprs = <RouteSegment<Query>>::parse(uri)?.filter_map(|segment| {
let dyn_exprs = <RouteSegment<'_, Query>>::parse(uri)?.filter_map(|segment| {
let segment = segment.expect("segment okay; prechecked on parse");
if segment.kind == Kind::Static {
let string = &segment.string;
Expand Down
14 changes: 7 additions & 7 deletions core/codegen/src/bang/uri_parsing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use self::syn::{Expr, Ident, LitStr, Path, Token, Type};
use self::syn::parse::{self, Parse, ParseStream};
use self::syn::punctuated::Punctuated;

use http::{uri::Origin, ext::IntoOwned};
use crate::http::{uri::Origin, ext::IntoOwned};
use indexmap::IndexMap;

#[derive(Debug)]
Expand Down Expand Up @@ -79,7 +79,7 @@ pub struct InternalUriParams {
}

impl Parse for ArgExpr {
fn parse(input: ParseStream) -> parse::Result<Self> {
fn parse(input: ParseStream<'_>) -> parse::Result<Self> {
if input.peek(Token![_]) {
return Ok(ArgExpr::Ignored(input.parse::<Token![_]>()?));
}
Expand All @@ -89,7 +89,7 @@ impl Parse for ArgExpr {
}

impl Parse for Arg {
fn parse(input: ParseStream) -> parse::Result<Self> {
fn parse(input: ParseStream<'_>) -> parse::Result<Self> {
let has_key = input.peek2(Token![=]);
if has_key {
let ident = input.parse::<Ident>()?;
Expand All @@ -109,7 +109,7 @@ fn err<T, S: AsRef<str>>(span: Span, s: S) -> parse::Result<T> {

impl Parse for UriParams {
// Parses the mount point, if any, route identifier, and arguments.
fn parse(input: ParseStream) -> parse::Result<Self> {
fn parse(input: ParseStream<'_>) -> parse::Result<Self> {
if input.is_empty() {
return Err(input.error("call to `uri!` cannot be empty"));
}
Expand Down Expand Up @@ -176,7 +176,7 @@ impl Parse for UriParams {
}

impl Parse for FnArg {
fn parse(input: ParseStream) -> parse::Result<FnArg> {
fn parse(input: ParseStream<'_>) -> parse::Result<FnArg> {
let ident = input.parse::<Ident>()?;
input.parse::<Token![:]>()?;
let mut ty = input.parse::<Type>()?;
Expand All @@ -186,7 +186,7 @@ impl Parse for FnArg {
}

impl Parse for InternalUriParams {
fn parse(input: ParseStream) -> parse::Result<InternalUriParams> {
fn parse(input: ParseStream<'_>) -> parse::Result<InternalUriParams> {
let route_uri_str = input.parse::<LitStr>()?;
input.parse::<Token![,]>()?;

Expand Down Expand Up @@ -215,7 +215,7 @@ impl InternalUriParams {
.join(", ")
}

pub fn validate(&self) -> Validation {
pub fn validate(&self) -> Validation<'_> {
let args = &self.uri_params.arguments;
match args {
Args::Unnamed(inner) => {
Expand Down
4 changes: 2 additions & 2 deletions core/codegen/src/derive/from_form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ fn is_valid_field_name(s: &str) -> bool {
}

impl FromMeta for FormField {
fn from_meta(meta: MetaItem) -> Result<Self> {
fn from_meta(meta: MetaItem<'_>) -> Result<Self> {
let string = String::from_meta(meta)?;
if !is_valid_field_name(&string) {
return Err(meta.value_span().error("invalid form field name"));
Expand All @@ -33,7 +33,7 @@ impl FromMeta for FormField {
}
}

fn validate_struct(gen: &DeriveGenerator, data: Struct) -> Result<()> {
fn validate_struct(gen: &DeriveGenerator, data: Struct<'_>) -> Result<()> {
if data.fields().is_empty() {
return Err(gen.input.span().error("at least one field is required"));
}
Expand Down
2 changes: 1 addition & 1 deletion core/codegen/src/derive/responder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use proc_macro::TokenStream;
use devise::{*, ext::TypeExt};
use devise::proc_macro2::TokenStream as TokenStream2;

use http_codegen::{ContentType, Status};
use crate::http_codegen::{ContentType, Status};

#[derive(Default, FromMeta)]
struct ItemAttr {
Expand Down
10 changes: 5 additions & 5 deletions core/codegen/src/derive/uri_display.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
use proc_macro::{Span, TokenStream};
use devise::*;

use derive::from_form::Form;
use proc_macro2::TokenStream as TokenStream2;
use crate::derive::from_form::Form;
use crate::proc_macro2::TokenStream as TokenStream2;

const NO_EMPTY_FIELDS: &str = "fieldless structs or variants are not supported";
const NO_NULLARY: &str = "nullary items are not supported";
const NO_EMPTY_ENUMS: &str = "empty enums are not supported";
const ONLY_ONE_UNNAMED: &str = "tuple structs or variants must have exactly one field";
const EXACTLY_ONE_FIELD: &str = "struct must have exactly one field";

fn validate_fields(fields: Fields, parent_span: Span) -> Result<()> {
fn validate_fields(fields: Fields<'_>, parent_span: Span) -> Result<()> {
if fields.count() == 0 {
return Err(parent_span.error(NO_EMPTY_FIELDS))
} else if fields.are_unnamed() && fields.count() > 1 {
Expand All @@ -22,11 +22,11 @@ fn validate_fields(fields: Fields, parent_span: Span) -> Result<()> {
Ok(())
}

fn validate_struct(gen: &DeriveGenerator, data: Struct) -> Result<()> {
fn validate_struct(gen: &DeriveGenerator, data: Struct<'_>) -> Result<()> {
validate_fields(data.fields(), gen.input.span())
}

fn validate_enum(gen: &DeriveGenerator, data: Enum) -> Result<()> {
fn validate_enum(gen: &DeriveGenerator, data: Enum<'_>) -> Result<()> {
if data.variants().count() == 0 {
return Err(gen.input.span().error(NO_EMPTY_ENUMS));
}
Expand Down
Loading

0 comments on commit be784a7

Please sign in to comment.