Coder Social home page Coder Social logo

attribute-derive's Introduction

Side Project Stack

(so I maybe finish some of it)

  • Sane Figglet unicode and toml based fogfont format
  • twitchctl
  • macro_rules like proc macro input parser
  • Implement secret store for ENV tokens
  • Email client
  • bitfireAT/icsx5#235

attribute-derive's People

Contributors

jwpjrdev avatar modprog avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

jwpjrdev eulegang

attribute-derive's Issues

Support flattening attributes

Similar to #4, I'm adding a new attribute for ViewSchema and want to share the view() identifier name, so that all the parameters just live on the same level, but I can avoid repeating the shared attributes on both types. Using the same example from #4, what I would hope is for:

struct Attr {
   #[attribute(flatten)]
   shared: SharedAttributes
}

struct SharedAttributes {
    shared_key: String
}

Should parse

#[attr(shared_key = "hi")]

Sub Attributes

Sometimes complex attributes might require sub attributes like so:

struct Attr {
   #[attribute(sub_attribute)]
   key: SubAttr
}

struct SubAttr {
    sub_key: String
}

Should parse

#[attr(key(sub_key = "hi"))]

trait bound `syn::ExprClosure: attribute_derive::parsing::AttributeBase` is not satisfied

When upgrading refuse-macros from 0.8 to 0.9, my usage of syn::ExprClosure no longer compiles with this error:

error[E0277]: the trait bound `syn::ExprClosure: attribute_derive::parsing::AttributeBase` is not satisfied
   --> refuse-macros/src/lib.rs:202:10
    |
202 | #[derive(FromAttr)]
    |          ^^^^^^^^ the trait `attribute_derive::parsing::AttributeBase` is not implemented for `syn::ExprClosure`
    |
    = help: the following other types implement trait `attribute_derive::parsing::AttributeBase`:
              bool
              char
              isize
              i8
              i16
              i32
              i64
              i128
            and 173 others
    = note: required for `std::option::Option<syn::ExprClosure>` to implement `attribute_derive::parsing::AttributeBase`
    = note: this error originates in the derive macro `FromAttr` (in Nightly builds, run with -Z macro-backtrace for more info)

I'm not sure if this isn't meant to be supported, if there's a better syn type to be using, or if this is just an oversight.

Edit: Oops, didn't realize that I haven't pushed the macro changes yet. Here's what the usage looks like:

#[derive(FromAttr)]
#[attribute(ident = collectable)]
struct MapAsAttr {
    target: Option<syn::Type>,
    map: Option<syn::ExprClosure>,
}

Error when trying to use the crate

I took an example directly from the docs:

#[derive(Attribute)]
#[attribute(ident = "collection")]
#[attribute(invalid_field = "Error when an unsupported value is set (e.g. meaning=42")]
struct CollectionAttribute {
    // Options are optional by default (will be set to None if not specified)
    authority: Option<String>,
    #[attribute(missing = "Error when the value is not set")]
    name: String,
    // Any type implementing default can be flagged as default
    // This will be set to Vec::default() when not specified
    #[attribute(default)]
    #[attribute(expected = "Error when an error occured while parsing")]
    views: Vec<Type>,
}

But I'm getting an error

error: expected `#[attribute(ident=attribute_name, aliases=[alias1, alias2], error="..", error(unknown_field="..", unknown_field_single="..", unknown_field_empty="..", duplicate_field="..", missing_field="..", field_help="..", missing_flag"..", flag_help".."))]`
 --> node_macro/src/lib.rs:6:10
  |
6 | #[derive(Attribute)]
  |          ^^^^^^^^^
  |
  = note: this error originates in the derive macro `Attribute` (in Nightly builds, run with -Z macro-backtrace for more info)

My Cargo.toml:

[package]
name = "node_macro"
version = "0.1.0"
edition = "2021"

[lib]
proc-macro = true

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
syn = { version = "2.0" }
quote = "1.0"
proc-macro2 = "1.0"
attribute-derive = "0.6"

allow fields to represent top level attributes

i.e.:

#[derive(FromAttr)]
#[attribute(flatten)]
struct FieldAttribute {
    default: FlagOrValue<Expr>,
    children: bool,
}

would parse

#[default]
#[default = <expr>]
// This is supported additionally, because
// `=` only supports a subset of expressions
#[default(<expr>)]

#[children]

We could support this maybe as a fieldlevel attribute as well, or allow the field value to be anything that implements FromAttr to allow hybrid structs with nested and unnested attributes.

Quoted/Parenthesized values

Support the "standard" way of using "meta" attributes:

#[derive(Attribute)]
struct Args {
    #[attribute(quoted)]
    key: Expression,
    #[attribute(parenthesized)]
    other: Path
}

should parse something like

#[attr(key="a + b", other(::a::b::C))]

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.