.cargo/
ombre/
ombre-derive/
reve/
syntaxe/
benches/
json.rs
2.4 KiB
twitter.json
73.7 KiB
src/
.gitignore
19 B
Cargo.toml
351 B
README.md
60 B
.gitignore
34 B
Cargo.lock
8.5 KiB
Cargo.toml
523 B
README
1.0 KiB
rust-toolchain.toml
77 B
rustfmt.toml
55 B
syntaxe/benches/json.rs
raw
| 1 | #![allow(clippy::unwrap_used)] |
| 2 | use std::collections::HashMap; |
| 3 | |
| 4 | use syntaxe::*; |
| 5 | |
| 6 | #[derive(Debug, PartialEq)] |
| 7 | enum Json { |
| 8 | Null, |
| 9 | Bool(bool), |
| 10 | Number(f64), |
| 11 | Str(String), |
| 12 | Array(Vec<Json>), |
| 13 | Object(HashMap<String, Json>), |
| 14 | } |
| 15 | |
| 16 | fn json_parser<'a>() -> impl Parser<'a, Output = Json> { |
| 17 | |input| json_value().parse_at(input) |
| 18 | } |
| 19 | |
| 20 | fn json_value<'a>() -> impl Parser<'a, Output = Json> { |
| 21 | string("null") |
| 22 | .map(|_| Json::Null) |
| 23 | .or(string("true").map(|_| Json::Bool(true))) |
| 24 | .or(string("false").map(|_| Json::Bool(false))) |
| 25 | .or(json_number()) |
| 26 | .or(json_string().map(Json::Str)) |
| 27 | .or(json_array()) |
| 28 | .or(json_object()) |
| 29 | } |
| 30 | |
| 31 | fn json_number<'a>() -> impl Parser<'a, Output = Json> { |
| 32 | signed(float::<f64>()) |
| 33 | .map(Json::Number) |
| 34 | .or(signed(natural::<i64>()).map(|i| Json::Number(i as f64))) |
| 35 | } |
| 36 | |
| 37 | fn json_string<'a>() -> impl Parser<'a, Output = String> { |
| 38 | let escaped_char = symbol('\\') |
| 39 | .then(choice([ |
| 40 | symbol('"').value('"'), |
| 41 | symbol('\\').value('\\'), |
| 42 | symbol('/').value('/'), |
| 43 | symbol('n').value('\n'), |
| 44 | symbol('t').value('\t'), |
| 45 | ])) |
| 46 | .map(|(_, c)| c); |
| 47 | |
| 48 | let string_char = escaped_char.or(satisfy(|c| *c != '"' && *c != '\\')); |
| 49 | |
| 50 | between(symbol('"'), symbol('"'), many::<_, String>(string_char)) |
| 51 | } |
| 52 | |
| 53 | fn json_array<'a>() -> impl Parser<'a, Output = Json> { |
| 54 | between( |
| 55 | symbol('[').skip(whitespace()), |
| 56 | whitespace().then(symbol(']')), |
| 57 | (|input| json_value().skip(whitespace()).parse_at(input)) |
| 58 | .delimited_by(symbol(',').skip(whitespace())), |
| 59 | ) |
| 60 | .map(Json::Array) |
| 61 | } |
| 62 | |
| 63 | fn json_object<'a>() -> impl Parser<'a, Output = Json> { |
| 64 | let key_value = json_string() |
| 65 | .skip(symbol(':')) |
| 66 | .skip(whitespace()) |
| 67 | .then(|input| json_value().parse_at(input)) |
| 68 | .skip(whitespace()); |
| 69 | |
| 70 | between( |
| 71 | symbol('{').then(whitespace()), |
| 72 | whitespace().then(symbol('}')), |
| 73 | key_value.delimited_by(symbol(',').skip(whitespace())), |
| 74 | ) |
| 75 | .map(|pairs| Json::Object(pairs.into_iter().collect())) |
| 76 | } |
| 77 | |
| 78 | fn main() { |
| 79 | let input = include_str!("twitter.json"); |
| 80 | let parser = json_parser(); |
| 81 | let size = input.bytes().len(); |
| 82 | let options = ombre::bench::Options::default(); |
| 83 | let result = ombre::bench::run(&options, || { |
| 84 | parser.parse(input).unwrap(); |
| 85 | }); |
| 86 | let t = result.throughput_bytes(size as u128); |
| 87 | |
| 88 | println!("{t}"); |
| 89 | } |