[][src]Trait syn::ext::IdentExt

pub trait IdentExt: Sized + Sealed {
    fn parse_any(input: ParseStream) -> Result<Self>;
}

Additional parsing methods for Ident.

This trait is sealed and cannot be implemented for types outside of Syn.

This trait is available if Syn is built with the "parsing" feature.

Required methods

fn parse_any(input: ParseStream) -> Result<Self>

Parses any identifier including keywords.

This is useful when parsing a DSL which allows Rust keywords as identifiers.

This code runs with edition 2018
use syn::{Error, Ident, Result, Token};
use syn::ext::IdentExt;
use syn::parse::ParseStream;

// Parses input that looks like `name = NAME` where `NAME` can be
// any identifier.
//
// Examples:
//
//     name = anything
//     name = impl
fn parse_dsl(input: ParseStream) -> Result<Ident> {
    let name_token: Ident = input.parse()?;
    if name_token != "name" {
        return Err(Error::new(name_token.span(), "expected `name`"));
    }
    input.parse::<Token![=]>()?;
    let name = input.call(Ident::parse_any)?;
    Ok(name)
}
Loading content...

Implementors

impl IdentExt for Ident
[src]

Loading content...