Add support for assign default values to fields directly. eg. #[darling(default=false)]
RWDai opened this issue · comments
RWDai commented
nice crate! but i need some help.
if i want assign true to bool by default,how can i do?
This is the implementation of my long-winded code now,how can i write it simply.
#[derive(FromMeta, Debug)]
struct MyMacors {
#[darling(default)]
_bool: FlipDeFaultBool,
}
#[derive(FromMeta,Debug)]
struct FlipDeFaultBool{
_bool:bool
}
impl Default for FlipDeFaultBool {
fn default() -> Self {
FlipDeFaultBool{ _bool:true}
}
}
if we can't do it better. Is there a way to add default values in #[darling(default)] ?
juet like
#[derive(FromMeta, Debug)]
struct MyMacors {
#[darling(default=true)]
_bool: bool,
}
thank you for you greate crate again!
Ted Driggs commented
The way to do this is to create a function that returns the value you want for the default and then pass the path as follows:
fn return_true() -> bool { true }
#[derive(FromMeta)]
struct Example {
#[darling(default = "return_true")]
some_field: bool,
}
I'm not looking to support other value types for defaults - the current implementation is simple and consistent, and works for both primitive and complex values.