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

Support placeholder in #[display] for struct #441

Open
yeongjet opened this issue Jan 21, 2025 · 2 comments
Open

Support placeholder in #[display] for struct #441

yeongjet opened this issue Jan 21, 2025 · 2 comments
Assignees
Milestone

Comments

@yeongjet
Copy link

yeongjet commented Jan 21, 2025

I have a struct which contains many fields and some of these fields (like b) need to display in Hex:

#[derive(Display)]
#[display("{a}, {b:x}, ..., {z}")]
struct A {
    a: u32,
    b: u32,
    ...,
    z: u32
}

but #[display("{a}, {b:x}, ..., {z}")] is long and verbose, how to use placeholder for struct?
I wish to use placeholder like enum:

#[derive(Display)]
#[display("{_variant}")]
struct A {
    a: u32,
    #[display("{:x}")]
    b: u32,
    ...,
    z: u32
}
@tyranron tyranron self-assigned this Jan 21, 2025
@tyranron tyranron added this to the 2.1.0 milestone Jan 21, 2025
@tyranron
Copy link
Collaborator

tyranron commented Jan 21, 2025

@yeongjet I don't understand your example:

I wish to use placeholder like enum:

#[derive(Display)]
#[display("{_variant}")]
struct A {
    a: u32,
    #[display("{:x}")]
    b: u32,
    ...,
    z: u32
}

How it's supposed to work? What's the logic behind {_variant} here? Why you're implying it should be something like #[display("{a}, {b:x}, ..., {z}")]?

We can theoretically support intermediate transformation of the field:

    #[display("{:x}")]
    b: u32,

But you still somehow should refer whether to include this into the final fmt representation.

@yeongjet
Copy link
Author

yeongjet commented Jan 21, 2025

@tyranron Sorry, it's just my rough idea which is somewhat lacking, more detailed example here:

#[derive(Display)]
// The _fields here is reserved like _variant, which is string array like `Vec<&str>` , 
// and the items is formatted by the macro `#[display("{_variant}")]` or `#[display("{:x}")]`
#[display(fmt = "all fields:{}, total:{}", _fields.join(","), _fields.len())
// Default field display
#[display("{_variant}")]
struct A {
    a: u32,
    // Special field display
    #[display("{:x}")]
    b: u32,
    ...,
    z: u32
}

Just my assumption, of course it won't work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants