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>
impl<'a> Attrs<'a>
Sourcepub fn once<K, F>(&mut self, key: K, f: F) -> &mut Self
pub fn once<K, F>(&mut self, key: K, f: F) -> &mut Self
Parse tokens following key using f, at most once.
See crate documentation for more.
§Panics
- If
keyhas already been registered. - If
keyis an invalid ident.
Sourcepub fn many<K, F>(&mut self, key: K, f: F) -> &mut Self
pub fn many<K, F>(&mut self, key: K, f: F) -> &mut Self
Parse tokens following key using f, potentially many times.
See crate documentation for more.
§Panics
- If
keyhas already been registered. - If
keyis an invalid ident.
Sourcepub fn fallback<F>(&mut self, f: F) -> &mut Self
pub fn fallback<F>(&mut self, f: F) -> &mut Self
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());Sourcepub fn alias<A, K>(&mut self, alias: A, key: K) -> &mut Selfwhere
A: UnwrapIdent,
K: UnwrapIdent,
pub fn alias<A, K>(&mut self, alias: A, key: K) -> &mut Selfwhere
A: UnwrapIdent,
K: UnwrapIdent,
If the key alias is encountered, call the parser for key.
See module documentation for more.
§Panics
- If
aliashas already been registered. - If
aliasis an invalid ident. - If
keyhas not been registered.
Sourcepub fn parse_attrs<Q>(&mut self, path: &Q, attrs: &[Attribute]) -> Result<()>
pub fn parse_attrs<Q>(&mut self, path: &Q, attrs: &[Attribute]) -> Result<()>
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);Sourcepub fn extract_from<Q>(
&mut self,
path: &Q,
attrs: &mut Vec<Attribute>,
) -> Result<()>
pub fn extract_from<Q>( &mut self, path: &Q, attrs: &mut Vec<Attribute>, ) -> Result<()>
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 thereTrait Implementations§
Source§impl Parser for &mut Attrs<'_>
Borrow from this object as a Parser.
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§impl Parser for Attrs<'_>
Move this object into a Parser.
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);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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more