mirror of
https://github.com/cargo-bins/cargo-binstall.git
synced 2025-05-03 18:50:02 +00:00
Leon template library (#766)
* leon: first implementation * Update crates/leon/src/values.rs Co-authored-by: Jiahao XU <Jiahao_XU@outlook.com> * Workaround orphan rules to make API more intuitive * Fmt * Clippy * Use CoW * Use cow for items too * Test that const construction works * leon: Initial attempt at O(n) parser * leon: finish parser (except escapes) * leon: Improve ergonomics of compile-time templates * Document helpers * leon: Docs tweaks * leon: Use macro to minimise parser tests * leon: add escapes to parser * leon: test escapes preceding keys * leon: add multibyte tests * leon: test escapes following keys * Format * Debug * leon: Don't actually need to keep track of the key * leon: Parse to vec first * leon: there's actually no need for string cows * leon: reorganise and redo macro now that there's no coww * Well that was silly * leon: Adjust text end when pushing * leon: catch unbalanced keys * Add error tests * leon: Catch unfinished escape * Comment out debugging * leon: fuzz * Clippy * leon: Box parse error * leon: &dyn instead of impl * Can't impl FromStr, so rename to parse * Add Vec<> to values * leon: Add benches for ways to supply values * leon: Add bench comparing to std and tt * Fix fuzz * Fmt * Split ParseError and RenderError * Make miette optional * Remove RenderError lifetime * Simplify ParseError type schema * Write concrete Values types instead of generics * Add license files * Reduce criterion deps * Make default a cow * Add a CLI leon tool * Fix tests * Clippy * Disable cli by default * Avoid failing the build when cli is off * Add to ci * Update crates/leon/src/main.rs Co-authored-by: Jiahao XU <Jiahao_XU@outlook.com> * Update crates/leon/Cargo.toml Co-authored-by: Jiahao XU <Jiahao_XU@outlook.com> * Bump version * Error not transparent * Diagnostic can do forwarding * Simplify error type * Expand doc examples * Generic Values for Hash and BTree maps * One more borrowed * Forward implementations * More generics * Add has_keys * Lock stdout in leon tool * No more debug comments in parser * Even more generics * Macros to reduce bench duplication * Further simplify error * Fix leon main * Stable support * Clippy --------- Co-authored-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
parent
daf8cdd010
commit
2227d363f7
20 changed files with 2382 additions and 162 deletions
89
crates/leon/benches/others.rs
Normal file
89
crates/leon/benches/others.rs
Normal file
|
@ -0,0 +1,89 @@
|
|||
use std::borrow::Cow;
|
||||
|
||||
use criterion::{black_box, criterion_group, criterion_main, Criterion};
|
||||
use leon::{vals, Template};
|
||||
use serde::Serialize;
|
||||
use tinytemplate::TinyTemplate;
|
||||
|
||||
fn compare_impls(c: &mut Criterion) {
|
||||
const TEMPLATE: &str = "hello {name}! i am {age} years old. my goal is to {goal}. i like: {flower}, {music}, {animal}, {color}, {food}. i'm drinking {drink}";
|
||||
fn replace_fn<'s>(key: &'s str) -> Option<Cow<'s, str>> {
|
||||
Some(Cow::Borrowed(match key {
|
||||
"name" => "marcus",
|
||||
"age" => "42",
|
||||
"goal" => "primary",
|
||||
"flower" => "lotus",
|
||||
"music" => "jazz",
|
||||
"animal" => "cat",
|
||||
"color" => "blue",
|
||||
"food" => "pizza",
|
||||
"drink" => "coffee",
|
||||
_ => return None,
|
||||
}))
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct Context<'c> {
|
||||
name: &'c str,
|
||||
age: u8,
|
||||
goal: &'c str,
|
||||
flower: &'c str,
|
||||
music: &'c str,
|
||||
animal: &'c str,
|
||||
color: &'c str,
|
||||
food: &'c str,
|
||||
drink: &'c str,
|
||||
}
|
||||
|
||||
let tt_context = Context {
|
||||
name: "marcus",
|
||||
age: 42,
|
||||
goal: "primary",
|
||||
flower: "lotus",
|
||||
music: "jazz",
|
||||
animal: "cat",
|
||||
color: "blue",
|
||||
food: "pizza",
|
||||
drink: "coffee",
|
||||
};
|
||||
|
||||
c.bench_function("leon", |b| {
|
||||
b.iter(|| {
|
||||
let template = Template::parse(black_box(TEMPLATE)).unwrap();
|
||||
let output = template.render(&vals(replace_fn)).unwrap();
|
||||
black_box(output);
|
||||
})
|
||||
});
|
||||
|
||||
c.bench_function("std, string replaces", |b| {
|
||||
b.iter(|| {
|
||||
let mut output = black_box(TEMPLATE).to_string();
|
||||
for (key, value) in [
|
||||
("name", "marcus"),
|
||||
("age", "42"),
|
||||
("goal", "primary"),
|
||||
("flower", "lotus"),
|
||||
("music", "jazz"),
|
||||
("animal", "cat"),
|
||||
("color", "blue"),
|
||||
("food", "pizza"),
|
||||
("drink", "coffee"),
|
||||
] {
|
||||
output = output.replace(&format!("{{{}}}", key), value);
|
||||
}
|
||||
black_box(output);
|
||||
})
|
||||
});
|
||||
|
||||
c.bench_function("tiny template", |b| {
|
||||
b.iter(|| {
|
||||
let mut tt = TinyTemplate::new();
|
||||
tt.add_template("tmp", black_box(TEMPLATE)).unwrap();
|
||||
let output = tt.render("tmp", &tt_context).unwrap();
|
||||
black_box(output);
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
criterion_group!(compare, compare_impls);
|
||||
criterion_main!(compare);
|
Loading…
Add table
Add a link
Reference in a new issue