-
Notifications
You must be signed in to change notification settings - Fork 69
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
RFE: Support negation for Flag
(or new similar type)
#278
Comments
I understand the desire and some of the use-cases for this, but I don't think this is possible under
Step 2 in that process is very flexible, but step 1 is very inflexible; the only way to change step 1 parsing would be to override the Even some of the deeper discussions about changing Possible Approach for Specific ScenariosIf you're trying to specifically parse a cfg-like attribute, you might be able to do the following. use darling::{FromDeriveInput, FromMeta};
use proc_macro2::TokenStream;
use syn::{parse_quote, Meta};
#[derive(Debug)]
struct NegatableIdentList {
tokens: TokenStream,
}
impl FromMeta for NegatableIdentList {
fn from_meta(item: &Meta) -> darling::Result<Self> {
match item {
Meta::Path(_) => Err(darling::Error::unsupported_format("word").with_span(item)),
Meta::List(list) => Ok(Self {
// A real implementation of this parse the token stream now to create the desired
// output data type. This involves a bunch of use-case-specific decisions about what
// features are needed, such as and/or/etc.
tokens: list.tokens.clone(),
}),
Meta::NameValue(_) => {
Err(darling::Error::unsupported_format("name-value").with_span(item))
}
}
}
}
#[derive(Debug, FromDeriveInput)]
#[darling(attributes(hello))]
struct Demo {
inner: NegatableIdentList,
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let di: syn::DeriveInput = parse_quote! {
// We need the extra `inner` here so that we don't have to reimplement the outer
// behavior of `FromDeriveInput` by hand. An alternate approach would be to forward
// the attribute that will contain this features list and then handle it using `with = ...`
// on the `attrs` magic field.
#[hello(inner(!example::flag, my_feature))]
struct Example;
};
let demo = Demo::from_derive_input(&di)?;
dbg!(&demo.inner.tokens);
Ok(())
} In this example, you take over all parsing of the meta contents before the parsing to |
In most configuration systems, a "key" when present let's enable (or enforce) associated feature activation. It is currently supported by
Flag
type.However, such systems also support disabling feature.
Example syntax:
#[my_attribute(!my_feature)]
Example (additional) API:
The text was updated successfully, but these errors were encountered: