serde-rs / serde

Serialization framework for Rust
https://serde.rs/
Apache License 2.0
9.06k stars 767 forks source link

Implement Debug trait for serde_derive structs, enums, etc. #2761

Open janis-ab opened 3 months ago

janis-ab commented 3 months ago

Writing derive macro code is hard and println!("{:?}", some_var) is very helpful when debugging and trying to understand things.

There are a lot of structs in serde_derive that do not implement Debug trait, i.e. enum TagType, struct RenameRule, etc. Yes, those are somewhat internal structs, but when implementing derive macro it is necessary to work with them.

I understand that for most use-cases Debug trait on those structs is not necessary or useful. It is useful only when developing derive macros, thus i would like to propose, in my opinion, a low maintenance, backwards compatible solution to this.

Create a feature flag debug-impls. It could be named anything, i just chose name similar to clone-impls in syn crate; since serde has that as dependency and naming convention would be somewhat similar.

Then in serde_derive code we could add cfg_attr lines like so:

#[derive(Copy, Clone)]
#[cfg_attr(feature = "debug-impls", derive(Debug))]
pub enum Identifier {
    No,
    Field,
    Variant,
}

or for structs that do not have anything derived:

#[cfg_attr(feature = "debug-impls", derive(Debug))]
pub enum Identifier {
    No,
    Field,
    Variant,
}

Syntax works in both cases.

I would like to take care of this and send PRs, if they are accepted.

v3xro commented 2 months ago

Would also be interested in seeing this