Skip to content

Commit

Permalink
run rustfmt
Browse files Browse the repository at this point in the history
  • Loading branch information
magiclen committed Sep 16, 2019
1 parent 2ab13d4 commit 9fbaf17
Show file tree
Hide file tree
Showing 71 changed files with 5,280 additions and 2,490 deletions.
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "educe"
version = "0.4.0"
version = "0.4.1"
authors = ["Magic Len <[email protected]>"]
edition = "2018"
repository = "https://github.com/magiclen/educe"
Expand All @@ -19,9 +19,9 @@ branch = "master"
proc-macro = true

[dependencies]
proc-macro2 = "0.4"
syn = { version = "0.15", features = ["full"] }
quote = "0.6"
proc-macro2 = "1"
syn = { version = "1", features = ["full"] }
quote = "1"

[features]
default = ["Debug", "PartialEq", "Eq", "PartialOrd", "Ord", "Hash", "Default", "Clone", "Copy", "Deref", "DerefMut"]
Expand Down
15 changes: 15 additions & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
brace_style = "PreferSameLine"
enum_discrim_align_threshold = 100
force_explicit_abi = false
force_multiline_blocks = true
format_code_in_doc_comments = true
format_macro_matchers = true
max_width = 100
newline_style = "Unix"
normalize_doc_attributes = true
overflow_delimited_expr = true
reorder_impl_items = true
struct_lit_single_line = false
use_field_init_shorthand = true
use_small_heuristics = "Off"
use_try_shorthand = true
30 changes: 16 additions & 14 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1523,6 +1523,7 @@ mod support_traits;
mod trait_handlers;

use proc_macro2::TokenStream;
use quote::ToTokens;
use syn::{DeriveInput, Meta, NestedMeta};

