frozenlib / parse-display

Procedural macro to implement Display and FromStr using common settings.
Apache License 2.0
182 stars 14 forks source link

Using pass-through formatting in Display #38

Open nyurik opened 7 months ago

nyurik commented 7 months ago

Is there a way to generate this Display implementation? Note that this is not a write!(f, "{0}", v), but rather v.fmt(f) call.

use std::fmt;

pub enum Term {
    Int(i32),
    Float(f64),
    Str(String),
}

impl fmt::Display for Term {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Int(v) => v.fmt(f),
            Self::Float(v) => v.fmt(f),
            Self::Str(v) => v.fmt(f),
        }
    }
}
frozenlib commented 7 months ago

Currently, parse-display does not have the ability to generate such code.

In the future, we may add the ability to generate such code by specifying an attribute such as #[display(transparent)].

nyurik commented 7 months ago

Thx @frozenlib for quick reply! Could you maybe point me to what changes would be needed to make it possible? I could try to hack on it in my spare time

frozenlib commented 7 months ago

https://github.com/frozenlib/parse-display/blob/1edea8ec3034b425b4e25b0dc7b49e3f20df81e4/parse-display-derive/src/lib.rs#L750-L759

I think the first step would be to add field transparent to DisplayArgs and change the code generation method to use this value.

I won't know until I actually write it, but it seems like many modifications will be necessary.