Skip to content

Commit f33a0c2

Browse files
spazmms705
authored andcommitted
extract repeated code from create_option parsing.
* Type signature for higher order combinators gets verbose and messy quickly. * The generic nature of create_option_equals_pair requires that ws_sep_equals also be generic over I. * I would have been happy to make create_option_equals_pair work over &u[8] directly, rather than a parametized I.
1 parent b595a37 commit f33a0c2

File tree

2 files changed

+55
-63
lines changed

2 files changed

+55
-63
lines changed

src/common.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -796,6 +796,16 @@ pub(crate) fn ws_sep_comma(i: &[u8]) -> IResult<&[u8], &[u8]> {
796796
delimited(multispace0, tag(","), multispace0)(i)
797797
}
798798

799+
pub(crate) fn ws_sep_equals<'a, I>(i: I) -> IResult<I, I>
800+
where
801+
I: nom::InputTakeAtPosition + nom::InputTake + nom::Compare<&'a str>,
802+
// Compare required by tag
803+
<I as nom::InputTakeAtPosition>::Item: nom::AsChar + Clone,
804+
// AsChar and Clone required by multispace0
805+
{
806+
delimited(multispace0, tag("="), multispace0)(i)
807+
}
808+
799809
pub fn assignment_expr_list(i: &[u8]) -> IResult<&[u8], Vec<(Column, FieldValueExpression)>> {
800810
many1(terminated(assignment_expr, opt(ws_sep_comma)))(i)
801811
}

src/create_table_options.rs

Lines changed: 45 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
use nom::character::complete::{alphanumeric1, multispace0, multispace1};
22

3-
use common::{integer_literal, sql_identifier, string_literal, ws_sep_comma};
3+
use common::{integer_literal, sql_identifier, string_literal, ws_sep_comma,
4+
ws_sep_equals};
45
use nom::branch::alt;
56
use nom::bytes::complete::{tag, tag_no_case};
67
use nom::combinator::{map, opt};
78
use nom::multi::separated_list;
8-
use nom::sequence::{preceded, tuple};
9+
use nom::sequence::tuple;
910
use nom::IResult;
1011

1112
pub fn table_options(i: &[u8]) -> IResult<&[u8], ()> {
@@ -35,58 +36,55 @@ fn create_option(i: &[u8]) -> IResult<&[u8], ()> {
3536
))(i)
3637
}
3738

