use std::collections::BTreeMap;
use std::fs;
use std::path::{Path, PathBuf};
use thiserror::Error;
use serde::{Deserialize, Serialize};
use url::Url;
use super::layout::{PluginUserConfiguration, RunPlugin, RunPluginLocation};
#[cfg(not(target_family = "wasm"))]
use crate::consts::ASSET_MAP;
pub use crate::data::PluginTag;
use crate::errors::prelude::*;
#[derive(Clone, Debug, Default, Eq, PartialEq, Deserialize, Serialize)]
pub struct PluginAliases {
pub aliases: BTreeMap<String, RunPlugin>,
}
impl PluginAliases {
pub fn merge(&mut self, other: Self) {
self.aliases.extend(other.aliases);
}
pub fn from_data(aliases: BTreeMap<String, RunPlugin>) -> Self {
PluginAliases { aliases }
}
pub fn list(&self) -> Vec<String> {
self.aliases.keys().cloned().collect()
}
}
#[derive(Clone, Debug, PartialEq, Eq, Hash, Deserialize, Serialize)]
pub struct PluginConfig {
pub path: PathBuf,
pub _allow_exec_host_cmd: bool,
pub location: RunPluginLocation,
pub initial_userspace_configuration: PluginUserConfiguration,
pub initial_cwd: Option<PathBuf>,
}
impl PluginConfig {
pub fn from_run_plugin(run_plugin: &RunPlugin) -> Option<PluginConfig> {
match &run_plugin.location {
RunPluginLocation::File(path) => Some(PluginConfig {
path: path.clone(),
_allow_exec_host_cmd: run_plugin._allow_exec_host_cmd,
location: run_plugin.location.clone(),
initial_userspace_configuration: run_plugin.configuration.clone(),
initial_cwd: run_plugin.initial_cwd.clone(),
}),
RunPluginLocation::Zellij(tag) => {
let tag = tag.to_string();
if tag == "status-bar"
|| tag == "tab-bar"
|| tag == "compact-bar"
|| tag == "strider"
|| tag == "session-manager"
|| tag == "configuration"
|| tag == "plugin-manager"
|| tag == "about"
|| tag == "share"
|| tag == "multiple-select"
|| tag == "layout-manager"
|| tag == "link"
{
Some(PluginConfig {
path: PathBuf::from(&tag),
_allow_exec_host_cmd: run_plugin._allow_exec_host_cmd,
location: RunPluginLocation::parse(&format!("zellij:{}", tag), None)
.ok()?,
initial_userspace_configuration: run_plugin.configuration.clone(),
initial_cwd: run_plugin.initial_cwd.clone(),
})
} else {
None
}
},
RunPluginLocation::Remote(_) => Some(PluginConfig {
path: PathBuf::new(),
_allow_exec_host_cmd: run_plugin._allow_exec_host_cmd,
location: run_plugin.location.clone(),
initial_userspace_configuration: run_plugin.configuration.clone(),
initial_cwd: run_plugin.initial_cwd.clone(),
}),
}
}
pub fn resolve_wasm_bytes(&self, plugin_dir: &Path) -> Result<Vec<u8>> {
let err_context =
|err: std::io::Error, path: &PathBuf| format!("{}: '{}'", err, path.display());
let paths_arr = [
&self.path,
&self.path.with_extension("wasm"),
&plugin_dir.join(&self.path).with_extension("wasm"),
];
let mut paths = paths_arr.to_vec();
paths.dedup();
let mut last_err: Result<Vec<u8>> = Err(anyhow!("failed to load plugin from disk"));
for path in paths {
#[cfg(not(target_family = "wasm"))]
if !cfg!(feature = "disable_automatic_asset_installation") && self.is_builtin() {
let asset_path = PathBuf::from("plugins").join(path);
if let Some(bytes) = ASSET_MAP.get(&asset_path) {
log::debug!("Loaded plugin '{}' from internal assets", path.display());
if plugin_dir.join(path).with_extension("wasm").exists() {
log::info!(
"Plugin '{}' exists in the 'PLUGIN DIR' at '{}' but is being ignored",
path.display(),
plugin_dir.display()
);
}
return Ok(bytes.to_vec());
}
}
match fs::read(&path) {
Ok(val) => {
log::debug!("Loaded plugin '{}' from disk", path.display());
return Ok(val);
},
Err(err) => {
last_err = last_err.with_context(|| err_context(err, &path));
},
}
}
#[cfg(not(target_family = "wasm"))]
if self.is_builtin() {
let plugin_path = self.path.with_extension("wasm");
if cfg!(feature = "disable_automatic_asset_installation")
&& ASSET_MAP.contains_key(&PathBuf::from("plugins").join(&plugin_path))
{
return Err(ZellijError::BuiltinPluginMissing {
plugin_path,
plugin_dir: plugin_dir.to_owned(),
source: last_err.unwrap_err(),
})
.context("failed to load a plugin");
} else {
return Err(ZellijError::BuiltinPluginNonexistent {
plugin_path,
source: last_err.unwrap_err(),
})
.context("failed to load a plugin");
}
}
return last_err;
}
pub fn is_builtin(&self) -> bool {
matches!(self.location, RunPluginLocation::Zellij(_))
}
}
#[derive(Error, Debug, PartialEq)]
pub enum PluginsConfigError {
#[error("Duplication in plugin tag names is not allowed: '{}'", String::from(.0.clone()))]
DuplicatePlugins(PluginTag),
#[error("Failed to parse url: {0:?}")]
InvalidUrl(#[from] url::ParseError),
#[error("Only 'file:', 'http(s):' and 'zellij:' url schemes are supported for plugin lookup. '{0}' does not match either.")]
InvalidUrlScheme(Url),
#[error("Could not find plugin at the path: '{0:?}'")]
InvalidPluginLocation(PathBuf),
}