strum icon indicating copy to clipboard operation
strum copied to clipboard

Feature Request: Add attribute for types that don't implement Default

Open 0xForerunner opened this issue 3 years ago • 5 comments

Heres a quick example of how it could look:

#[derive(EnumIter)]
pub enum AssetInfo {
    Token {
        #[strum(default=Addr::unchecked(""))]
        contract_addr: Addr 
    },
    NativeToken { 
        denom: String 
    },
}

This would be very useful when working with types from external crates, or when implementing Default doesn't otherwise make sense.

0xForerunner avatar Oct 16 '22 18:10 0xForerunner

Hey @ewoolsey, there have been a few similiar requests, and I think this would be a very good fit for strum, but I haven't had time to implement it yet. If you're interested, I'd be happy to accept a PR, otherwise, I'll work on it when I have time.

Peternator7 avatar Oct 22 '22 22:10 Peternator7

I think I would err towards a design like the one serde uses #[serde(serialize_with = "path")] so that it feels familiar to people. Probably `#[strum(default_with = "path")] where path is the path to a function that accepts 0 arguments and returns a value of type T

Peternator7 avatar Oct 22 '22 22:10 Peternator7

@Peternator7 since posting this I’ve found an alternative solution (for my specific use case) with the clap Subcommand derive macro. My workload is pretty high the next few weeks so I’m not sure I’ll have time to contribute for a while, although I would like to get my hands dirty with some more involved proc macro projects. If you decide to pickup the torch on this let me know, I do think this would be a great addition to strum.

0xForerunner avatar Oct 23 '22 17:10 0xForerunner

I will knock this out

ericmcbride avatar Feb 16 '23 12:02 ericmcbride

I got a working proto type of this at the Variant Level + The Inner Variant Fields. Tests all pass and I didn't break anything. I plan on refactoring a little bit of the code, so Its easier to add Non-Derive / Non-Variant level macros (inner variant named fields), if its required in the future.

ericmcbride avatar Feb 24 '23 02:02 ericmcbride