Skip to content

Commit 9cff115

Browse files
Gealepage
andauthored
Ensure all examples compile (#1604)
* docs: Remove unused example * docs: Ensure all examples compile Co-authored-by: Ed Page <[email protected]>
1 parent b66ff43 commit 9cff115

File tree

4 files changed

+78
-91
lines changed

4 files changed

+78
-91
lines changed

Cargo.toml

+10
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,21 @@ name = "reborrow_fold"
104104
name = "fnmut"
105105
required-features = ["alloc"]
106106

107+
[[example]]
108+
name = "custom_error"
109+
required-features = ["alloc"]
110+
path = "examples/custom_error.rs"
111+
107112
[[example]]
108113
name = "json"
109114
required-features = ["alloc"]
110115
path = "examples/json.rs"
111116

117+
[[example]]
118+
name = "json_iterator"
119+
required-features = ["alloc"]
120+
path = "examples/json_iterator.rs"
121+
112122
[[example]]
113123
name = "iterator"
114124
path = "examples/iterator.rs"

examples/custom_error.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@ impl<I> ParseError<I> for CustomError<I> {
2121
}
2222
}
2323

24-
fn parse(input: &str) -> IResult<&str, &str, CustomError<&str>> {
24+
pub fn parse(_input: &str) -> IResult<&str, &str, CustomError<&str>> {
2525
Err(Error(CustomError::MyError))
2626
}
2727

28+
fn main() {}
29+
2830
#[cfg(test)]
2931
mod tests {
3032
use super::parse;

examples/json_iterator.rs

+65-73
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,25 @@ use nom::{
44
branch::alt,
55
bytes::complete::{escaped, tag, take_while},
66
character::complete::{alphanumeric1 as alphanumeric, char, one_of},
7-
combinator::{map, opt, cut},
8-
error::{context, ErrorKind, ParseError},
9-
error::{VerboseError, VerboseErrorKind},
10-
multi::separated_list,
7+
combinator::{cut, map},
8+
error::{context, ParseError},
9+
multi::separated_list0,
1110
number::complete::double,
12-
sequence::{delimited, preceded, separated_pair, terminated},
13-
Err, IResult, Offset,
11+
sequence::{preceded, separated_pair, terminated},
12+
IResult,
1413
};
1514
use std::collections::HashMap;
1615

17-
use std::str;
1816
use std::cell::Cell;
19-
20-
struct Cursor<'a> {
21-
inner: &'a str,
22-
offset: usize,
23-
}
17+
use std::str;
2418

2519
#[derive(Clone, Debug)]
2620
pub struct JsonValue<'a, 'b> {
2721
input: &'a str,
2822
pub offset: &'b Cell<usize>,
2923
}
3024

31-
impl<'a, 'b:'a> JsonValue<'a, 'b> {
25+
impl<'a, 'b: 'a> JsonValue<'a, 'b> {
3226
pub fn new(input: &'a str, offset: &'b Cell<usize>) -> JsonValue<'a, 'b> {
3327
JsonValue { input, offset }
3428
}
@@ -49,7 +43,7 @@ impl<'a, 'b:'a> JsonValue<'a, 'b> {
4943
self.offset(i);
5044
println!("-> {}", s);
5145
Some(s)
52-
},
46+
}
5347
_ => None,
5448
}
5549
}
@@ -61,27 +55,27 @@ impl<'a, 'b:'a> JsonValue<'a, 'b> {
6155
self.offset(i);
6256
println!("-> {}", o);
6357
Some(o)
64-
},
58+
}
6559
_ => None,
6660
}
6761
}
6862

6963
pub fn number(&self) -> Option<f64> {
7064
println!("number()");
71-
match double::<_,()>(self.data()) {
65+
match double::<_, ()>(self.data()) {
7266
Ok((i, o)) => {
7367
self.offset(i);
7468
println!("-> {}", o);
7569
Some(o)
76-
},
70+
}
7771
_ => None,
7872
}
7973
}
8074

