mirror of
https://github.com/cargo-bins/cargo-binstall.git
synced 2025-05-06 12:10:02 +00:00
Use leon for template in binstalk & detect malformed pkg-url/pkg-fmt early (#933)
Fixed #851 * Add new dep leon to crate binstalk * Add new variant `BinstallError::Template{Parse, Render}Error` * Use `leon::Template` in mod `bins` * Use `leon::Template` in mod `fetchers::gh_crate_meta` * Refactor mod `bins`: Rm unused associated fn & fields from `Context` * Simplify unit testing in mod `fetchers::gh_crate_meta` * Rm soft-deprecated field `fetchers::gh_crate_meta::Context::format` and change the `match` to resolve `archive` => `self.archive_format`. * Make macro_rules `leon::template!` far easier to use * Construct `leon::Template<'_>` as constant in `gh_crate_meta::hosting` * Simplify `leon::Values` trait Change its method `get_value` signature to ```rust fn get_value(&self, key: &str) -> Option<Cow<'_, str>>; ``` Now, `ValuesFn` also accepts non-`'static` function, but now `leon::Values` is only implemented for `&ValuesFn<F>` now. This makes it a little bit more cumbersome to use but I think it's a reasonable drawback. * Rm `Send` bound req from `ValuesFn` * Impl new fn `leon::Template::cast` for casting `Template<'s>` to `Template<'t>` where `'s: 't` * Rename `leon::Template::has_keys` => `has_any_of_keys` * Make checking whether format related keys are present more robust * Optimize `GhCrateMeta::launch_baseline_find_tasks`: Skip checking all fmt ext if none of the format related keys ("format", "archive-format", "archive-suffix") are present. * Only ret `.exe` in `PkgFmt::extensions` on windows by adding a new param `is_windows: bool` * Improve debug msg in `GhCrateMeta::fetch_and_extract` * Add warnings to `GhCrateMeta::find` * Rm dep tinytemplate * `impl<'s, 'rhs: 's> ops::AddAssign<&Template<'rhs>> for Template<'s>` * `impl<'s, 'rhs: 's> ops::AddAssign<Template<'rhs>> for Template<'s>` * `impl<'s, 'item: 's> ops::AddAssign<Item<'item>> for Template<'s>` * `impl<'s, 'item: 's> ops::AddAssign<&Item<'item>> for Template<'s>` * `impl<'s, 'rhs: 's> ops::Add<Template<'rhs>> for Template<'s>` (improved existing `Add` impl) * `impl<'s, 'rhs: 's> ops::Add<&Template<'rhs>> for Template<'s>` * `impl<'s, 'item: 's> ops::Add<Item<'item>> for Template<'s>` * `impl<'s, 'item: 's> ops::Add<&Item<'item>> for Template<'s>` Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com> Co-authored-by: Félix Saparelli <felix@passcod.name>
This commit is contained in:
parent
47d4aeaa96
commit
a27d5aebf6
13 changed files with 512 additions and 312 deletions
|
@ -5,14 +5,14 @@ use std::{
|
|||
};
|
||||
|
||||
pub trait Values {
|
||||
fn get_value<'s, 'k: 's>(&'s self, key: &'k str) -> Option<Cow<'s, str>>;
|
||||
fn get_value(&self, key: &str) -> Option<Cow<'_, str>>;
|
||||
}
|
||||
|
||||
impl<T> Values for &T
|
||||
where
|
||||
T: Values,
|
||||
{
|
||||
fn get_value<'s, 'k: 's>(&'s self, key: &'k str) -> Option<Cow<'s, str>> {
|
||||
fn get_value(&self, key: &str) -> Option<Cow<'_, str>> {
|
||||
T::get_value(self, key)
|
||||
}
|
||||
}
|
||||
|
@ -22,7 +22,7 @@ where
|
|||
K: AsRef<str>,
|
||||
V: AsRef<str>,
|
||||
{
|
||||
fn get_value<'s, 'k: 's>(&'s self, key: &'k str) -> Option<Cow<'s, str>> {
|
||||
fn get_value(&self, key: &str) -> Option<Cow<'_, str>> {
|
||||
self.iter().find_map(|(k, v)| {
|
||||
if k.as_ref() == key {
|
||||
Some(Cow::Borrowed(v.as_ref()))
|
||||
|
@ -38,7 +38,7 @@ where
|
|||
K: AsRef<str>,
|
||||
V: AsRef<str>,
|
||||
{
|
||||
fn get_value<'s, 'k: 's>(&'s self, key: &'k str) -> Option<Cow<'s, str>> {
|
||||
fn get_value(&self, key: &str) -> Option<Cow<'_, str>> {
|
||||
(*self).get_value(key)
|
||||
}
|
||||
}
|
||||
|
@ -48,7 +48,7 @@ where
|
|||
K: AsRef<str>,
|
||||
V: AsRef<str>,
|
||||
{
|
||||
fn get_value<'s, 'k: 's>(&'s self, key: &'k str) -> Option<Cow<'s, str>> {
|
||||
fn get_value(&self, key: &str) -> Option<Cow<'_, str>> {
|
||||
self.as_slice().get_value(key)
|
||||
}
|
||||
}
|
||||
|
@ -58,7 +58,7 @@ where
|
|||
K: AsRef<str>,
|
||||
V: AsRef<str>,
|
||||
{
|
||||
fn get_value<'s, 'k: 's>(&'s self, key: &'k str) -> Option<Cow<'s, str>> {
|
||||
fn get_value(&self, key: &str) -> Option<Cow<'_, str>> {
|
||||
self.as_slice().get_value(key)
|
||||
}
|
||||
}
|
||||
|
@ -69,7 +69,7 @@ where
|
|||
V: AsRef<str>,
|
||||
S: BuildHasher,
|
||||
{
|
||||
fn get_value<'s, 'k: 's>(&'s self, key: &'k str) -> Option<Cow<'s, str>> {
|
||||
fn get_value(&self, key: &str) -> Option<Cow<'_, str>> {
|
||||
self.get(key).map(|v| Cow::Borrowed(v.as_ref()))
|
||||
}
|
||||
}
|
||||
|
@ -79,7 +79,7 @@ where
|
|||
K: Borrow<str> + Ord,
|
||||
V: AsRef<str>,
|
||||
{
|
||||
fn get_value<'s, 'k: 's>(&'s self, key: &'k str) -> Option<Cow<'s, str>> {
|
||||
fn get_value(&self, key: &str) -> Option<Cow<'_, str>> {
|
||||
self.get(key).map(|v| Cow::Borrowed(v.as_ref()))
|
||||
}
|
||||
}
|
||||
|
@ -87,25 +87,22 @@ where
|
|||
/// 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,
|
||||
{
|
||||
pub struct ValuesFn<F> {
|
||||
inner: F,
|
||||
}
|
||||
|
||||
impl<F> Values for ValuesFn<F>
|
||||
impl<'s, F> Values for &'s ValuesFn<F>
|
||||
where
|
||||
F: for<'s> Fn(&'s str) -> Option<Cow<'s, str>> + Send + 'static,
|
||||
F: Fn(&str) -> Option<Cow<'s, str>> + 's,
|
||||
{
|
||||
fn get_value<'s, 'k: 's>(&'s self, key: &'k str) -> Option<Cow<'s, str>> {
|
||||
fn get_value(&self, key: &str) -> Option<Cow<'_, str>> {
|
||||
(self.inner)(key)
|
||||
}
|
||||
}
|
||||
|
||||
impl<F> From<F> for ValuesFn<F>
|
||||
impl<'f, F> From<F> for ValuesFn<F>
|
||||
where
|
||||
F: for<'s> Fn(&'s str) -> Option<Cow<'s, str>> + Send + 'static,
|
||||
F: Fn(&str) -> Option<Cow<'f, str>> + 'f,
|
||||
{
|
||||
fn from(inner: F) -> Self {
|
||||
Self { inner }
|
||||
|
@ -123,11 +120,11 @@ where
|
|||
///
|
||||
/// fn use_values(_values: impl Values) {}
|
||||
///
|
||||
/// use_values(vals(|_| Some("hello".into())));
|
||||
/// use_values(&vals(|_| Some("hello".into())));
|
||||
/// ```
|
||||
pub const fn vals<F>(func: F) -> ValuesFn<F>
|
||||
pub const fn vals<'f, F>(func: F) -> ValuesFn<F>
|
||||
where
|
||||
F: for<'s> Fn(&'s str) -> Option<Cow<'s, str>> + Send + 'static,
|
||||
F: Fn(&str) -> Option<Cow<'f, str>> + 'f,
|
||||
{
|
||||
ValuesFn { inner: func }
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue