diff --git a/Cargo.toml b/Cargo.toml index f2ccce5..88b3e69 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "nom-parse-macros" description = "Procedural macros for generating parser functions for the nom libary" -version = "0.4.0" +version = "0.4.1" license = "MIT/Apache-2.0" keywords = ["nom", "parser", "parsable"] categories = ["parsing"] diff --git a/examples/extra_where_clause.rs b/examples/extra_where_clause.rs index a76bbb0..0b56e59 100644 --- a/examples/extra_where_clause.rs +++ b/examples/extra_where_clause.rs @@ -18,11 +18,9 @@ where { move |mut input: I| { let mut array = [T::default(); D]; - for ix in 0..D { - let (rest, _) = prefix.parse(input)?; - let (rest, value) = T::parse(rest)?; - input = rest; - array[ix] = value; + for val in &mut array { + (input, _) = prefix.parse(input)?; + (input, *val) = T::parse(input)?; } Ok((input, array)) } diff --git a/src/fields.rs b/src/fields.rs index 225e733..c00616a 100644 --- a/src/fields.rs +++ b/src/fields.rs @@ -149,6 +149,7 @@ impl Fields { .collect() } + #[allow(clippy::collapsible_else_if)] pub fn create_instance_expr(&self, variant_name: Option<&Ident>) -> TokenStream { let creation_names = self.get_creation_names(); diff --git a/src/lib.rs b/src/lib.rs index 5e76503..8538728 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -17,10 +17,10 @@ //! don't need a ton of use statements in your code. But there are also a couple of special cases: //! //! - `{}` or `()` will be replaced with a [`nom_parse_trait::ParseFrom::parse`] call for the -//! corresponding field. This is useful when you are using types that have implemented the -//! `ParseFrom` trait already. +//! corresponding field. This is useful when you are using types that have implemented the +//! `ParseFrom` trait already. //! - Strings, bytes strings and characters will be translated to match the input verbatim using -//! the [`nom::bytes::complete::tag`] function. +//! the [`nom::bytes::complete::tag`] function. //! //! # Input types that are supported //! @@ -34,13 +34,13 @@ //! # Known limitations //! //! - When your try to use a custom parser combinator, the nom function parser will try to change -//! all parameters to be nom parsers. This is useful in many cases, but when you need to pass in -//! a normal string for example, it won't work. In these cases, you can define a separate function -//! to wrap the call. I'm not sure how to fix that right now, but I'm open to suggestions. +//! all parameters to be nom parsers. This is useful in many cases, but when you need to pass in +//! a normal string for example, it won't work. In these cases, you can define a separate function +//! to wrap the call. I'm not sure how to fix that right now, but I'm open to suggestions. //! //! - Since the generated input type is very generic, all functions that you want to use in the -//! nom expression should also be very generic. In the future I might add a way to specify if you -//! want to generate a specific input type, but for now it's not possible. +//! nom expression should also be very generic. In the future I might add a way to specify if you +//! want to generate a specific input type, but for now it's not possible. extern crate proc_macro; mod fields; @@ -146,7 +146,6 @@ use quote::ToTokens; /// end: [N; D], /// } /// ``` - #[proc_macro_attribute] pub fn parse_from(attrs: TokenStream, object: TokenStream) -> TokenStream { let parse_format = syn::parse_macro_input!(attrs as ParseFormat); diff --git a/src/nom_packages.rs b/src/nom_packages.rs index 782f8f7..7aa33b0 100644 --- a/src/nom_packages.rs +++ b/src/nom_packages.rs @@ -216,7 +216,7 @@ fn parse_call(call: &mut ExprCall) -> Result<()> { if ident == "tuple" || ident == "alt" { let args = call.args.clone(); if args.len() != 1 { - call.args = Punctuated::from(Punctuated::new()); + call.args = Punctuated::new(); call.args.push(Expr::Tuple(ExprTuple { attrs: vec![], paren_token: Default::default(), @@ -228,7 +228,7 @@ fn parse_call(call: &mut ExprCall) -> Result<()> { update_nom_expression(arg)?; } // Nom functions without parameters should not be called, but referenced directly - } else if parameters.len() == 0 { + } else if parameters.is_empty() { if ident != "fail" { return Err(syn::Error::new_spanned( call.func.clone(), diff --git a/src/parser_generator.rs b/src/parser_generator.rs index 554c969..d577e92 100644 --- a/src/parser_generator.rs +++ b/src/parser_generator.rs @@ -54,10 +54,8 @@ impl ToTokens for ParserGenerator { tokens.extend(object.to_token_stream()); - let (mapping_names, mappings): (Vec<_>, Vec<_>) = variants - .iter() - .map(|variant| generate_variant(variant)) - .unzip(); + let (mapping_names, mappings): (Vec<_>, Vec<_>) = + variants.iter().map(generate_variant).unzip(); generate_parser( tokens, @@ -78,7 +76,7 @@ impl ToTokens for ParserGenerator { fn generate_variant(variant: &ParsedVariant) -> (Ident, TokenStream) { let mapping_name = Ident::new( - &format!("map_{}", variant.name.to_string().to_lowercase()), + &format!("parse_{}", variant.name.to_string().to_lowercase()), Span::call_site(), ); let format_expr = variant.format.to_token_stream(); @@ -113,14 +111,16 @@ fn generate_parser( token_stream: &mut TokenStream, name: &Ident, generics: &Generics, - extra_where_clauses: &Vec, + extra_where_clauses: &[WherePredicate], content: impl ToTokens, ) { let (_, type_generics, _) = generics.split_for_impl(); - let parser_generics = parser_generics(&generics, extra_where_clauses); + let parser_generics = parser_generics(generics, extra_where_clauses); let (impl_generics, _, where_statement) = parser_generics.split_for_impl(); token_stream.extend(quote! { + #[automatically_derived] + #[allow(unused)] impl #impl_generics nom_parse_trait::ParseFrom for #name #type_generics #where_statement { @@ -134,7 +134,7 @@ fn generate_parser( }); } -fn parser_generics(generics: &Generics, extra_where_clauses: &Vec) -> Generics { +fn parser_generics(generics: &Generics, extra_where_clauses: &[WherePredicate]) -> Generics { let mut generics = generics.clone(); // If there are no generics, start a new one