mirror of
https://github.com/cargo-bins/cargo-binstall.git
synced 2025-05-06 12:10: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
133
crates/leon/src/values.rs
Normal file
133
crates/leon/src/values.rs
Normal file
|
@ -0,0 +1,133 @@
|
|||
use std::{
|
||||
borrow::{Borrow, Cow},
|
||||
collections::{BTreeMap, HashMap},
|
||||
hash::{BuildHasher, Hash},
|
||||
};
|
||||
|
||||
pub trait Values {
|
||||
fn get_value<'s, 'k: 's>(&'s self, key: &'k str) -> Option<Cow<'s, str>>;
|
||||
}
|
||||
|
||||
impl<T> Values for &T
|
||||
where
|
||||
T: Values,
|
||||
{
|
||||
fn get_value<'s, 'k: 's>(&'s self, key: &'k str) -> Option<Cow<'s, str>> {
|
||||
T::get_value(self, key)
|
||||
}
|
||||
}
|
||||
|
||||
impl<K, V> Values for [(K, V)]
|
||||
where
|
||||
K: AsRef<str>,
|
||||
V: AsRef<str>,
|
||||
{
|
||||
fn get_value<'s, 'k: 's>(&'s self, key: &'k str) -> Option<Cow<'s, str>> {
|
||||
self.iter().find_map(|(k, v)| {
|
||||
if k.as_ref() == key {
|
||||
Some(Cow::Borrowed(v.as_ref()))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<K, V> Values for &[(K, V)]
|
||||
where
|
||||
K: AsRef<str>,
|
||||
V: AsRef<str>,
|
||||
{
|
||||
fn get_value<'s, 'k: 's>(&'s self, key: &'k str) -> Option<Cow<'s, str>> {
|
||||
(*self).get_value(key)
|
||||
}
|
||||
}
|
||||
|
||||
impl<K, V, const N: usize> Values for [(K, V); N]
|
||||
where
|
||||
K: AsRef<str>,
|
||||
V: AsRef<str>,
|
||||
{
|
||||
fn get_value<'s, 'k: 's>(&'s self, key: &'k str) -> Option<Cow<'s, str>> {
|
||||
self.as_slice().get_value(key)
|
||||
}
|
||||
}
|
||||
|
||||
impl<K, V> Values for Vec<(K, V)>
|
||||
where
|
||||
K: AsRef<str>,
|
||||
V: AsRef<str>,
|
||||
{
|
||||
fn get_value<'s, 'k: 's>(&'s self, key: &'k str) -> Option<Cow<'s, str>> {
|
||||
self.as_slice().get_value(key)
|
||||
}
|
||||
}
|
||||
|
||||
impl<K, V, S> Values for HashMap<K, V, S>
|
||||
where
|
||||
K: Borrow<str> + Eq + Hash,
|
||||
V: AsRef<str>,
|
||||
S: BuildHasher,
|
||||
{
|
||||
fn get_value<'s, 'k: 's>(&'s self, key: &'k str) -> Option<Cow<'s, str>> {
|
||||
self.get(key).map(|v| Cow::Borrowed(v.as_ref()))
|
||||
}
|
||||
}
|
||||
|
||||
impl<K, V> Values for BTreeMap<K, V>
|
||||
where
|
||||
K: Borrow<str> + Ord,
|
||||
V: AsRef<str>,
|
||||
{
|
||||
fn get_value<'s, 'k: 's>(&'s self, key: &'k str) -> Option<Cow<'s, str>> {
|
||||
self.get(key).map(|v| Cow::Borrowed(v.as_ref()))
|
||||
}
|
||||
}
|
||||
|
||||
/// Workaround to allow using functions as [`Values`].
|
||||
///
|
||||
/// As this isn't constructible you'll want to use [`vals()`] instead.
|
||||
pub struct ValuesFn<F>
|
||||
where
|
||||
F: for<'s> Fn(&'s str) -> Option<Cow<'s, str>> + Send + 'static,
|
||||
{
|
||||
inner: F,
|
||||
}
|
||||
|
||||
impl<F> Values for ValuesFn<F>
|
||||
where
|
||||
F: for<'s> Fn(&'s str) -> Option<Cow<'s, str>> + Send + 'static,
|
||||
{
|
||||
fn get_value<'s, 'k: 's>(&'s self, key: &'k str) -> Option<Cow<'s, str>> {
|
||||
(self.inner)(key)
|
||||
}
|
||||
}
|
||||
|
||||
impl<F> From<F> for ValuesFn<F>
|
||||
where
|
||||
F: for<'s> Fn(&'s str) -> Option<Cow<'s, str>> + Send + 'static,
|
||||
{
|
||||
fn from(inner: F) -> Self {
|
||||
Self { inner }
|
||||
}
|
||||
}
|
||||
|
||||
/// Workaround to allow using functions as [`Values`].
|
||||
///
|
||||
/// Wraps your function so it implements [`Values`].
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use leon::{Values, vals};
|
||||
///
|
||||
/// fn use_values(_values: impl Values) {}
|
||||
///
|
||||
/// use_values(vals(|_| Some("hello".into())));
|
||||
/// ```
|
||||
pub const fn vals<F>(func: F) -> ValuesFn<F>
|
||||
where
|
||||
F: for<'s> Fn(&'s str) -> Option<Cow<'s, str>> + Send + 'static,
|
||||
{
|
||||
ValuesFn { inner: func }
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue