-
Notifications
You must be signed in to change notification settings - Fork 68
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
serde_json multi-typed literal parsing #308
Comments
I'm afraid there's no magical match statement - you'll need to write your own that looks at each Your simplest approach might be this: #[derive(FromMeta)]
struct Receiver {
#[darling(with = preserve_str_literal)]
default: syn::Expr,
} Then in the code you generate from this, you'd use default by writing... let default_expr = self.default;
quote_spanned!(default_expr.span()=>serde_json::Value::from(#default_expr)); This allows any input that evaluates to something which can be converted to a JSON value. |
@TedDriggs thank you so much for your help! Everything works great, except that it now a required field. Is it possible to have optional expressions, to parse things like #[derive(FromMeta)]
struct Receiver {
#[darling(with = preserve_str_literal)]
default: Option<syn::Expr>,
special: Option<bool>,
} |
Alternatively, perhaps it might make sense to simplify the API, and use dedicated virtual attributes: // `param` has two attributes, one "bool" and one with an aribtrary value
fn foo( #[default("value")] #[special] param: String ) Is this type of parsing possible? |
|
That should just work already: If |
I am implementing
#[attr(default = <literal>)]
, and my value can be multi-typed depending on what it gets attached to. Theserde_json::Value
is the format I will eventually have to work with. Darling seem to have the neededfrom_value()
functions, but I am not too certain how to use it to convert from asyn::Lit
intoserde_json::Value
, possibly via some magicalmatch
statement? Thx!The text was updated successfully, but these errors were encountered: