Ensure all examples compile (#1604)

* docs: Remove unused example

* docs: Ensure all examples compile

Co-authored-by: Ed Page <eopage@gmail.com>
This commit is contained in:
Geoffroy Couprie 2022-12-30 16:29:22 +01:00 committed by GitHub
parent b66ff43eff
commit 9cff115667
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 78 additions and 91 deletions

View File

@ -104,11 +104,21 @@ name = "reborrow_fold"
name = "fnmut"
required-features = ["alloc"]
[[example]]
name = "custom_error"
required-features = ["alloc"]
path = "examples/custom_error.rs"
[[example]]
name = "json"
required-features = ["alloc"]
path = "examples/json.rs"
[[example]]
name = "json_iterator"
required-features = ["alloc"]
path = "examples/json_iterator.rs"
[[example]]
name = "iterator"
path = "examples/iterator.rs"

View File

@ -21,10 +21,12 @@ impl<I> ParseError<I> for CustomError<I> {
}
}
fn parse(input: &str) -> IResult<&str, &str, CustomError<&str>> {
pub fn parse(_input: &str) -> IResult<&str, &str, CustomError<&str>> {
Err(Error(CustomError::MyError))
}
fn main() {}
#[cfg(test)]
mod tests {
use super::parse;

View File

@ -4,23 +4,17 @@ use nom::{
branch::alt,
bytes::complete::{escaped, tag, take_while},
character::complete::{alphanumeric1 as alphanumeric, char, one_of},
combinator::{map, opt, cut},
error::{context, ErrorKind, ParseError},
error::{VerboseError, VerboseErrorKind},
multi::separated_list,
combinator::{cut, map},
error::{context, ParseError},
multi::separated_list0,
number::complete::double,
sequence::{delimited, preceded, separated_pair, terminated},
Err, IResult, Offset,
sequence::{preceded, separated_pair, terminated},
IResult,
};
use std::collections::HashMap;
use std::str;
use std::cell::Cell;
struct Cursor<'a> {
inner: &'a str,
offset: usize,
}
use std::str;
#[derive(Clone, Debug)]
pub struct JsonValue<'a, 'b> {
@ -49,7 +43,7 @@ impl<'a, 'b:'a> JsonValue<'a, 'b> {
self.offset(i);
println!("-> {}", s);
Some(s)
},
}
_ => None,
}
}
@ -61,7 +55,7 @@ impl<'a, 'b:'a> JsonValue<'a, 'b> {
self.offset(i);
println!("-> {}", o);
Some(o)
},
}
_ => None,
}
}
@ -73,7 +67,7 @@ impl<'a, 'b:'a> JsonValue<'a, 'b> {
self.offset(i);
println!("-> {}", o);
Some(o)
},
}
_ => None,
}
}
@ -103,19 +97,18 @@ impl<'a, 'b:'a> JsonValue<'a, 'b> {
match value(v.data()) {
Ok((i, _)) => {
v.offset(i);
},
Err(_) => {},
}
Err(_) => {}
}
}
match tag::<_, _, ()>("]")(v.data()) {
Ok((i, _)) => {
println!("]");
v.offset(i);
done = true;
return None;
},
}
Err(_) => {}
};
@ -126,7 +119,7 @@ impl<'a, 'b:'a> JsonValue<'a, 'b> {
Ok((i, _)) => {
println!(",");
v.offset(i);
},
}
Err(_) => {
done = true;
return None;
@ -137,7 +130,6 @@ impl<'a, 'b:'a> JsonValue<'a, 'b> {
println!("-> {}", v.data());
previous = v.offset.get();
Some(v.clone())
}))
}
}
@ -169,8 +161,8 @@ impl<'a, 'b:'a> JsonValue<'a, 'b> {
match value(v.data()) {
Ok((i, _)) => {
v.offset(i);
},
Err(_) => {},
}
Err(_) => {}
}
}
@ -180,7 +172,7 @@ impl<'a, 'b:'a> JsonValue<'a, 'b> {
v.offset(i);
done = true;
return None;
},
}
Err(_) => {}
};
@ -191,7 +183,7 @@ impl<'a, 'b:'a> JsonValue<'a, 'b> {
Ok((i, _)) => {
println!(",");
v.offset(i);
},
}
Err(_) => {
done = true;
return None;
@ -214,10 +206,9 @@ impl<'a, 'b:'a> JsonValue<'a, 'b> {
Some((key, v.clone()))
}
}
},
}
_ => None,
}
}))
}
}
@ -235,30 +226,27 @@ fn parse_str<'a, E: ParseError<&'a str>>(i: &'a str) -> IResult<&'a str, &'a str
}
fn string<'a>(i: &'a str) -> IResult<&'a str, &'a str> {
context("string",
preceded(
char('\"'),
cut(terminated(
parse_str,
char('\"')
))))(i)
context(
"string",
preceded(char('\"'), cut(terminated(parse_str, char('\"')))),
)(i)
}
fn boolean<'a>(input: &'a str) -> IResult<&'a str, bool> {
alt((
map(tag("false"), |_| false),
map(tag("true"), |_| true)
))(input)
alt((map(tag("false"), |_| false), map(tag("true"), |_| true)))(input)
}
fn array<'a>(i: &'a str) -> IResult<&'a str, ()> {
context(
"array",
preceded(char('['),
preceded(
char('['),
cut(terminated(
map(separated_list(preceded(sp, char(',')), value), |_| ()),
preceded(sp, char(']'))))
))(i)
map(separated_list0(preceded(sp, char(',')), value), |_| ()),
preceded(sp, char(']')),
)),
),
)(i)
}
fn key_value<'a>(i: &'a str) -> IResult<&'a str, (&'a str, ())> {
@ -268,14 +256,14 @@ separated_pair(preceded(sp, string), cut(preceded(sp, char(':'))), value)(i)
fn hash<'a>(i: &'a str) -> IResult<&'a str, ()> {
context(
"map",
preceded(char('{'),
preceded(
char('{'),
cut(terminated(
map(
separated_list(preceded(sp, char(',')), key_value),
|_| ()),
map(separated_list0(preceded(sp, char(',')), key_value), |_| ()),
preceded(sp, char('}')),
))
))(i)
)),
),
)(i)
}
fn value<'a>(i: &'a str) -> IResult<&'a str, ()> {
@ -314,13 +302,17 @@ fn main() {
let parser = JsonValue::new(data, &offset);
if let Some(o) = parser.object() {
let s: HashMap<&str, &str> = o.filter(|(k,_)| *k == "users" )
.filter_map(|(_, v)| v.object()).flatten()
let s: HashMap<&str, &str> = o
.filter(|(k, _)| *k == "users")
.filter_map(|(_, v)| v.object())
.flatten()
.filter_map(|(user, v)| v.object().map(|o| (user, o)))
.map(|(user, o)| {
o.filter(|(k, _)| *k == "city")
.filter_map(move |(_, v)| v.string().map(|s| (user, s)))
}).flatten().collect();
})
.flatten()
.collect();
println!("res = {:?}", s);
}

View File

@ -1,17 +0,0 @@
#[macro_use]
extern crate nom;
use nom::{character::complete::digit0, number::complete::be_u32};
named!(first<u32>, flat_map!(digit0, parse_to!(u32)));
named!(second<u32>, call!(be_u32));
named!(parser<u32>, alt!(first | second));
fn main() {
let data = b"1234;";
let res = parser(&data[..]);
println!("res: {:?}", res);
}