implement feedback from review

This commit is contained in:
Yoshua Wuyts 2021-08-16 18:23:51 +02:00
parent 9374d526da
commit 067dc660ae

View File

@ -154,8 +154,6 @@ fn gen_debug_impl(adt: &ast::Adt, func: &ast::Fn) -> Option<()> {
match variant.field_list() {
Some(ast::FieldList::RecordFieldList(list)) => {
let mut pats = vec![];
// => f.debug_struct(name)
let target = make::expr_path(make::ext::ident_path("f"));
let method = make::name_ref("debug_struct");
@ -163,20 +161,20 @@ fn gen_debug_impl(adt: &ast::Adt, func: &ast::Fn) -> Option<()> {
let args = make::arg_list(Some(make::expr_literal(&struct_name).into()));
let mut expr = make::expr_method_call(target, method, args);
let mut pats = vec![];
for field in list.fields() {
let name = field.name()?;
let field_name = field.name()?;
// create a field pattern for use in `MyStruct { fields.. }`
let field_name = field.name()?;
let pat = make::ident_pat(false, false, field_name.clone());
pats.push(pat.into());
// => <expr>.field("field_name", field)
let method_name = make::name_ref("field");
let field_name = make::expr_literal(&(format!("\"{}\"", name))).into();
let field_path = &format!("{}", name);
let field_path = make::expr_path(make::ext::ident_path(field_path));
let args = make::arg_list(vec![field_name, field_path]);
let name = make::expr_literal(&(format!("\"{}\"", field_name))).into();
let path = &format!("{}", field_name);
let path = make::expr_path(make::ext::ident_path(path));
let args = make::arg_list(vec![name, path]);
expr = make::expr_method_call(expr, method_name, args);
}
@ -189,8 +187,6 @@ fn gen_debug_impl(adt: &ast::Adt, func: &ast::Fn) -> Option<()> {
arms.push(make::match_arm(Some(pat.into()), None, expr));
}
Some(ast::FieldList::TupleFieldList(list)) => {
let mut pats = vec![];
// => f.debug_tuple(name)
let target = make::expr_path(make::ext::ident_path("f"));
let method = make::name_ref("debug_tuple");
@ -198,6 +194,7 @@ fn gen_debug_impl(adt: &ast::Adt, func: &ast::Fn) -> Option<()> {
let args = make::arg_list(Some(make::expr_literal(&struct_name).into()));
let mut expr = make::expr_method_call(target, method, args);
let mut pats = vec![];
for (i, _) in list.fields().enumerate() {
let name = format!("arg{}", i);
@ -224,7 +221,7 @@ fn gen_debug_impl(adt: &ast::Adt, func: &ast::Fn) -> Option<()> {
}
None => {
let fmt_string = make::expr_literal(&(format!("\"{}\"", name))).into();
let args = make::arg_list(vec![target, fmt_string]);
let args = make::arg_list([target, fmt_string]);
let macro_name = make::expr_path(make::ext::ident_path("write"));
let macro_call = make::expr_macro_call(macro_name, args);
@ -267,7 +264,7 @@ fn gen_debug_impl(adt: &ast::Adt, func: &ast::Fn) -> Option<()> {
let f_path = make::expr_path(make::ext::ident_path("self"));
let f_path = make::expr_ref(f_path, false);
let f_path = make::expr_field(f_path, &format!("{}", name)).into();
let args = make::arg_list(vec![f_name, f_path]);
let args = make::arg_list([f_name, f_path]);
expr = make::expr_method_call(expr, make::name_ref("field"), args);
}
expr