Skip to content

Commit

Permalink
Merge #98
Browse files Browse the repository at this point in the history
98: Minor cleanup r=taiki-e a=taiki-e



Co-authored-by: Taiki Endo <[email protected]>
  • Loading branch information
bors[bot] and taiki-e authored May 7, 2020
2 parents f5d1c1e + a0839ca commit 48a5d22
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 84 deletions.
35 changes: 14 additions & 21 deletions core/src/auto_enum/context.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use std::{collections::hash_map::DefaultHasher, hash::Hasher, iter, mem};

use proc_macro2::TokenStream;
use quote::format_ident;
use std::{collections::hash_map::DefaultHasher, hash::Hasher, iter, mem};
#[cfg(feature = "type_analysis")]
use syn::Type;
use syn::{
parse::{Parse, ParseStream},
Result, *,
token, Attribute, Error, Expr, Ident, ItemEnum, Macro, Path, Result,
};

use crate::utils::{expr_call, path, replace_expr, unit, VisitedNode};

use super::visitor::{Dummy, Visitor};
use crate::utils::{expr_call, path, replace_expr, unit, VisitedNode};

// =================================================================================================
// Context
Expand Down Expand Up @@ -157,8 +157,8 @@ impl Context {
}

#[cfg(auto_enums_def_site_enum_ident)]
pub(super) fn update_enum_ident(&mut self, item: &ItemFn) {
self.builder.update_enum_ident(&item.sig.ident)
pub(super) fn update_enum_ident(&mut self, ident: &Ident) {
self.builder.update_enum_ident(ident)
}

/// Returns `true` if one or more errors occurred.
Expand Down Expand Up @@ -322,25 +322,18 @@ impl Parse for Args {
fn parse(input: ParseStream<'_>) -> Result<Self> {
let mut args = Vec::new();
let mut marker = None;

while !input.is_empty() {
if input.peek(kw::marker) && input.peek2(Token![=]) {
let _: kw::marker = input.parse()?;
let _: Token![=] = input.parse()?;
let i: Ident = input.parse()?;
if marker.is_some() {
if input.peek(kw::marker) && input.peek2(token::Eq) {
let i: kw::marker = input.parse()?;
let _: token::Eq = input.parse()?;
let ident: Ident = input.parse()?;
if marker.replace(ident).is_some() {
return Err(error!(i, "duplicate `marker` argument"));
} else {
marker = Some(i);
if !input.is_empty() {
let _: token::Comma = input.parse()?;
}
continue;
}
} else {
args.push(input.parse()?);
}

args.push(input.parse()?);

if !input.is_empty() {
let _: token::Comma = input.parse()?;
}
Expand Down
8 changes: 5 additions & 3 deletions core/src/auto_enum/expr.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use syn::{
token,
visit_mut::{self, VisitMut},
Result, *,
Arm, Block, Expr, ExprBlock, ExprBreak, ExprCall, ExprIf, ExprLoop, ExprMacro, ExprMatch,
ExprMethodCall, ExprParen, ExprPath, ExprTry, ExprType, ExprUnsafe, Item, Label, Lifetime,
Result, Stmt,
};

use crate::utils::{expr_block, replace_block, replace_expr, Attrs};

use super::{visitor, Context, NAME, NESTED, NEVER};
use crate::utils::{expr_block, replace_block, replace_expr, Attrs};

/// Visits last expression.
///
Expand Down
23 changes: 14 additions & 9 deletions core/src/auto_enum/mod.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
use proc_macro2::TokenStream;
use quote::ToTokens;
use syn::*;

use crate::utils::{block, expr_block, replace_expr};

mod context;
mod expr;
#[cfg(feature = "type_analysis")]
mod type_analysis;
mod visitor;

use proc_macro2::TokenStream;
use quote::ToTokens;
#[cfg(feature = "type_analysis")]
use syn::Pat;
use syn::{
AngleBracketedGenericArguments, Expr, ExprClosure, GenericArgument, Item, ItemEnum, ItemFn,
Local, PathArguments, Result, ReturnType, Stmt, Type, TypePath,
};

use crate::utils::{block, expr_block, replace_expr};

use self::{
context::{Context, VisitLastMode, VisitMode, DEFAULT_MARKER},
expr::child_expr,
Expand Down Expand Up @@ -116,8 +121,8 @@ fn expand_parent_expr(expr: &mut Expr, cx: &mut Context, has_semi: bool) -> Resu
fn expand_parent_local(local: &mut Local, cx: &mut Context) -> Result<()> {
#[cfg(feature = "type_analysis")]
{
if let Pat::Type(PatType { ty, .. }) = &mut local.pat {
cx.collect_trait(&mut *ty);
if let Pat::Type(pat) = &mut local.pat {
cx.collect_trait(&mut pat.ty);
}
}

Expand All @@ -142,7 +147,7 @@ fn expand_parent_local(local: &mut Local, cx: &mut Context) -> Result<()> {

fn expand_parent_item_fn(item: &mut ItemFn, cx: &mut Context) -> Result<()> {
#[cfg(auto_enums_def_site_enum_ident)]
cx.update_enum_ident(item);
cx.update_enum_ident(&item.sig.ident);

let ItemFn { sig, block, .. } = item;
if let ReturnType::Type(_, ty) = &mut sig.output {
Expand Down
6 changes: 3 additions & 3 deletions core/src/auto_enum/visitor.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use proc_macro2::Group;
use quote::ToTokens;
use syn::{
token,
visit_mut::{self, VisitMut},
*,
Arm, Attribute, Expr, ExprMacro, ExprMatch, ExprReturn, ExprTry, Item, Local, Stmt,
};

use crate::utils::{parse_as_empty, replace_expr, Attrs, VisitedNode};

use super::{Context, VisitMode, DEFAULT_MARKER, NAME, NESTED, NEVER};
use crate::utils::{parse_as_empty, replace_expr, Attrs, VisitedNode};

#[derive(Clone, Copy, Default)]
struct Scope {
Expand Down
8 changes: 5 additions & 3 deletions core/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use std::{iter, mem};

use proc_macro2::TokenStream;
use syn::{punctuated::Punctuated, visit_mut::VisitMut, *};
use std::{iter, mem};
use syn::{
punctuated::Punctuated, token, visit_mut::VisitMut, Arm, Attribute, Block, Expr, ExprBlock,
ExprCall, ExprPath, ExprTuple, ItemFn, Local, Path, PathSegment, Result, Stmt,
};

macro_rules! error {
($span:expr, $msg:expr) => {
Expand Down
10 changes: 4 additions & 6 deletions derive/src/enum_derive.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use std::collections::HashMap;

use lazy_static::lazy_static;
use proc_macro2::TokenStream;
use std::collections::HashMap;
use syn::{
parse::{Parse, ParseStream},
token, ItemEnum, Path,
};

use crate::utils::*;
use crate::utils::{Data, EnumData, ItemImpl, Result, ToTokens};

pub(crate) fn attribute(args: TokenStream, input: TokenStream) -> TokenStream {
expand(args, input).unwrap_or_else(|e| e.to_compile_error())
Expand Down Expand Up @@ -229,10 +228,9 @@ impl Parse for Args {
}

let mut inner = Vec::new();

while !input.is_empty() {
let value = input.parse()?;
inner.push((to_trimed_string(&value), value));
let path = input.parse()?;
inner.push((to_trimed_string(&path), path));

if !input.is_empty() {
let _: token::Comma = input.parse()?;
Expand Down
32 changes: 14 additions & 18 deletions derive/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,12 @@
use std::ops::Deref;

use proc_macro2::TokenStream;
use std::ops::Deref;

#[cfg(feature = "fn_traits")]
pub(crate) use derive_utils::Trait;
pub(crate) use derive_utils::{derive_trait_internal as derive_trait, EnumData};
pub(crate) use quote::{format_ident, quote, ToTokens};
pub(crate) use syn::{parse2, ItemImpl, Result};

pub(crate) struct Data {
pub(crate) data: EnumData,
pub(crate) span: TokenStream,
}

impl Deref for Data {
type Target = EnumData;

fn deref(&self) -> &Self::Target {
&self.data
}
}

// =================================================================================================
// Macros

#[cfg(any(feature = "fn_traits", feature = "transpose_methods"))]
macro_rules! param_ident {
($($tt:tt)*) => {
Expand Down Expand Up @@ -52,3 +35,16 @@ macro_rules! error {
error!($span, format!($($tt)*))
};
}

pub(crate) struct Data {
pub(crate) data: EnumData,
pub(crate) span: TokenStream,
}

impl Deref for Data {
type Target = EnumData;

fn deref(&self) -> &Self::Target {
&self.data
}
}
Loading

0 comments on commit 48a5d22

Please sign in to comment.