Skip to content

Commit

Permalink
Ensure all examples compile (#1604)
Browse files Browse the repository at this point in the history
* docs: Remove unused example

* docs: Ensure all examples compile

Co-authored-by: Ed Page <eopage@gmail.com>
  • Loading branch information
Geal and epage committed Dec 30, 2022
1 parent b66ff43 commit 9cff115
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 91 deletions.
10 changes: 10 additions & 0 deletions Cargo.toml
Expand Up @@ -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"
Expand Down
4 changes: 3 additions & 1 deletion examples/custom_error.rs
Expand Up @@ -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;
Expand Down
138 changes: 65 additions & 73 deletions examples/json_iterator.rs
Expand Up @@ -4,31 +4,25 @@ 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> {
input: &'a str,
pub offset: &'b Cell<usize>,
}

impl<'a, 'b:'a> JsonValue<'a, 'b> {
impl<'a, 'b: 'a> JsonValue<'a, 'b> {
pub fn new(input: &'a str, offset: &'b Cell<usize>) -> JsonValue<'a, 'b> {
JsonValue { input, offset }
}
Expand All @@ -49,7 +43,7 @@ impl<'a, 'b:'a> JsonValue<'a, 'b> {
self.offset(i);
println!("-> {}", s);
Some(s)
},
}
_ => None,
}
}
Expand All @@ -61,27 +55,27 @@ impl<'a, 'b:'a> JsonValue<'a, 'b> {
self.offset(i);
println!("-> {}", o);
Some(o)
},
}
_ => None,
}
}

pub fn number(&self) -> Option<f64> {
println!("number()");
match double::<_,()>(self.data()) {
match double::<_, ()>(self.data()) {
Ok((i, o)) => {
self.offset(i);
println!("-> {}", o);
Some(o)
},
}
_ => None,
}
}

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

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

let v = self.clone();

Some(std::iter::from_fn(move|| {
Some(std::iter::from_fn(move || {
if done {
return None;
}
Expand All @@ -103,30 +97,29 @@ impl<'a, 'b:'a> JsonValue<'a, 'b> {
match value(v.data()) {
Ok((i, _)) => {
v.offset(i);
},
Err(_) => {},
}
Err(_) => {}
}
}


match tag::<_,_,()>("]")(v.data()) {
match tag::<_, _, ()>("]")(v.data()) {
Ok((i, _)) => {
println!("]");
v.offset(i);
done = true;
return None;
},
}
Err(_) => {}
};

if first {
first = false;
} else {
match tag::<_,_,()>(",")(v.data()) {
match tag::<_, _, ()>(",")(v.data()) {
Ok((i, _)) => {
println!(",");
println!(",");
v.offset(i);
},
}
Err(_) => {
done = true;
return None;
Expand All @@ -137,15 +130,14 @@ impl<'a, 'b:'a> JsonValue<'a, 'b> {
println!("-> {}", v.data());
previous = v.offset.get();
Some(v.clone())

}))
}
}
}

pub fn object(&self) -> Option<impl Iterator<Item=(&'a str, JsonValue<'a, 'b>)>> {
pub fn object(&self) -> Option<impl Iterator<Item = (&'a str, JsonValue<'a, 'b>)>> {
println!("object()");
match tag::<_,_,()>("{")(self.data()) {
match tag::<_, _, ()>("{")(self.data()) {
Err(_) => None,
Ok((i, _)) => {
self.offset(i);
Expand All @@ -158,7 +150,7 @@ impl<'a, 'b:'a> JsonValue<'a, 'b> {

let v = self.clone();

Some(std::iter::from_fn(move|| {
Some(std::iter::from_fn(move || {
if done {
return None;
}
Expand All @@ -169,29 +161,29 @@ impl<'a, 'b:'a> JsonValue<'a, 'b> {
match value(v.data()) {
Ok((i, _)) => {
v.offset(i);
},
Err(_) => {},
}
Err(_) => {}
}
}

match tag::<_,_,()>("}")(v.data()) {
match tag::<_, _, ()>("}")(v.data()) {
Ok((i, _)) => {
println!("}}");
v.offset(i);
done = true;
return None;
},
}
Err(_) => {}
};

if first {
first = false;
} else {
match tag::<_,_,()>(",")(v.data()) {
match tag::<_, _, ()>(",")(v.data()) {
Ok((i, _)) => {
println!(",");
v.offset(i);
},
}
Err(_) => {
done = true;
return None;
Expand All @@ -203,7 +195,7 @@ impl<'a, 'b:'a> JsonValue<'a, 'b> {
Ok((i, key)) => {
v.offset(i);

match tag::<_,_,()>(":")(v.data()) {
match tag::<_, _, ()>(":")(v.data()) {
Err(_) => None,
Ok((i, _)) => {
v.offset(i);
Expand All @@ -214,10 +206,9 @@ impl<'a, 'b:'a> JsonValue<'a, 'b> {
Some((key, v.clone()))
}
}
},
}
_ => None,
}

}))
}
}
Expand All @@ -235,47 +226,44 @@ 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('['),
cut(terminated(
map(separated_list(preceded(sp, char(',')), value), |_| ()),
preceded(sp, char(']'))))
))(i)
preceded(
char('['),
cut(terminated(
map(separated_list0(preceded(sp, char(',')), value), |_| ()),
preceded(sp, char(']')),
)),
),
)(i)
}

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

fn hash<'a>(i: &'a str) -> IResult<&'a str, ()> {
context(
"map",
preceded(char('{'),
cut(terminated(
map(
separated_list(preceded(sp, char(',')), key_value),
|_| ()),
preceded(sp, char('}')),
))
))(i)
preceded(
char('{'),
cut(terminated(
map(separated_list0(preceded(sp, char(',')), key_value), |_| ()),
preceded(sp, char('}')),
)),
),
)(i)
}

fn value<'a>(i: &'a str) -> IResult<&'a str, ()> {
Expand Down Expand Up @@ -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()
.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();
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();

println!("res = {:?}", s);
}
Expand Down
17 changes: 0 additions & 17 deletions examples/macro.rs

This file was deleted.

0 comments on commit 9cff115

Please sign in to comment.