81-
pub fn array(&self) -> Option<impl Iterator<Item=JsonValue<'a, 'b>>> {
75+
pub fn array(&self) -> Option<impl Iterator<Item = JsonValue<'a, 'b>>> {
8276
println!("array()");
8377

84-
match tag::<_,_,()>("[")(self.data()) {
78+
match tag::<_, _, ()>("[")(self.data()) {
8579
Err(_) => None,
8680
Ok((i, _)) => {
8781
println!("[");
@@ -92,7 +86,7 @@ impl<'a, 'b:'a> JsonValue<'a, 'b> {
9286

9387
let v = self.clone();
9488

95-
Some(std::iter::from_fn(move|| {
89+
Some(std::iter::from_fn(move || {
9690
if done {
9791
return None;
9892
}
@@ -103,30 +97,29 @@ impl<'a, 'b:'a> JsonValue<'a, 'b> {
10397
match value(v.data()) {
10498
Ok((i, _)) => {
10599
v.offset(i);
106-
},
107-
Err(_) => {},
100+
}
101+
Err(_) => {}
108102
}
109103
}
110104

111-
112-
match tag::<_,_,()>("]")(v.data()) {
105+
match tag::<_, _, ()>("]")(v.data()) {
113106
Ok((i, _)) => {
114107
println!("]");
115108
v.offset(i);
116109
done = true;
117110
return None;
118-
},
111+
}
119112
Err(_) => {}
120113
};
121114

122115
if first {
123116
first = false;
124117
} else {
125-
match tag::<_,_,()>(",")(v.data()) {
118+
match tag::<_, _, ()>(",")(v.data()) {
126119
Ok((i, _)) => {
127-
println!(",");
120+
println!(",");
128121
v.offset(i);
129-
},
122+
}
130123
Err(_) => {
131124
done = true;
132125
return None;
@@ -137,15 +130,14 @@ impl<'a, 'b:'a> JsonValue<'a, 'b> {
137130
println!("-> {}", v.data());
138131
previous = v.offset.get();
139132
Some(v.clone())
140-
141133
}))
142134
}
143135
}
144136
}
145137

146-
pub fn object(&self) -> Option<impl Iterator<Item=(&'a str, JsonValue<'a, 'b>)>> {
138+
pub fn object(&self) -> Option<impl Iterator<Item = (&'a str, JsonValue<'a, 'b>)>> {
147139
println!("object()");
148-
match tag::<_,_,()>("{")(self.data()) {
140+
match tag::<_, _, ()>("{")(self.data()) {
149141
Err(_) => None,
150142
Ok((i, _)) => {
151143
self.offset(i);
@@ -158,7 +150,7 @@ impl<'a, 'b:'a> JsonValue<'a, 'b> {
158150

159151
let v = self.clone();
160152

161-
Some(std::iter::from_fn(move|| {
153+
Some(std::iter::from_fn(move || {
162154
if done {
163155
return None;
164156
}
@@ -169,29 +161,29 @@ impl<'a, 'b:'a> JsonValue<'a, 'b> {
169161
match value(v.data()) {
170162
Ok((i, _)) => {
171163
v.offset(i);
172-
},
173-
Err(_) => {},
164+
}
165+
Err(_) => {}
174166
}
175167
}
176168

177-
match tag::<_,_,()>("}")(v.data()) {
169+
match tag::<_, _, ()>("}")(v.data()) {
178170
Ok((i, _)) => {
179171
println!("}}");
180172
v.offset(i);
181173
done = true;
182174
return None;
183-
},
175+
}
184176
Err(_) => {}
185177
};
186178

187179
if first {
188180
first = false;
189181
} else {
190-
match tag::<_,_,()>(",")(v.data()) {
182+
match tag::<_, _, ()>(",")(v.data()) {
191183
Ok((i, _)) => {
192184
println!(",");
193185
v.offset(i);
194-
},
186+
}
195187
Err(_) => {
196188
done = true;
197189
return None;
@@ -203,7 +195,7 @@ impl<'a, 'b:'a> JsonValue<'a, 'b> {
203195
Ok((i, key)) => {
204196
v.offset(i);
205197

206-
match tag::<_,_,()>(":")(v.data()) {
198+
match tag::<_, _, ()>(":")(v.data()) {
207199
Err(_) => None,
208200
Ok((i, _)) => {
209201
v.offset(i);
@@ -214,10 +206,9 @@ impl<'a, 'b:'a> JsonValue<'a, 'b> {
214206
Some((key, v.clone()))
215207
}
216208
}
217-
},
209+
}
218210
_ => None,
219211
}
220-
221212
}))
222213
}
223214
}
@@ -235,47 +226,44 @@ fn parse_str<'a, E: ParseError<&'a str>>(i: &'a str) -> IResult<&'a str, &'a str
235226
}
236227

237228
fn string<'a>(i: &'a str) -> IResult<&'a str, &'a str> {
238-
context("string",
239-
preceded(
240-
char('\"'),
241-
cut(terminated(
242-
parse_str,
243-
char('\"')
244-
))))(i)
229+
context(
230+
"string",
231+
preceded(char('\"'), cut(terminated(parse_str, char('\"')))),
232+
)(i)
245233
}
246234

247235
fn boolean<'a>(input: &'a str) -> IResult<&'a str, bool> {
248-
alt((
249-
map(tag("false"), |_| false),
250-
map(tag("true"), |_| true)
251-
))(input)
236+
alt((map(tag("false"), |_| false), map(tag("true"), |_| true)))(input)
252237
}
253238

254239
fn array<'a>(i: &'a str) -> IResult<&'a str, ()> {
255240
context(
256241
"array",
257-
preceded(char('['),
258-
cut(terminated(
259-
map(separated_list(preceded(sp, char(',')), value), |_| ()),
260-
preceded(sp, char(']'))))
261-
))(i)
242+
preceded(
243+
char('['),
244+
cut(terminated(
245+
map(separated_list0(preceded(sp, char(',')), value), |_| ()),
246+
preceded(sp, char(']')),
247+
)),
248+
),
249+
)(i)
262250
}
263251

264252
fn key_value<'a>(i: &'a str) -> IResult<&'a str, (&'a str, ())> {
265-
separated_pair(preceded(sp, string), cut(preceded(sp, char(':'))), value)(i)
253+
separated_pair(preceded(sp, string), cut(preceded(sp, char(':'))), value)(i)
266254
}
267255

268256
fn hash<'a>(i: &'a str) -> IResult<&'a str, ()> {
269257
context(
270258
"map",
271-
preceded(char('{'),
272-
cut(terminated(
273-
map(
274-
separated_list(preceded(sp, char(',')), key_value),
275-
|_| ()),
276-
preceded(sp, char('}')),
277-
))
278-
))(i)
259+
preceded(
260+
char('{'),
261+
cut(terminated(
262+
map(separated_list0(preceded(sp, char(',')), key_value), |_| ()),
263+
preceded(sp, char('}')),
264+
)),
265+
),
266+
)(i)
279267
}
280268

281269
fn value<'a>(i: &'a str) -> IResult<&'a str, ()> {
@@ -314,13 +302,17 @@ fn main() {
314302
let parser = JsonValue::new(data, &offset);
315303

316304
if let Some(o) = parser.object() {
317-
let s: HashMap<&str, &str> = o.filter(|(k,_)| *k == "users" )
318-
.filter_map(|(_, v)| v.object()).flatten()
319-
.filter_map(|(user, v)| v.object().map(|o| (user, o)))
320-
.map(|(user, o)| {
321-
o.filter(|(k,_)| *k == "city" )
322-
.filter_map(move |(_, v)| v.string().map(|s| (user, s)))
323-
}).flatten().collect();
305+
let s: HashMap<&str, &str> = o
306+
.filter(|(k, _)| *k == "users")
307+
.filter_map(|(_, v)| v.object())
308+
.flatten()
309+
.filter_map(|(user, v)| v.object().map(|o| (user, o)))
310+
.map(|(user, o)| {
311+
o.filter(|(k, _)| *k == "city")
312+
.filter_map(move |(_, v)| v.string().map(|s| (user, s)))
313+
})
314+
.flatten()
315+
.collect();
324316

325317
println!("res = {:?}", s);
326318
}

examples/macro.rs

-17
This file was deleted.

0 commit comments

Comments
 (0)