Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@

Basically clap for attribute macros:
```rust
use attribute_derive::Attribute;
use attribute_derive::FromAttr;
use syn::Type;

#[derive(Attribute)]
#[derive(FromAttr)]
#[attribute(ident = collection)]
#[attribute(error(missing_field = "`{field}` was not specified"))]
struct CollectionAttribute {
Expand Down Expand Up @@ -42,18 +42,18 @@ There are some limitations in syntax parsing that will be lifted future releases

## Parse methods

There are multiple ways of parsing a struct deriving [`Attribute`](https://docs.rs/attribute-derive/latest/attribute_derive/trait.Attribute.html).
There are multiple ways of parsing a struct deriving [`FromAttr`](https://docs.rs/attribute-derive/latest/attribute_derive/trait.FromAttr.html).

For helper attributes there is:
- [`Attribute::from_attributes`](https://docs.rs/attribute-derive/latest/attribute_derive/trait.Attribute.html#tymethod.from_attributes) which takes in an [`IntoIterator<Item = &'a
- [`FromAttr::from_attributes`](https://docs.rs/attribute-derive/latest/attribute_derive/trait.FromAttr.html#tymethod.from_attributes) which takes in an [`IntoIterator<Item = &'a
syn::Attribute`](https://docs.rs/syn/latest/syn/struct.Attribute.html)
(e.g. a [`&Vec<syn::Attribute>`](https://docs.rs/syn/latest/syn/struct.Attribute.html)). Most useful for derive macros.
- [`Attribute::remove_attributes`](https://docs.rs/attribute-derive/latest/attribute_derive/trait.Attribute.html#tymethod.remove_attributes) which takes an [`&mut Vec<syn::Attribute>`](https://docs.rs/syn/latest/syn/struct.Attribute.html)
and does not only parse the [`Attribute`](https://docs.rs/attribute-derive/latest/attribute_derive/trait.Attribute.html#tymethod.from_attributes) but also removes those matching. Useful for helper
- [`FromAttr::remove_attributes`](https://docs.rs/attribute-derive/latest/attribute_derive/trait.FromAttr.html#tymethod.remove_attributes) which takes an [`&mut Vec<syn::Attribute>`](https://docs.rs/syn/latest/syn/struct.Attribute.html)
and does not only parse the [`FromAttr`](https://docs.rs/attribute-derive/latest/attribute_derive/trait.FromAttr.html#tymethod.from_attributes) but also removes those matching. Useful for helper
attributes for proc macros, where the helper attributes need to be removed.

For parsing a single [`TokenStream`](https://docs.rs/proc-macro2/latest/proc_macro2/struct.TokenStream.html) e.g. for parsing the proc macro input there a two ways:

- [`Attribute::from_args`](https://docs.rs/attribute-derive/latest/attribute_derive/trait.Attribute.html#tymethod.from_args) taking in a [`TokenStream`](https://docs.rs/proc-macro2/latest/proc_macro2/struct.TokenStream.html)
- As `derive(Attribute)` also derives [`Parse`](https://docs.rs/syn/latest/syn/parse/trait.Parse.html) so you can use the [parse](https://docs.rs/syn/latest/syn/parse/index.html) API,
- [`FromAttr::from_input`](https://docs.rs/attribute-derive/latest/attribute_derive/trait.FromAttr.html#tymethod.from_input) taking in a [`TokenStream`](https://docs.rs/proc-macro2/latest/proc_macro2/struct.TokenStream.html)
- As `derive(FromAttr)` also derives [`Parse`](https://docs.rs/syn/latest/syn/parse/trait.Parse.html) so you can use the [parse](https://docs.rs/syn/latest/syn/parse/index.html) API,
e.g. with [`parse_macro_input!(tokens as Attribute)`](https://docs.rs/syn/latest/syn/macro.parse_macro_input.html).
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
//! use syn::{Attribute, parse_quote};
//! use attribute_derive::FromAttr;
//!
//! #[derive(FromAttr, PartialEq, Debug)]
//! #[derive(FromAttr, PartialEq, Debug)]
//! #[attribute(ident = flag)]
//! struct Flag(bool);
//!
Expand Down Expand Up @@ -138,7 +138,7 @@
//! For parsing a single [`TokenStream`] e.g. for parsing the proc macro input
//! there are two ways:
//!
//! - [`FromAttr::from_args`] taking in a [`TokenStream`]
//! - [`FromAttr::from_input`] taking in a [`TokenStream`]
//! - As `derive(FromAttr)` also derives [`Parse`] so you can use the
//! [parse](mod@syn::parse) API, e.g. with [`parse_macro_input!(tokens as
//! Attribute)`](syn::parse_macro_input!).
Expand Down Expand Up @@ -318,7 +318,7 @@ mod tmp {
}

/// Parses from a single attribute. Ignoring the name.
///
///
/// This is available even without `#[attribute(ident = ...)]`, because
/// it ignores the attribute's path, allowing to use it to parse e.g.
/// literals:
Expand Down
8 changes: 4 additions & 4 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,20 @@ use crate::{SpannedValue, *};
/// }
///
/// assert_eq!(
/// Test::from_args(quote!(param)).unwrap().param,
/// Test::from_input(quote!(param)).unwrap().param,
/// FlagOrValue::Flag
/// );
/// assert_eq!(
/// Test::from_args(quote!(param = "value")).unwrap().param,
/// Test::from_input(quote!(param = "value")).unwrap().param,
/// FlagOrValue::Value("value".into())
/// );
/// assert_eq!(
/// Test::from_args(quote!(param, param = "value", param))
/// Test::from_input(quote!(param, param = "value", param))
/// .unwrap()
/// .param,
/// FlagOrValue::Value("value".into())
/// );
/// assert_eq!(Test::from_args(quote!()).unwrap().param, FlagOrValue::None);
/// assert_eq!(Test::from_input(quote!()).unwrap().param, FlagOrValue::None);
/// ```
#[derive(Debug, Default, PartialEq, Eq, PartialOrd, Ord)]
pub enum FlagOrValue<T> {
Expand Down
Loading