#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct PluginFeatureSpec {
pub plugin_id: &'static str,
pub feature_flag: &'static str,
pub enabled_in_this_binary: bool,
}
pub const PLUGIN_FEATURE_TABLE: &[PluginFeatureSpec] = &[
PluginFeatureSpec {
plugin_id: "apex",
feature_flag: "plugin-apex",
enabled_in_this_binary: cfg!(feature = "plugin-apex"),
},
PluginFeatureSpec {
plugin_id: "abap",
feature_flag: "plugin-abap",
enabled_in_this_binary: cfg!(feature = "plugin-abap"),
},
PluginFeatureSpec {
plugin_id: "servicenow-xanadu-js",
feature_flag: "plugin-servicenow-xanadu",
enabled_in_this_binary: cfg!(feature = "plugin-servicenow-xanadu"),
},
PluginFeatureSpec {
plugin_id: "servicenow-xml",
feature_flag: "plugin-servicenow-xml",
enabled_in_this_binary: cfg!(feature = "plugin-servicenow-xml"),
},
PluginFeatureSpec {
plugin_id: "terraform",
feature_flag: "plugin-terraform",
enabled_in_this_binary: cfg!(feature = "plugin-terraform"),
},
PluginFeatureSpec {
plugin_id: "puppet",
feature_flag: "plugin-puppet",
enabled_in_this_binary: cfg!(feature = "plugin-puppet"),
},
PluginFeatureSpec {
plugin_id: "pulumi",
feature_flag: "plugin-pulumi",
enabled_in_this_binary: cfg!(feature = "plugin-pulumi"),
},
];
#[must_use]
pub fn missing_features_for<S: AsRef<str>>(unknown_ids: &[S]) -> Vec<&'static str> {
let mut out: Vec<&'static str> = unknown_ids
.iter()
.filter_map(|id| {
PLUGIN_FEATURE_TABLE
.iter()
.find(|spec| spec.plugin_id == id.as_ref() && !spec.enabled_in_this_binary)
.map(|spec| spec.feature_flag)
})
.collect();
out.sort_unstable();
out.dedup();
out
}
#[must_use]
pub fn all_unknown_ids_have_features<S: AsRef<str>>(unknown_ids: &[S]) -> bool {
!unknown_ids.is_empty()
&& unknown_ids.iter().all(|id| {
PLUGIN_FEATURE_TABLE
.iter()
.any(|spec| spec.plugin_id == id.as_ref())
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn feature_table_contains_every_known_specialty_plugin() {
let ids: Vec<&'static str> = PLUGIN_FEATURE_TABLE.iter().map(|s| s.plugin_id).collect();
assert_eq!(
ids,
[
"apex",
"abap",
"servicenow-xanadu-js",
"servicenow-xml",
"terraform",
"puppet",
"pulumi",
]
);
}
#[test]
fn missing_features_for_returns_disabled_gates() {
let result = missing_features_for(&["terraform"]);
if !cfg!(feature = "plugin-terraform") {
assert_eq!(result, vec!["plugin-terraform"]);
}
}
#[test]
fn missing_features_for_returns_empty_for_unrelated_ids() {
let result = missing_features_for(&["totally-made-up-plugin-id"]);
assert!(result.is_empty(), "expected empty, got {result:?}");
}
#[test]
fn all_have_features_only_when_every_id_matches() {
assert!(all_unknown_ids_have_features(&["terraform"]));
assert!(all_unknown_ids_have_features(&["terraform", "puppet"]));
assert!(!all_unknown_ids_have_features(&["terraform", "made-up"]));
assert!(!all_unknown_ids_have_features::<&str>(&[]));
}
}