Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use 0: for named-fields debug of tuples and tuple variants #32

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/common/ident_index.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use quote::ToTokens;
use syn::{Ident, Index};

#[derive(Clone)]
pub(crate) enum IdentOrIndex {
Ident(Ident),
Index(Index),
Expand Down
12 changes: 8 additions & 4 deletions src/trait_handlers/debug/debug_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +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, path::path_to_string},
supported_traits::Trait,
trait_handlers::TraitHandler,
};

pub(crate) struct DebugEnumHandler;

Expand Down Expand Up @@ -219,9 +223,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
18 changes: 6 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 @@ -63,17 +63,11 @@ impl TraitHandler for DebugStructHandler {
continue;
}

let (key, field_name) = match field_attribute.name {
FieldName::Custom(name) => {
(name, IdentOrIndex::from_ident_with_index(field.ident.as_ref(), index))
},
FieldName::Default => {
if let Some(ident) = field.ident.as_ref() {
(ident.clone(), IdentOrIndex::from(ident))
} else {
(format_ident!("_{}", index), IdentOrIndex::from(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(),
};

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
Loading