From e4e505d02c0041c00bf8fc672d43a950603eac85 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Thu, 1 Aug 2024 11:07:35 +0100 Subject: [PATCH 1/4] Preparation: impl Clone for IdentOrIndex We'll use this shortly. --- src/common/ident_index.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/common/ident_index.rs b/src/common/ident_index.rs index c0e38d4..23403cb 100644 --- a/src/common/ident_index.rs +++ b/src/common/ident_index.rs @@ -1,6 +1,7 @@ use quote::ToTokens; use syn::{Ident, Index}; +#[derive(Clone)] pub(crate) enum IdentOrIndex { Ident(Ident), Index(Index), From 0d7c7c8d9d5fb1ab4bc29ef8543bf3d176367797 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Thu, 1 Aug 2024 12:07:26 +0100 Subject: [PATCH 2/4] debug_struct: Split up calculation of field_name and key field_name is the real field name and doesn't depend on the attribute. Separating this makes things less confusing. --- src/trait_handlers/debug/debug_struct.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/trait_handlers/debug/debug_struct.rs b/src/trait_handlers/debug/debug_struct.rs index 6430480..38beb33 100644 --- a/src/trait_handlers/debug/debug_struct.rs +++ b/src/trait_handlers/debug/debug_struct.rs @@ -63,15 +63,16 @@ impl TraitHandler for DebugStructHandler { continue; } - let (key, field_name) = match field_attribute.name { + let field_name = IdentOrIndex::from_ident_with_index(field.ident.as_ref(), index); + let key = match field_attribute.name { FieldName::Custom(name) => { - (name, IdentOrIndex::from_ident_with_index(field.ident.as_ref(), index)) + name }, FieldName::Default => { if let Some(ident) = field.ident.as_ref() { - (ident.clone(), IdentOrIndex::from(ident)) + ident.clone() } else { - (format_ident!("_{}", index), IdentOrIndex::from(index)) + format_ident!("_{}", index) } }, }; From e7eeb6fbd6fa061fe4e912981ddc48eae1bf7eb8 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Thu, 1 Aug 2024 11:58:40 +0100 Subject: [PATCH 3/4] Use `0:` for named-fields debug output of tuples Change the output when the user requests debug printing of a tuple struct in named fields syntax. Previously we output `_0: ` (for example), which isn't really right: Rust permits `0: `, and the field is actually called `0`. This is a behavioural change, but I don't think Debug output is regarded as semver-relevant, so this shouldn't be a breaking change. --- src/trait_handlers/debug/debug_enum.rs | 7 ++++--- src/trait_handlers/debug/debug_struct.rs | 16 ++++------------ tests/debug_enum.rs | 6 +++--- tests/debug_struct.rs | 4 ++-- 4 files changed, 13 insertions(+), 20 deletions(-) diff --git a/src/trait_handlers/debug/debug_enum.rs b/src/trait_handlers/debug/debug_enum.rs index 1e42381..1ef53f3 100644 --- a/src/trait_handlers/debug/debug_enum.rs +++ b/src/trait_handlers/debug/debug_enum.rs @@ -3,6 +3,7 @@ use syn::{Data, DeriveInput, Fields, Meta, Type}; use super::models::{FieldAttributeBuilder, FieldName, TypeAttributeBuilder, TypeName}; use crate::{common::path::path_to_string, supported_traits::Trait, trait_handlers::TraitHandler}; +use crate::common::ident_index::IdentOrIndex; pub(crate) struct DebugEnumHandler; @@ -219,9 +220,9 @@ impl TraitHandler for DebugEnumHandler { let field_name_var = format_ident!("_{}", index); - let key = match field_attribute.name { - FieldName::Custom(name) => name, - FieldName::Default => field_name_var.clone(), + let key: IdentOrIndex = match field_attribute.name { + FieldName::Custom(name) => name.into(), + FieldName::Default => index.into(), }; pattern_token_stream.extend(quote!(#field_name_var,)); diff --git a/src/trait_handlers/debug/debug_struct.rs b/src/trait_handlers/debug/debug_struct.rs index 38beb33..c3bed81 100644 --- a/src/trait_handlers/debug/debug_struct.rs +++ b/src/trait_handlers/debug/debug_struct.rs @@ -1,4 +1,4 @@ -use quote::{format_ident, quote}; +use quote::quote; use syn::{Data, DeriveInput, Fields, Meta, Type}; use super::{ @@ -64,17 +64,9 @@ impl TraitHandler for DebugStructHandler { } let field_name = IdentOrIndex::from_ident_with_index(field.ident.as_ref(), index); - let key = match field_attribute.name { - FieldName::Custom(name) => { - name - }, - FieldName::Default => { - if let Some(ident) = field.ident.as_ref() { - ident.clone() - } else { - format_ident!("_{}", index) - } - }, + let key: IdentOrIndex = match field_attribute.name { + FieldName::Custom(name) => name.into(), + FieldName::Default => field_name.clone(), }; let ty = &field.ty; diff --git a/tests/debug_enum.rs b/tests/debug_enum.rs index 05a158a..1b2ce76 100644 --- a/tests/debug_enum.rs +++ b/tests/debug_enum.rs @@ -253,7 +253,7 @@ fn named_field_1() { f1: 1 }) ); - assert_eq!("Tuple { _0: 1 }", format!("{:?}", Enum::Tuple(1))); + assert_eq!("Tuple { 0: 1 }", format!("{:?}", Enum::Tuple(1))); } #[test] @@ -273,7 +273,7 @@ fn named_field_2() { f1: 1 }) ); - assert_eq!("Tuple { _0: 1 }", format!("{:?}", Enum::Tuple(1))); + assert_eq!("Tuple { 0: 1 }", format!("{:?}", Enum::Tuple(1))); } #[test] @@ -302,7 +302,7 @@ fn named_field_3() { f1: 1 }) ); - assert_eq!("Tuple { _0: Hi }", format!("{:?}", Enum::Tuple(1))); + assert_eq!("Tuple { 0: Hi }", format!("{:?}", Enum::Tuple(1))); } #[test] diff --git a/tests/debug_struct.rs b/tests/debug_struct.rs index 4cd8573..4781b89 100644 --- a/tests/debug_struct.rs +++ b/tests/debug_struct.rs @@ -245,7 +245,7 @@ fn named_field_1() { #[educe(Debug(named_field = true))] struct Tuple(u8); - assert_eq!("Tuple { _0: 1 }", format!("{:?}", Tuple(1))); + assert_eq!("Tuple { 0: 1 }", format!("{:?}", Tuple(1))); } #[test] @@ -267,7 +267,7 @@ fn named_field_2() { #[educe(Debug(named_field(true)))] struct Tuple(u8); - assert_eq!("Tuple { _0: 1 }", format!("{:?}", Tuple(1))); + assert_eq!("Tuple { 0: 1 }", format!("{:?}", Tuple(1))); } #[test] From 04fd6c27e3c0d8b745a3b987a652d71f19a8a31f Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Thu, 1 Aug 2024 13:22:05 +0100 Subject: [PATCH 4/4] rustfmt --- src/trait_handlers/debug/debug_enum.rs | 7 +++++-- src/trait_handlers/debug/debug_struct.rs | 3 ++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/trait_handlers/debug/debug_enum.rs b/src/trait_handlers/debug/debug_enum.rs index 1ef53f3..a361d06 100644 --- a/src/trait_handlers/debug/debug_enum.rs +++ b/src/trait_handlers/debug/debug_enum.rs @@ -2,8 +2,11 @@ use quote::{format_ident, quote, ToTokens}; use syn::{Data, DeriveInput, Fields, Meta, Type}; use super::models::{FieldAttributeBuilder, FieldName, TypeAttributeBuilder, TypeName}; -use crate::{common::path::path_to_string, supported_traits::Trait, trait_handlers::TraitHandler}; -use crate::common::ident_index::IdentOrIndex; +use crate::{ + common::{ident_index::IdentOrIndex, path::path_to_string}, + supported_traits::Trait, + trait_handlers::TraitHandler, +}; pub(crate) struct DebugEnumHandler; diff --git a/src/trait_handlers/debug/debug_struct.rs b/src/trait_handlers/debug/debug_struct.rs index c3bed81..342719b 100644 --- a/src/trait_handlers/debug/debug_struct.rs +++ b/src/trait_handlers/debug/debug_struct.rs @@ -63,7 +63,8 @@ impl TraitHandler for DebugStructHandler { continue; } - let field_name = IdentOrIndex::from_ident_with_index(field.ident.as_ref(), index); + let field_name = + IdentOrIndex::from_ident_with_index(field.ident.as_ref(), index); let key: IdentOrIndex = match field_attribute.name { FieldName::Custom(name) => name.into(), FieldName::Default => field_name.clone(),