Skip to content

Commit 8f46091

Browse files
committed
Upgrade to nom 8
This also refactors the internal stuff completely
1 parent c233965 commit 8f46091

15 files changed

+486
-408
lines changed

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "nom-parse-macros"
33
description = "Procedural macros for generating parser functions for the nom libary"
4-
version = "0.2.0"
4+
version = "0.3.0"
55
license = "MIT/Apache-2.0"
66
keywords = ["nom", "parser", "parsable"]
77
categories = ["parsing"]
@@ -43,5 +43,5 @@ proc-macro2 = "1.0"
4343
syn = { version = "2.0", features = ["extra-traits", "full", "visit", "visit-mut"] }
4444
itertools = "0.14.0"
4545
phf = { version = "0.11.2", features = ["macros"] }
46-
nom-parse-trait = "0.2.0"
47-
nom = "7.1.3"
46+
nom-parse-trait = "0.3.0"
47+
nom = "8.0.0"

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ As a quick example, consider the following struct:
1919
```rust
2020
use nom_parse_macros::parse_from;
2121

22-
#[parse_from(tuple(be_32, be_u16))]
22+
#[parse_from((be_32, be_u16))]
2323
struct MyStruct {
2424
a: u32,
2525
b: u16,

examples/basic_struct_with_members.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use nom::IResult;
66
use nom_parse_macros::parse_from;
77
use nom_parse_trait::ParseFrom;
88

9-
#[parse_from(separated_pair({}, tuple(space0, ",", space0), {}))]
9+
#[parse_from(separated_pair({}, (space0, ",", space0), {}))]
1010
#[derive(Debug, PartialEq)]
1111
struct NumberPair {
1212
x: u32,

examples/derived_fields.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
//! This example shows how you can use the derived attribute.
2-
//! A derived field is not actually parsed, but derived from all of the other
2+
//! A derived field is not actually parsed, but derived from all the other
33
//! fields that are parsed.
44
55
use nom::IResult;
66
use nom_parse_macros::parse_from;
77
use nom_parse_trait::ParseFrom;
88

9-
#[parse_from(separated_pair({}, tuple(space0, ",", space0), {}))]
9+
#[parse_from(separated_pair({}, (space0, ",", space0), {}))]
1010
#[derive(Debug, PartialEq)]
1111
struct NumberPair {
1212
x: u32,

examples/use_matching_string.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
//! the characters are matched verbatim.
44
55
use nom::IResult;
6-
use nom_parse_macros::parse_match;
6+
use nom_parse_macros::parse_from;
77
use nom_parse_trait::ParseFrom;
88

9-
#[parse_match("({},{},{})")]
9+
#[parse_from(match "({},{},{})")]
1010
#[derive(Debug, PartialEq)]
1111
struct Vector3 {
1212
x: u32,

src/fields.rs

Lines changed: 27 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ use syn::punctuated::Punctuated;
66
use syn::visit_mut::VisitMut;
77
use syn::{Expr, FieldsNamed, FieldsUnnamed, Path, Result, Type};
88

9-
pub enum Field {
10-
Default { name: Ident, ty: Type },
9+
pub enum FieldFormat {
10+
Expression { name: Ident, ty: Type },
1111
Derived { name: Ident, ty: Type, expr: Expr },
1212
}
1313

1414
pub struct Fields {
1515
pub(crate) is_named: bool,
16-
pub(crate) fields: Vec<Field>,
16+
pub(crate) fields_format: Vec<FieldFormat>,
1717
}
1818

1919
pub fn parse_fields(fields: &mut syn::Fields) -> Result<Fields> {
@@ -22,7 +22,7 @@ pub fn parse_fields(fields: &mut syn::Fields) -> Result<Fields> {
2222
syn::Fields::Unnamed(unnamed_fields) => parse_unnamed_fields(unnamed_fields),
2323
syn::Fields::Unit => Ok(Fields {
2424
is_named: false,
25-
fields: Vec::new(),
25+
fields_format: Vec::new(),
2626
}),
2727
}
2828
}
@@ -33,7 +33,7 @@ fn parse_named_fields(fields: &mut FieldsNamed) -> Result<Fields> {
3333
})
3434
.map(|fields| Fields {
3535
is_named: true,
36-
fields,
36+
fields_format: fields,
3737
})
3838
}
3939

@@ -43,14 +43,14 @@ fn parse_unnamed_fields(fields: &mut FieldsUnnamed) -> Result<Fields> {
4343
})
4444
.map(|fields| Fields {
4545
is_named: false,
46-
fields,
46+
fields_format: fields,
4747
})
4848
}
4949

5050
fn parse_field_iterator<'a>(
5151
fields: impl Iterator<Item = &'a mut syn::Field>,
5252
get_name: impl Fn(usize, &syn::Field) -> Ident,
53-
) -> Result<Vec<Field>> {
53+
) -> Result<Vec<FieldFormat>> {
5454
let mut result = Vec::new();
5555

5656
for (index, field) in fields.enumerate() {
@@ -65,43 +65,34 @@ fn parse_field_iterator<'a>(
6565
{
6666
let expr = attr.parse_args::<Expr>()?;
6767
field.attrs.remove(ix);
68-
result.push(Field::Derived { name, ty, expr });
68+
result.push(FieldFormat::Derived { name, ty, expr });
6969
} else {
70-
result.push(Field::Default { name, ty });
70+
result.push(FieldFormat::Expression { name, ty });
7171
}
7272
}
7373

7474
Ok(result)
7575
}
7676

77-
impl Field {
78-
pub fn generate_expression(&self) -> Option<TokenStream> {
79-
match self {
80-
Field::Default { name, ty } => Some(quote! {
81-
let (input, #name): (_, #ty) = nom_parse_trait::ParseFrom::parse(input)?;
82-
}),
83-
Field::Derived { .. } => None,
84-
}
85-
}
86-
77+
impl FieldFormat {
8778
pub fn get_name(&self) -> &Ident {
8879
match self {
89-
Field::Default { name, .. } => name,
90-
Field::Derived { name, .. } => name,
80+
FieldFormat::Expression { name, .. } => name,
81+
FieldFormat::Derived { name, .. } => name,
9182
}
9283
}
9384

9485
pub fn get_type(&self) -> &Type {
9586
match self {
96-
Field::Default { ty, .. } => ty,
97-
Field::Derived { ty, .. } => ty,
87+
FieldFormat::Expression { ty, .. } => ty,
88+
FieldFormat::Derived { ty, .. } => ty,
9889
}
9990
}
10091

10192
pub fn generate_derived_expression(&self, fields: &Fields) -> Option<TokenStream> {
10293
match self {
103-
Field::Default { .. } => None,
104-
Field::Derived { expr, ty, .. } => {
94+
FieldFormat::Expression { .. } => None,
95+
FieldFormat::Derived { expr, ty, .. } => {
10596
let name = self.get_param_name();
10697
let mut expr = expr.clone();
10798
fields.rename_derive_expr(&mut expr);
@@ -120,39 +111,39 @@ impl Field {
120111
impl Fields {
121112
pub fn get_creation_names(&self) -> Vec<TokenStream> {
122113
let transform = if self.is_named {
123-
|field: &Field| {
114+
|field: &FieldFormat| {
124115
let name = field.get_name();
125116
let param_name = field.get_param_name();
126117
quote! { #name: #param_name }
127118
}
128119
} else {
129-
|field: &Field| {
120+
|field: &FieldFormat| {
130121
let name = field.get_param_name();
131122
quote! { #name }
132123
}
133124
};
134125

135-
self.fields.iter().map(transform).collect()
126+
self.fields_format.iter().map(transform).collect()
136127
}
137128

138129
pub fn get_expression_names(&self) -> Vec<Ident> {
139-
self.fields
130+
self.fields_format
140131
.iter()
141-
.filter(|field| !matches!(field, Field::Derived { .. }))
142-
.map(Field::get_param_name)
132+
.filter(|field| !matches!(field, FieldFormat::Derived { .. }))
133+
.map(FieldFormat::get_param_name)
143134
.collect()
144135
}
145136

146137
pub fn get_expression_types(&self) -> Vec<Type> {
147-
self.fields
138+
self.fields_format
148139
.iter()
149-
.filter(|field| !matches!(field, Field::Derived { .. }))
140+
.filter(|field| !matches!(field, FieldFormat::Derived { .. }))
150141
.map(|field| field.get_type().clone())
151142
.collect()
152143
}
153144

154145
pub fn get_derived_expressions(&self) -> Vec<TokenStream> {
155-
self.fields
146+
self.fields_format
156147
.iter()
157148
.filter_map(|field| field.generate_derived_expression(self))
158149
.collect()
@@ -199,9 +190,9 @@ impl Fields {
199190
}
200191

201192
let mapping = self
202-
.fields
193+
.fields_format
203194
.iter()
204-
.filter(|field| !matches!(field, Field::Derived { .. }))
195+
.filter(|field| !matches!(field, FieldFormat::Derived { .. }))
205196
.map(|field| (field.get_name().clone(), field.get_param_name()))
206197
.collect();
207198

0 commit comments

Comments
 (0)