summaryrefslogtreecommitdiffstats
path: root/zellij-utils/src/input/plugins.rs
diff options
context:
space:
mode:
authorAram Drevekenin <aram@poor.dev>2022-10-05 07:44:00 +0200
committerGitHub <noreply@github.com>2022-10-05 07:44:00 +0200
commit79bf6ab868cbdab1f9a3827c9b70198f54548b44 (patch)
tree2d6fc4c1d8a79ebd727a1a5f8b6406617dd0de55 /zellij-utils/src/input/plugins.rs
parent917e9b2ff0f583183c0155060d243afd295770b9 (diff)
feat(config): switch to kdl (#1759)
* chore(config): default kdl keybindings config * tests * work * refactor(config): move stuff around * work * tab merge layout * work * work * layouts working * work * layout tests * work * work * feat(parsing): kdl layouts without config * refactor(kdl): move stuff around * work * tests(layout): add cases and fix bugs * work * fix(kdl): various bugs * chore(layouts): move all layouts to kdl * feat(kdl): shared keybidns * fix(layout): do not count fixed panes toward percentile * fix(keybinds): missing keybinds and actions * fix(config): adjust default tips * refactor(config): move stuff around * fix(tests): make e2e tests pass * fix(kdl): add verbose parsing errors * fix(kdl): focused tab * fix(layout): corret default_tab_template behavior * style(code): fix compile warnings * feat(cli): send actions through the cli * fix(cli): exit only when action is done * fix(cli): open embedded pane from floating pane * fix(cli): send actions to other sessions * feat(cli): command alias * feat(converter): convert old config * feat(converter): convert old layout and theme files * feat(kdl): pretty errors * feat(client): convert old YAML files on startup * fix: various bugs and styling issues * fix: e2e tests * fix(screen): propagate errors after merge * style(clippy): lower clippy level * fix(tests): own session_name variable * style(fmt): rustfmt * fix(cli): various action fixes * style(fmt): rustfmt * fix(themes): loading of theme files * style(fmt): rustfmt * fix(tests): theme fixtures * fix(layouts): better errors on unknown nodes * fix(kdl): clarify valid node terminator error * fix(e2e): adjust close tab test * fix(e2e): adjust close tab test again * style(code): cleanup some comments
Diffstat (limited to 'zellij-utils/src/input/plugins.rs')
-rw-r--r--zellij-utils/src/input/plugins.rs191
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(())
- }
-}