Comments (6)
Something like this in this case:
if let attr_pat!{ #[_(treat_none_as_null = "true")] } = options_attr {
true
} else {
usage_err()
}
or:
if let attr_pat!{ #[_(treat_none_as_null = ref value)] } = options_attr {
value == "true"
} else {
usage_err()
}
from syn.
Interesting idea to use a macro for this. The macro as you described it above looks for this explicit pattern, right? How'd you handle the default case, i.e., the option we are looking for is not set (but maybe others are)?
I'd've suggested introducing a special attr type for attrs that are like #[$name($($key = $val),+)]
(in contrast to e.g. #[$name = $val]
).
let changeset_options: OptionsAttr =
macro_input
.attr("changeset_options")
.parse_as_options_attr()
.unwrap_or_else(usage_error);
let treat_none_as_null =
changeset_options
.boolean_field("treat_none_as_null")
.unwrap_or(false);
from syn.
One easy win would be to drop Attribute
entirely. I don't think the is_sugared_doc
field does much for us.
from syn.
Here is a neat idea from @killercup in colin-kiegel/rust-derive-builder#32 - use Serde!
I feel like there should be an idiomatic solution to parsing syn attributes to structs. Maybe another derive crate? A small serde plugin for (de)serializing things to/from rust attributes? The possibilities are infinite! :D
#[derive(Deserialize)] struct Options { setters: SetterOptions, /* … */ } #[derive(Deserialize)] struct GetterOptions { pattern: SetterPattern, public: bool, private: bool } #[derive(Deserialize)] enum SetterPattern { Owned, Mutable, Immutable } // … let attr: syn::Attribute = attr; let opt: Options = parse_attribute!(builder_options);
from syn.
I wrote a little procedural macro crate for parsing attributes prom-attire
. Still needs documentation written, but the initial diesel example (#[changeset_options(treat_none_as_null = "true")]
) would be:
#[derive(PromAttire)]
#[attire(scope = "changeset_options")]
ChangesetOptions {
treat_none_as_null: bool
}
let options = ChangesetOptions::from(macro_input.attrs.as_slice());
from syn.
Amazing. I'm happy to recommend prom-attire as the solution to this. Thanks @Nemo157!
from syn.
Related Issues (20)
- Parsing keywords in `Meta`-path positions fails with syn v2 HOT 4
- Can't parse `DeriveInput` containing an expression-position macro invocation without `full` feature HOT 1
- [Question/Bug] Raw strings in function HOT 3
- Mention 5036fd9c6e9b89809f93a6ffa17ac6663652baed in release notes for 2.0.9
- `parse_nested_meta` docs: mention that error will be returned if closure doesn't parse non-path parts HOT 1
- cannot find macro `compile_error` in this scope
- Attribute of assign expr HOT 2
- Make syn::meta::parse_nested_meta public?
- `Ident::as_str()` method? HOT 2
- Parsing expression `a.0.1` doesn't have the right spans
- Parse "return type notation"
- Parse non-lifetime binders
- Syn 2.0 doesn't causes problems when parsing keywords in attributes HOT 1
- Add `parse_macro_input2` for `proc_macro2::TokenStream` HOT 1
- Incorrect line column information when parsing from string HOT 2
- Why is Spanned sealed? HOT 6
- "unexpected token" error even when all input has been parsed HOT 4
- dump_syntax doesn't give span info for lex errors
- How to avoid from the specific error when parsing nested meta? HOT 3
- Add `parse2_macro_input` (or equivalent) parsing macro using `syn::parse2` instead of `syn::parse` HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from syn.