Skip to content

Commit

Permalink
Use 0: for named-fields debug output of tuples
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ijackson committed Aug 1, 2024
1 parent 0d7c7c8 commit e7eeb6f
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 20 deletions.
7 changes: 4 additions & 3 deletions src/trait_handlers/debug/debug_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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,));
Expand Down
16 changes: 4 additions & 12 deletions src/trait_handlers/debug/debug_struct.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use quote::{format_ident, quote};
use quote::quote;
use syn::{Data, DeriveInput, Fields, Meta, Type};

use super::{
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions tests/debug_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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]
Expand Down Expand Up @@ -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]
Expand Down
4 changes: 2 additions & 2 deletions tests/debug_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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]
Expand Down

0 comments on commit e7eeb6f

Please sign in to comment.