From f462d11025cf21eddd594771bf4b9f30f23a68df Mon Sep 17 00:00:00 2001 From: Avril Date: Fri, 22 Jul 2022 17:11:14 +0100 Subject: [PATCH] Made `lookup_plugin_name()` return an `IntoIterator` for if we allow dirs to be plugins in the future. (Currently, it will always be just `Option`) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Added `PluginTrust<"a>: Cow<"a, Path>` as return type from this iterator, so the caller can know the trust of the path the plugin was loaded from. Fortune for rng's current commit: Future blessing − 末吉 --- src/plugin/searcher.rs | 89 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 87 insertions(+), 2 deletions(-) diff --git a/src/plugin/searcher.rs b/src/plugin/searcher.rs index b8f1621..f18bb93 100644 --- a/src/plugin/searcher.rs +++ b/src/plugin/searcher.rs @@ -6,6 +6,9 @@ use std::path::{ }; use std::{ fs, io, + borrow::{ + Cow, Borrow, + }, //collections::HashMap, }; use regex::{ @@ -558,7 +561,89 @@ impl> LookupVisitor

} } -pub fn lookup_plugin_name(name: impl AsRef) -> Option +/// Denoted a plugin and if it was loaded from a trusted path or not +#[derive(Debug, Clone, Eq, Hash, PartialOrd, Ord)] +pub enum PluginTrust<'a> { - todo!("Create a LookupVisitor() for each path in `locations`, then call `start_lookup(, name)` on each *in order* specified in `locations`") + Trusted(Cow<'a, Path>), + Untrusted(Cow<'a, Path>) +} + +impl<'a> Borrow for PluginTrust<'a> +{ + #[inline(always)] + fn borrow(&self) -> &Path + { + match self { + Self::Trusted(a) | Self::Untrusted(a) => a.borrow() + } + } +} + +impl<'a> AsRef for PluginTrust<'a> +{ + #[inline] + fn as_ref(&self) -> &Path + { + self.borrow() + } +} + +impl<'a> From> for Cow<'a, Path> +{ + #[inline] + fn from(from: PluginTrust<'a>) -> Self + { + match from { + PluginTrust::Trusted(a) | PluginTrust::Untrusted(a) => a + } + } +} + +impl<'a, T: AsRef + ?Sized> PartialEq for PluginTrust<'a> +{ + #[inline] + fn eq(&self, other: &T) -> bool + { + other.as_ref() == self.as_ref() + } +} + +impl<'a> PluginTrust<'a> +{ + /// Was this plugin loaded from a trusted path? + #[inline(always)] + pub fn is_trusted(&self) -> bool + { + if let Self::Trusted(_) = self { + true + } else { + false + } + } + + /// Was this plugin loaded from an untrusted path? + #[inline] + pub fn is_untrusted(&self) -> bool + { + !self.is_trusted() + } +} + +impl From> for PathBuf +{ + #[inline] + fn from(from: PluginTrust<'static>) -> Self + { + match from { + PluginTrust::Trusted(a) | PluginTrust::Untrusted(a) => a.into_owned() + } + } +} + + +pub fn lookup_plugin_name(name: impl AsRef) -> impl IntoIterator> + Send + 'static +{ + todo!("Create a LookupVisitor() for each path in `locations`, then call `start_lookup(, name)` on each *in order* specified in `locations`"); + None:: //TODO: ^ see above }