cargo-binstall/crates/leon/src/main.rs
Félix Saparelli 2227d363f7
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>
2023-03-21 01:36:02 +00:00

61 lines
1.7 KiB
Rust

#[cfg(feature = "cli")]
fn main() -> miette::Result<()> {
use std::{collections::HashMap, error::Error, io::stdout};
use clap::Parser;
use leon::Template;
/// Render a Leon template with the given values.
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
struct Args {
/// Leon template
template: String,
/// Default to use for missing keys
#[arg(long)]
default: Option<String>,
/// Use values from the environment
#[arg(long)]
env: bool,
/// Key-value pairs to use
#[arg(short, long, value_parser = parse_key_val::<String, String>)]
values: Vec<(String, String)>,
}
/// Parse a single key-value pair
fn parse_key_val<T, U>(s: &str) -> Result<(T, U), Box<dyn Error + Send + Sync + 'static>>
where
T: std::str::FromStr,
T::Err: Error + Send + Sync + 'static,
U: std::str::FromStr,
U::Err: Error + Send + Sync + 'static,
{
let (k, v) = s
.split_once('=')
.ok_or_else(|| format!("invalid KEY=value: no `=` found in `{s}`"))?;
Ok((k.parse()?, v.parse()?))
}
let args = Args::parse();
let mut values: HashMap<String, String> = HashMap::from_iter(args.values);
if args.env {
for (key, value) in std::env::vars() {
values.entry(key).or_insert(value);
}
}
let template = args.template;
let mut template = Template::parse(&template)?;
if let Some(default) = &args.default {
template.set_default(default);
}
template.render_into(&mut stdout().lock(), &values)?;
Ok(())
}
#[cfg(not(feature = "cli"))]
fn main() {}