diff options
Diffstat (limited to 'zellij-utils/src/input/plugins.rs')
-rw-r--r-- | zellij-utils/src/input/plugins.rs | 191 |
1 files changed, 17 insertions, 174 deletions
diff --git a/zellij-utils/src/input/plugins.rs b/zellij-utils/src/input/plugins.rs index 8dec828d5..55ea42838 100644 --- a/zellij-utils/src/input/plugins.rs +++ b/zellij-utils/src/input/plugins.rs @@ -1,45 +1,39 @@ //! Plugins configuration metadata use std::borrow::Borrow; use std::collections::HashMap; -use std::convert::TryFrom; use std::fs; use std::path::{Path, PathBuf}; use thiserror::Error; -use lazy_static::lazy_static; use serde::{Deserialize, Serialize}; use url::Url; -use super::config::ConfigFromYaml; use super::layout::{RunPlugin, RunPluginLocation}; pub use crate::data::PluginTag; -use crate::setup; -lazy_static! { - static ref DEFAULT_CONFIG_PLUGINS: PluginsConfig = { - let cfg = String::from_utf8(setup::DEFAULT_CONFIG.to_vec()).unwrap(); - let cfg_yaml: ConfigFromYaml = serde_yaml::from_str(&cfg).unwrap(); - PluginsConfig::try_from(cfg_yaml.plugins).unwrap() - }; -} - -#[derive(Clone, Debug, Default, PartialEq, Deserialize, Serialize)] -pub struct PluginsConfigFromYaml(Vec<PluginConfigFromYaml>); +use std::collections::BTreeMap; +use std::fmt; /// Used in the config struct for plugin metadata -#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)] -pub struct PluginsConfig(HashMap<PluginTag, PluginConfig>); +#[derive(Clone, PartialEq, Deserialize, Serialize)] +pub struct PluginsConfig(pub HashMap<PluginTag, PluginConfig>); + +impl fmt::Debug for PluginsConfig { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + let mut stable_sorted = BTreeMap::new(); + for (plugin_tag, plugin_config) in self.0.iter() { + stable_sorted.insert(plugin_tag, plugin_config); + } + write!(f, "{:#?}", stable_sorted) + } +} impl PluginsConfig { pub fn new() -> Self { Self(HashMap::new()) } - - /// Entrypoint from the config module - pub fn get_plugins_with_default(user_plugins: Self) -> Self { - let mut base_plugins = DEFAULT_CONFIG_PLUGINS.clone(); - base_plugins.0.extend(user_plugins.0); - base_plugins + pub fn from_data(data: HashMap<PluginTag, PluginConfig>) -> Self { + PluginsConfig(data) } /// Get plugin config from run configuration specified in layout files. @@ -74,37 +68,7 @@ impl PluginsConfig { impl Default for PluginsConfig { fn default() -> Self { - Self::get_plugins_with_default(PluginsConfig::new()) - } -} - -impl TryFrom<PluginsConfigFromYaml> for PluginsConfig { - type Error = PluginsConfigError; - - fn try_from(yaml: PluginsConfigFromYaml) -> Result<Self, PluginsConfigError> { - let mut plugins = HashMap::new(); - for plugin in yaml.0 { - if plugins.contains_key(&plugin.tag) { - return Err(PluginsConfigError::DuplicatePlugins(plugin.tag)); - } - plugins.insert(plugin.tag.clone(), plugin.into()); - } - - Ok(PluginsConfig(plugins)) - } -} - -impl From<PluginConfigFromYaml> for PluginConfig { - fn from(plugin: PluginConfigFromYaml) -> Self { - PluginConfig { - path: plugin.path, - run: match plugin.run { - PluginTypeFromYaml::Pane => PluginType::Pane(None), - PluginTypeFromYaml::Headless => PluginType::Headless, - }, - _allow_exec_host_cmd: plugin._allow_exec_host_cmd, - location: RunPluginLocation::Zellij(plugin.tag), - } + PluginsConfig(HashMap::new()) } } @@ -172,31 +136,6 @@ impl Default for PluginType { } } -#[derive(Clone, Debug, Default, PartialEq, Deserialize, Serialize)] -pub struct PluginConfigFromYaml { - pub path: PathBuf, - pub tag: PluginTag, - #[serde(default)] - pub run: PluginTypeFromYaml, - #[serde(default)] - pub config: serde_yaml::Value, - #[serde(default)] - pub _allow_exec_host_cmd: bool, -} - -#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)] -#[serde(rename_all = "kebab-case")] -pub enum PluginTypeFromYaml { - Headless, - Pane, -} - -impl Default for PluginTypeFromYaml { - fn default() -> Self { - Self::Pane - } -} - #[derive(Error, Debug, PartialEq)] pub enum PluginsConfigError { #[error("Duplication in plugin tag names is not allowed: '{}'", String::from(.0.clone()))] @@ -206,99 +145,3 @@ pub enum PluginsConfigError { #[error("Could not find plugin at the path: '{0:?}'")] InvalidPluginLocation(PathBuf), } - -#[cfg(test)] -mod tests { - use super::*; - use crate::input::config::ConfigError; - use std::convert::TryInto; - - #[test] - fn run_plugin_permissions_are_inherited() -> Result<(), ConfigError> { - let yaml_plugins: PluginsConfigFromYaml = serde_yaml::from_str( - " - - path: boo.wasm - tag: boo - _allow_exec_host_cmd: false - ", - )?; - let plugins = PluginsConfig::try_from(yaml_plugins)?; - - assert_eq!( - plugins.get(RunPlugin { - _allow_exec_host_cmd: true, - location: RunPluginLocation::Zellij(PluginTag::new("boo")) - }), - Some(PluginConfig { - _allow_exec_host_cmd: true, - path: PathBuf::from("boo.wasm"), - location: RunPluginLocation::Zellij(PluginTag::new("boo")), - run: PluginType::Pane(None), - }) - ); - - Ok(()) - } - - #[test] - fn try_from_yaml_fails_when_duplicate_tag_names_are_present() -> Result<(), ConfigError> { - let ConfigFromYaml { plugins, .. } = serde_yaml::from_str( - " - plugins: - - path: /foo/bar/baz.wasm - tag: boo - - path: /foo/bar/boo.wasm - tag: boo - ", - )?; - - assert_eq!( - PluginsConfig::try_from(plugins), - Err(PluginsConfigError::DuplicatePlugins(PluginTag::new("boo"))) - ); - - Ok(()) - } - - #[test] - fn default_plugins() -> Result<(), ConfigError> { - let ConfigFromYaml { plugins, .. } = serde_yaml::from_str( - " - plugins: - - path: boo.wasm - tag: boo - ", - )?; - let plugins = PluginsConfig::get_plugins_with_default(plugins.try_into()?); - - assert_eq!(plugins.iter().count(), 5); - Ok(()) - } - - #[test] - fn default_plugins_allow_overriding() -> Result<(), ConfigError> { - let ConfigFromYaml { plugins, .. } = serde_yaml::from_str( - " - plugins: - - path: boo.wasm - tag: tab-bar - ", - )?; - let plugins = PluginsConfig::get_plugins_with_default(plugins.try_into()?); - - assert_eq!( - plugins.get(RunPlugin { - _allow_exec_host_cmd: false, - location: RunPluginLocation::Zellij(PluginTag::new("tab-bar")) - }), - Some(PluginConfig { - _allow_exec_host_cmd: false, - path: PathBuf::from("boo.wasm"), - location: RunPluginLocation::Zellij(PluginTag::new("tab-bar")), - run: PluginType::Pane(None), - }) - ); - - Ok(()) - } -} |