39+
40+
/// Helper to parse equals-separated create option pairs.
41+
/// Throws away the create option and value
42+
pub fn create_option_equals_pair<'a, I, O1, O2, F, G>(first: F, second: G) -> impl Fn(I) -> IResult<I, ()>
43+
where
44+
F: Fn(I) -> IResult<I, O1>,
45+
G: Fn(I) -> IResult<I, O2>,
46+
I: nom::InputTakeAtPosition + nom::InputTake + nom::Compare<&'a str>,
47+
<I as nom::InputTakeAtPosition>::Item: nom::AsChar + Clone,
48+
{
49+
move |i: I| {
50+
let (i, _o1) = first(i)?;
51+
let (i, _) = ws_sep_equals(i)?;
52+
let (i, _o2) = second(i)?;
53+
Ok(( i, () ))
54+
}
55+
}
56+
3857
fn create_option_type(i: &[u8]) -> IResult<&[u8], ()> {
39-
map(
40-
preceded(
41-
tag_no_case("type"),
42-
preceded(
43-
multispace0,
44-
preceded(tag("="), preceded(multispace0, alphanumeric1)),
45-
),
46-
),
47-
|_| (),
58+
create_option_equals_pair(
59+
tag_no_case("type"),
60+
alphanumeric1
4861
)(i)
4962
}
5063

5164
fn create_option_pack_keys(i: &[u8]) -> IResult<&[u8], ()> {
52-
let (remaining_input, (_, _, _, _, _)) = tuple((
65+
create_option_equals_pair(
5366
tag_no_case("pack_keys"),
54-
multispace0,
55-
tag("="),
56-
multispace0,
57-
alt((tag("0"), tag("1"))),
58-
))(i)?;
59-
Ok((remaining_input, ()))
67+
alt((tag("0"), tag("1")))
68+
)(i)
6069
}
6170

6271
fn create_option_engine(i: &[u8]) -> IResult<&[u8], ()> {
63-
let (remaining_input, (_, _, _, _, _)) = tuple((
72+
create_option_equals_pair(
6473
tag_no_case("engine"),
65-
multispace0,
66-
tag("="),
67-
multispace0,
68-
opt(alphanumeric1),
69-
))(i)?;
70-
Ok((remaining_input, ()))
74+
opt(alphanumeric1)
75+
)(i)
7176
}
7277

7378
fn create_option_auto_increment(i: &[u8]) -> IResult<&[u8], ()> {
74-
let (remaining_input, (_, _, _, _, _)) = tuple((
79+
create_option_equals_pair(
7580
tag_no_case("auto_increment"),
76-
multispace0,
77-
tag("="),
78-
multispace0,
7981
integer_literal,
80-
))(i)?;
81-
Ok((remaining_input, ()))
82+
)(i)
8283
}
8384

8485
fn create_option_default_charset(i: &[u8]) -> IResult<&[u8], ()> {
85-
let (remaining_input, (_, _, _, _, _)) = tuple((
86+
create_option_equals_pair(
8687
tag_no_case("default charset"),
87-
multispace0,
88-
tag("="),
89-
multispace0,
9088
alt((
9189
tag("utf8mb4"),
9290
tag("utf8"),
@@ -95,55 +93,39 @@ fn create_option_default_charset(i: &[u8]) -> IResult<&[u8], ()> {
9593
tag("ucs2"),
9694
tag("latin1"),
9795
)),
98-
))(i)?;
99-
Ok((remaining_input, ()))
96+
)(i)
10097
}
10198

10299
fn create_option_collate(i: &[u8]) -> IResult<&[u8], ()> {
103-
let (remaining_input, (_, _, _, _, _)) = tuple((
100+
create_option_equals_pair(
104101
tag_no_case("collate"),
105-
multispace0,
106-
tag("="),
107-
multispace0,
108102
// TODO(malte): imprecise hack, should not accept everything
109103
sql_identifier,
110-
))(i)?;
111-
Ok((remaining_input, ()))
104+
)(i)
112105
}
113106

114107
fn create_option_comment(i: &[u8]) -> IResult<&[u8], ()> {
115-
let (remaining_input, (_, _, _, _, _)) = tuple((
108+
create_option_equals_pair(
116109
tag_no_case("comment"),
117-
multispace0,
118-
tag("="),
119-
multispace0,
120110
string_literal,
121-
))(i)?;
122-
Ok((remaining_input, ()))
111+
)(i)
123112
}
124113

125114
fn create_option_max_rows(i: &[u8]) -> IResult<&[u8], ()> {
126-
let (remaining_input, (_, _, _, _, _)) = tuple((
115+
create_option_equals_pair(
127116
tag_no_case("max_rows"),
128-
multispace0,
129-
opt(tag("=")),
130-
multispace0,
131-
integer_literal,
132-
))(i)?;
133-
Ok((remaining_input, ()))
117+
integer_literal
118+
)(i)
134119
}
135120

136121
fn create_option_avg_row_length(i: &[u8]) -> IResult<&[u8], ()> {
137-
let (remaining_input, (_, _, _, _, _)) = tuple((
122+
create_option_equals_pair(
138123
tag_no_case("avg_row_length"),
139-
multispace0,
140-
opt(tag("=")),
141-
multispace0,
142-
integer_literal,
143-
))(i)?;
144-
Ok((remaining_input, ()))
124+
integer_literal
125+
)(i)
145126
}
146127

128+
147129
fn create_option_row_format(i: &[u8]) -> IResult<&[u8], ()> {
148130
let (remaining_input, (_, _, _, _, _)) = tuple((
149131
tag_no_case("row_format"),

0 commit comments

Comments
 (0)