use support_traits::Trait;
Expand Down Expand Up @@ -1585,15 +1586,15 @@ fn derive_input_handler(ast: DeriveInput) -> TokenStream {
for attr in ast.attrs.iter() {
let attr_meta = attr.parse_meta().unwrap();

let attr_meta_name = attr_meta.name().to_string();
let attr_meta_name = attr_meta.path().into_token_stream().to_string();

match attr_meta_name.as_str() {
"educe" => match attr_meta {
if attr_meta_name.as_str() == "educe" {
match attr_meta {
Meta::List(list) => {
for p in list.nested {
match p {
NestedMeta::Meta(meta) => {
let meta_name = meta.name().to_string();
let meta_name = meta.path().into_token_stream().to_string();

let t = Trait::from_str(meta_name);

Expand All @@ -1604,19 +1605,20 @@ fn derive_input_handler(ast: DeriveInput) -> TokenStream {
traits.push(t);
metas.push(meta);
}
NestedMeta::Literal(_) => panic::attribute_incorrect_format(
"educe",
&[stringify!(#[educe(Trait1, Trait2, ..., TraitN)])],
),
NestedMeta::Lit(_) => {
panic::attribute_incorrect_format("educe", &[
stringify!(#[educe(Trait1, Trait2, ..., TraitN)]),
])
}
}
}
}
_ => panic::attribute_incorrect_format(
"educe",
&[stringify!(#[educe(Trait1, Trait2, ..., TraitN)])],
),
},
_ => (),
_ => {
panic::attribute_incorrect_format("educe", &[
stringify!(#[educe(Trait1, Trait2, ..., TraitN)]),
])
}
}
}
}

Expand Down
10 changes: 2 additions & 8 deletions src/panic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,7 @@ pub fn reset_parameter(parameter_name: &str) -> ! {

#[inline]
pub fn unknown_parameter(attribute_name: &str, parameter_name: &str) -> ! {
panic!(
"Unknown parameter `{}` used in the `{}` attribute.",
parameter_name, attribute_name
)
panic!("Unknown parameter `{}` used in the `{}` attribute.", parameter_name, attribute_name)
}

#[inline]
Expand Down Expand Up @@ -190,10 +187,7 @@ pub fn reuse_a_value(value: isize) -> ! {

#[inline]
pub fn educe_format_incorrect() -> ! {
attribute_incorrect_format(
"educe",
&[stringify!(#[educe(Trait1, Trait2, ..., TraitN)])],
)
attribute_incorrect_format("educe", &[stringify!(#[educe(Trait1, Trait2, ..., TraitN)])])
}

fn concat_string_slice_array(array: &[&str]) -> String {
Expand Down
14 changes: 8 additions & 6 deletions src/support_traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub enum Trait {

impl Trait {
#[inline]
pub fn as_str(&self) -> &'static str {
pub fn as_str(self) -> &'static str {
match self {
Trait::Debug => "Debug",
Trait::PartialEq => "PartialEq",
Expand Down Expand Up @@ -59,11 +59,13 @@ impl Trait {
"Deref" => Trait::Deref,
#[cfg(feature = "DerefMut")]
"DerefMut" => Trait::DerefMut,
_ => panic!(
"Unsupported trait `{}`. Available traits are {:?}",
s,
Trait::support_traits()
),
_ => {
panic!(
"Unsupported trait `{}`. Available traits are {:?}",
s,
Trait::support_traits()
)
}
}
}

Expand Down
59 changes: 33 additions & 26 deletions src/trait_handlers/clone/clone_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::Trait;
pub struct CloneEnumHandler;

impl TraitHandler for CloneEnumHandler {
#[allow(clippy::cognitive_complexity)]
fn trait_meta_handler(
ast: &DeriveInput,
tokens: &mut TokenStream,
Expand Down Expand Up @@ -52,8 +53,10 @@ impl TraitHandler for CloneEnumHandler {
is_tuple = false;

for field in fields.named.iter() {
let field_attribute = FieldAttributeBuilder { enable_impl: true }
.from_attributes(&field.attrs, traits);
let field_attribute = FieldAttributeBuilder {
enable_impl: true,
}
.from_attributes(&field.attrs, traits);

let field_name = field.ident.as_ref().unwrap().to_string();

Expand All @@ -68,8 +71,10 @@ impl TraitHandler for CloneEnumHandler {
Fields::Unnamed(fields) => {
// TODO Tuple
for (index, field) in fields.unnamed.iter().enumerate() {
let field_attribute = FieldAttributeBuilder { enable_impl: true }
.from_attributes(&field.attrs, traits);
let field_attribute = FieldAttributeBuilder {
enable_impl: true,
}
.from_attributes(&field.attrs, traits);

let field_name = format!("_{}", index);

Expand Down Expand Up @@ -105,7 +110,7 @@ impl TraitHandler for CloneEnumHandler {
let is_tuple = field_attributes_names.0;
let field_attributes = &field_attributes_names.1;
let field_names = &field_attributes_names.2;
let is_unit = field_names.len() == 0;
let is_unit = field_names.is_empty();

if is_unit {
match_tokens.write_fmt(format_args!("{enum_name}::{variant_ident} => {{ if let {enum_name}::{variant_ident} = _source {{ done = true; }} }}", enum_name = enum_name, variant_ident = variant_ident)).unwrap();
Expand Down Expand Up @@ -196,7 +201,7 @@ impl TraitHandler for CloneEnumHandler {
let is_tuple = field_attributes_names.0;
let field_attributes = &field_attributes_names.1;
let field_names = &field_attributes_names.2;
let is_unit = field_names.len() == 0;
let is_unit = field_names.is_empty();

if is_unit {
clone_match_tokens.write_fmt(format_args!("{enum_name}::{variant_ident} => {{ {enum_name}::{variant_ident} }}", enum_name = enum_name, variant_ident = variant_ident)).unwrap();
Expand Down Expand Up @@ -255,27 +260,29 @@ impl TraitHandler for CloneEnumHandler {
.unwrap();
clone_from.write_fmt(format_args!("*{field_name} = {clone_trait}::{clone_method}(___{field_name});", clone_trait = clone_trait, clone_method = clone_method, field_name = field_name)).unwrap();
}
None => match clone_method {
Some(clone_method) => {
clone
.write_fmt(format_args!(
"{clone_method}({field_name}),",
clone_method = clone_method,
field_name = field_name
))
.unwrap();
clone_from.write_fmt(format_args!("*{field_name} = {clone_method}(___{field_name});", clone_method = clone_method, field_name = field_name)).unwrap();
}
None => {
clone
.write_fmt(format_args!(
"core::clone::Clone::clone({field_name}),",
field_name = field_name
))
.unwrap();
clone_from.write_fmt(format_args!("core::clone::Clone::clone_from({field_name}, ___{field_name});", field_name = field_name)).unwrap();
None => {
match clone_method {
Some(clone_method) => {
clone
.write_fmt(format_args!(
"{clone_method}({field_name}),",
clone_method = clone_method,
field_name = field_name
))
.unwrap();
clone_from.write_fmt(format_args!("*{field_name} = {clone_method}(___{field_name});", clone_method = clone_method, field_name = field_name)).unwrap();
}
None => {
clone
.write_fmt(format_args!(
"core::clone::Clone::clone({field_name}),",
field_name = field_name
))
.unwrap();
clone_from.write_fmt(format_args!("core::clone::Clone::clone_from({field_name}, ___{field_name});", field_name = field_name)).unwrap();
}
}
},
}
}
}

Expand Down
8 changes: 5 additions & 3 deletions src/trait_handlers/clone/clone_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ impl TraitHandler for CloneStructHandler {
let mut has_custom_clone_method = false;

for (index, field) in data.fields.iter().enumerate() {
let field_attribute = FieldAttributeBuilder { enable_impl: true }
.from_attributes(&field.attrs, traits);
let field_attribute = FieldAttributeBuilder {
enable_impl: true,
}
.from_attributes(&field.attrs, traits);

let field_name = if let Some(ident) = field.ident.as_ref() {
ident.to_string()
Expand Down Expand Up @@ -74,7 +76,7 @@ impl TraitHandler for CloneStructHandler {

let (is_unit, is_tuple) = {
if let Some(field) = data.fields.iter().next() {
if let Some(_) = field.ident {
if field.ident.is_some() {
(false, false)
} else {
(false, true)
Expand Down
6 changes: 4 additions & 2 deletions src/trait_handlers/clone/clone_union.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ impl TraitHandler for CloneUnionHandler {

if let Data::Union(data) = &ast.data {
for field in data.fields.named.iter() {
let _ = FieldAttributeBuilder { enable_impl: false }
.from_attributes(&field.attrs, traits);
let _ = FieldAttributeBuilder {
enable_impl: false,
}
.from_attributes(&field.attrs, traits);
}
}

Expand Down
Loading

0 comments on commit 9fbaf17

Please sign in to comment.