Struct Attrs

Source
pub struct Attrs<'a> { /* private fields */ }
Expand description

Ergonomic Parser for #[attributes].

See crate documentation for more.

let mut untagged = false;
let mut krate = None::<syn::Path>;

let parseme: syn::Attribute = syn::parse_quote! {
    #[serde(untagged, crate = "path::to::serde")]
};

parseme.parse_args_with(
    Attrs::new()
        .once("untagged", set::flag(&mut untagged))
        .once("crate", with::eq(set::parse_str(&mut krate)))
)?;

assert!(krate.is_some() && untagged);

Implementations§

Source§

impl<'a> Attrs<'a>

Source

pub fn new() -> Self

Create a new empty parser.

Source

pub fn contains<Q>(&self, key: &Q) -> bool
where Q: ?Sized, Ident: PartialEq<Q>,

Whether key already exists in this parser.

Source

pub fn once<K, F>(&mut self, key: K, f: F) -> &mut Self
where K: UnwrapIdent, F: 'a + FnOnce(ParseStream<'_>) -> Result<()>,

Parse tokens following key using f, at most once.

See crate documentation for more.

§Panics
  • If key has already been registered.
  • If key is an invalid ident.
Source

pub fn many<K, F>(&mut self, key: K, f: F) -> &mut Self
where K: UnwrapIdent, F: 'a + FnMut(ParseStream<'_>) -> Result<()>,

Parse tokens following key using f, potentially many times.

See crate documentation for more.

§Panics
  • If key has already been registered.
  • If key is an invalid ident.
Source

pub fn fallback<F>(&mut self, f: F) -> &mut Self
where F: 'a + FnMut(&Ident, ParseStream<'_>) -> Result<()>,

Parse unrecognised keys using f.

let mut krate = None::<syn::Path>;

let parseme: syn::Attribute = syn::parse_quote! {
    #[serde(crate = "path::to::serde")]
};

parseme.parse_args_with(Attrs::new().fallback(|key, input| {
    assert_eq!(key, "crate");
    input.parse::<syn::Token![=]>()?;
    krate = Some(input.parse::<syn::LitStr>()?.parse()?);
    Ok(())
}))?;

assert!(krate.is_some());
Source

pub fn alias<A, K>(&mut self, alias: A, key: K) -> &mut Self
where A: UnwrapIdent, K: UnwrapIdent,

If the key alias is encountered, call the parser for key.

See module documentation for more.

§Panics
  • If alias has already been registered.
  • If alias is an invalid ident.
  • If key has not been registered.
Source

pub fn parse_attrs<Q>(&mut self, path: &Q, attrs: &[Attribute]) -> Result<()>
where Q: ?Sized, Ident: PartialEq<Q>,

Parse all the Attributes where their path is the given path.


let mut rename_all = None::<String>;
let mut untagged = false;
let mut deny_unknown_fields = false;
let attrs: Vec<Attribute> = parse_quote! {
    #[serde(rename_all = "kebab-case", untagged)]
    #[default] // SKIPPED
    #[serde(deny_unknown_fields)]
};

Attrs::new()
    .once("rename_all", with::eq(set::from_str(&mut rename_all)))
    .once("untagged", set::flag(&mut untagged))
    .once("deny_unknown_fields", set::flag(&mut deny_unknown_fields))
    .parse_attrs("serde", &attrs)?;

assert!(rename_all.is_some() && untagged && deny_unknown_fields);
Source

pub fn extract_from<Q>( &mut self, path: &Q, attrs: &mut Vec<Attribute>, ) -> Result<()>
where Q: ?Sized, Ident: PartialEq<Q>,

Parse and remove all the Attributes where their path is the given path.


let mut rename_all = None::<String>;
let mut untagged = false;
let mut deny_unknown_fields = false;
let mut attrs: Vec<Attribute> = parse_quote! {
    #[serde(rename_all = "kebab-case", untagged)]
    #[default] // SKIPPED
    #[serde(deny_unknown_fields)]
};

Attrs::new()
    .once("rename_all", with::eq(set::from_str(&mut rename_all)))
    .once("untagged", set::flag(&mut untagged))
    .once("deny_unknown_fields", set::flag(&mut deny_unknown_fields))
    .extract_from("serde", &mut attrs)?;

assert!(rename_all.is_some() && untagged && deny_unknown_fields);
assert_eq!(attrs.len(), 1); // `#[default]` is still there

Trait Implementations§

Source§

impl Debug for Attrs<'_>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a> Default for Attrs<'a>

Source§

fn default() -> Attrs<'a>

Returns the “default value” for a type. Read more
Source§

impl Parser for &mut Attrs<'_>

Borrow from this object as a Parser.

use syn::parse::Parser as _;

let mut untagged = false;
let mut krate = None::<Path>;

Attrs::new()
    .once("untagged", set::flag(&mut untagged))
    .once("crate", with::eq(set::parse_str(&mut krate)))
    .parse_str(r#"untagged, crate = "path::to::serde""#)?;

assert!(krate.is_some() && untagged);
Source§

type Output = ()

Source§

fn parse2(self, tokens: TokenStream) -> Result<Self::Output>

Parse a proc-macro2 token stream into the chosen syntax tree node. Read more
Source§

fn parse_str(self, s: &str) -> Result<Self::Output, Error>

Parse a string of Rust code into the chosen syntax tree node. Read more
Source§

impl Parser for Attrs<'_>

Move this object into a Parser.

use syn::parse::Parser as _;

let mut untagged = false;
let mut krate = None::<Path>;

let mut attrs = Attrs::new();
attrs
    .once("untagged", set::flag(&mut untagged))
    .once("crate", with::eq(set::parse_str(&mut krate)));
attrs.parse_str(r#"untagged, crate = "path::to::serde""#)?;

assert!(krate.is_some() && untagged);
Source§

type Output = ()

Source§

fn parse2(self, tokens: TokenStream) -> Result<Self::Output>

Parse a proc-macro2 token stream into the chosen syntax tree node. Read more
Source§

fn parse_str(self, s: &str) -> Result<Self::Output, Error>

Parse a string of Rust code into the chosen syntax tree node. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for Attrs<'a>

§

impl<'a> !RefUnwindSafe for Attrs<'a>

§

impl<'a> !Send for Attrs<'a>

§

impl<'a> !Sync for Attrs<'a>

§

impl<'a> Unpin for Attrs<'a>

§

impl<'a> !UnwindSafe for Attrs<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.