-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(plugins): Abstract plugin registry
This will allow changing the implementation without impacting users. BREAKING CHANGE: Changed from `HashMap` to `PluginRegistry` for filters, tags, and blocks.
- Loading branch information
Showing
19 changed files
with
153 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
use std::collections::hash_map; | ||
|
||
type MapImpl<K, V> = hash_map::HashMap<K, V>; | ||
type KeysImpl<'a, K, V> = hash_map::Keys<'a, K, V>; | ||
|
||
/// Liquid language plugin registry. | ||
pub struct PluginRegistry<P> { | ||
plugins: MapImpl<&'static str, P> | ||
} | ||
|
||
impl<P> PluginRegistry<P> { | ||
/// Create a new registry. | ||
pub fn new() -> Self { | ||
Self { | ||
plugins: Default::default(), | ||
} | ||
} | ||
|
||
/// Register a plugin | ||
/// | ||
/// Generally this is used when setting up the program. | ||
/// | ||
/// Returns whether this overrode an existing plugin. | ||
pub fn register(&mut self, name: &'static str, plugin: P) -> bool { | ||
let old = self.plugins.insert(name, plugin); | ||
old.is_some() | ||
} | ||
|
||
/// Look up an existing plugin. | ||
/// | ||
/// Generally this is used for running plugins. | ||
pub fn get(&self, name: &str) -> Option<&P> { | ||
self.plugins.get(name) | ||
} | ||
|
||
/// All available plugins | ||
pub fn plugin_names(&self) -> PluginNames<P> { | ||
PluginNames { | ||
iter: self.plugins.keys(), | ||
} | ||
} | ||
} | ||
|
||
impl<P> Default for PluginRegistry<P> { | ||
#[inline] | ||
fn default() -> Self { | ||
Self::new() | ||
} | ||
} | ||
|
||
impl<P> Clone for PluginRegistry<P> | ||
where | ||
P: Clone | ||
{ | ||
#[inline] | ||
fn clone(&self) -> Self { | ||
Self { | ||
plugins: self.plugins.clone(), | ||
} | ||
} | ||
} | ||
|
||
////////////////////////////////////////////////////////////////////////////// | ||
|
||
macro_rules! delegate_iterator { | ||
(($name:ident $($generics:tt)*) => $item:ty) => { | ||
impl $($generics)* Iterator for $name $($generics)* { | ||
type Item = $item; | ||
#[inline] | ||
fn next(&mut self) -> Option<Self::Item> { | ||
self.iter.next().map(|s| *s) | ||
} | ||
#[inline] | ||
fn size_hint(&self) -> (usize, Option<usize>) { | ||
self.iter.size_hint() | ||
} | ||
} | ||
|
||
impl $($generics)* ExactSizeIterator for $name $($generics)* { | ||
#[inline] | ||
fn len(&self) -> usize { | ||
self.iter.len() | ||
} | ||
} | ||
} | ||
} | ||
|
||
////////////////////////////////////////////////////////////////////////////// | ||
|
||
/// Available plugins. | ||
#[derive(Debug)] | ||
pub struct PluginNames<'a, P> | ||
where | ||
P: 'a | ||
{ | ||
iter: KeysImpl<'a, &'static str, P>, | ||
} | ||
|
||
delegate_iterator!((PluginNames<'a, P>) => &'static str); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.