Coder Social home page Coder Social logo

Comments (6)

dtolnay avatar dtolnay commented on July 16, 2024

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.

killercup avatar killercup commented on July 16, 2024

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.

sgrif avatar sgrif commented on July 16, 2024

One easy win would be to drop Attribute entirely. I don't think the is_sugared_doc field does much for us.

from syn.

dtolnay avatar dtolnay commented on July 16, 2024

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.

Nemo157 avatar Nemo157 commented on July 16, 2024

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.

dtolnay avatar dtolnay commented on July 16, 2024

Amazing. I'm happy to recommend prom-attire as the solution to this. Thanks @Nemo157!

from syn.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.