refactor: compose HostnameMetaPlugin with BaseMetaPlugin
Co-authored-by: aider (openai/andrew/openrouter/sonoma-sky-alpha) <aider@aider.chat>
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
use crate::meta_plugin::{MetaPlugin, MetaPluginType};
|
use crate::meta_plugin::{MetaPlugin, MetaPluginType, BaseMetaPlugin};
|
||||||
|
|
||||||
use smart_default::SmartDefault;
|
use smart_default::SmartDefault;
|
||||||
|
|
||||||
@@ -6,16 +6,7 @@ use smart_default::SmartDefault;
|
|||||||
pub struct HostnameMetaPlugin {
|
pub struct HostnameMetaPlugin {
|
||||||
#[default = false]
|
#[default = false]
|
||||||
is_finalized: bool,
|
is_finalized: bool,
|
||||||
#[default(std::collections::HashMap::new())]
|
base: BaseMetaPlugin,
|
||||||
outputs: std::collections::HashMap<String, serde_yaml::Value>,
|
|
||||||
#[default({
|
|
||||||
let mut map = std::collections::HashMap::new();
|
|
||||||
map.insert("hostname".to_string(), serde_yaml::Value::String("true".to_string()));
|
|
||||||
map.insert("hostname_full".to_string(), serde_yaml::Value::Bool(true));
|
|
||||||
map.insert("hostname_short".to_string(), serde_yaml::Value::Bool(true));
|
|
||||||
map
|
|
||||||
})]
|
|
||||||
options: std::collections::HashMap<String, serde_yaml::Value>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl HostnameMetaPlugin {
|
impl HostnameMetaPlugin {
|
||||||
@@ -23,13 +14,17 @@ impl HostnameMetaPlugin {
|
|||||||
options: Option<std::collections::HashMap<String, serde_yaml::Value>>,
|
options: Option<std::collections::HashMap<String, serde_yaml::Value>>,
|
||||||
outputs: Option<std::collections::HashMap<String, serde_yaml::Value>>,
|
outputs: Option<std::collections::HashMap<String, serde_yaml::Value>>,
|
||||||
) -> HostnameMetaPlugin {
|
) -> HostnameMetaPlugin {
|
||||||
// Start with default options
|
let mut base = BaseMetaPlugin::new();
|
||||||
let mut final_options = std::collections::HashMap::new();
|
|
||||||
// Set default values - hostname is now boolean only
|
// Set default outputs
|
||||||
final_options.insert("hostname".to_string(), serde_yaml::Value::Bool(true));
|
let default_outputs = &["hostname", "hostname_full", "hostname_short"];
|
||||||
final_options.insert("hostname_full".to_string(), serde_yaml::Value::Bool(true));
|
base.initialize_plugin(default_outputs, options, outputs);
|
||||||
final_options.insert("hostname_short".to_string(), serde_yaml::Value::Bool(true));
|
|
||||||
|
// Start with default options - hostname is now boolean only
|
||||||
|
base.options.insert("hostname".to_string(), serde_yaml::Value::Bool(true));
|
||||||
|
base.options.insert("hostname_full".to_string(), serde_yaml::Value::Bool(true));
|
||||||
|
base.options.insert("hostname_short".to_string(), serde_yaml::Value::Bool(true));
|
||||||
|
|
||||||
// Override with provided options
|
// Override with provided options
|
||||||
if let Some(opts) = options {
|
if let Some(opts) = options {
|
||||||
for (key, value) in opts {
|
for (key, value) in opts {
|
||||||
@@ -37,54 +32,54 @@ impl HostnameMetaPlugin {
|
|||||||
if key == "hostname"
|
if key == "hostname"
|
||||||
&& let serde_yaml::Value::String(s) = &value {
|
&& let serde_yaml::Value::String(s) = &value {
|
||||||
if s == "false" {
|
if s == "false" {
|
||||||
final_options.insert(key, serde_yaml::Value::Bool(false));
|
base.options.insert(key, serde_yaml::Value::Bool(false));
|
||||||
continue;
|
continue;
|
||||||
} else if s == "true" {
|
} else if s == "true" {
|
||||||
final_options.insert(key, serde_yaml::Value::Bool(true));
|
base.options.insert(key, serde_yaml::Value::Bool(true));
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
final_options.insert(key, value);
|
base.options.insert(key, value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Determine which outputs are enabled based on options
|
// Determine which outputs are enabled based on options
|
||||||
let hostname_enabled = final_options.get("hostname")
|
let hostname_enabled = base.options.get("hostname")
|
||||||
.and_then(|v| v.as_bool())
|
.and_then(|v| v.as_bool())
|
||||||
.unwrap_or(true);
|
.unwrap_or(true);
|
||||||
|
|
||||||
let hostname_full_enabled = final_options.get("hostname_full")
|
let hostname_full_enabled = base.options.get("hostname_full")
|
||||||
.and_then(|v| v.as_bool())
|
.and_then(|v| v.as_bool())
|
||||||
.unwrap_or(true);
|
.unwrap_or(true);
|
||||||
|
|
||||||
let hostname_short_enabled = final_options.get("hostname_short")
|
let hostname_short_enabled = base.options.get("hostname_short")
|
||||||
.and_then(|v| v.as_bool())
|
.and_then(|v| v.as_bool())
|
||||||
.unwrap_or(true);
|
.unwrap_or(true);
|
||||||
|
|
||||||
// Start with default outputs, setting disabled ones to None
|
// Start with default outputs, setting disabled ones to None
|
||||||
let mut final_outputs = std::collections::HashMap::new();
|
let mut final_outputs = std::collections::HashMap::new();
|
||||||
|
|
||||||
// Handle hostname output
|
// Handle hostname output
|
||||||
if hostname_enabled {
|
if hostname_enabled {
|
||||||
final_outputs.insert("hostname".to_string(), serde_yaml::Value::String("hostname".to_string()));
|
final_outputs.insert("hostname".to_string(), serde_yaml::Value::String("hostname".to_string()));
|
||||||
} else {
|
} else {
|
||||||
final_outputs.insert("hostname".to_string(), serde_yaml::Value::Null);
|
final_outputs.insert("hostname".to_string(), serde_yaml::Value::Null);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle hostname_full output
|
// Handle hostname_full output
|
||||||
if hostname_full_enabled {
|
if hostname_full_enabled {
|
||||||
final_outputs.insert("hostname_full".to_string(), serde_yaml::Value::String("hostname_full".to_string()));
|
final_outputs.insert("hostname_full".to_string(), serde_yaml::Value::String("hostname_full".to_string()));
|
||||||
} else {
|
} else {
|
||||||
final_outputs.insert("hostname_full".to_string(), serde_yaml::Value::Null);
|
final_outputs.insert("hostname_full".to_string(), serde_yaml::Value::Null);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle hostname_short output
|
// Handle hostname_short output
|
||||||
if hostname_short_enabled {
|
if hostname_short_enabled {
|
||||||
final_outputs.insert("hostname_short".to_string(), serde_yaml::Value::String("hostname_short".to_string()));
|
final_outputs.insert("hostname_short".to_string(), serde_yaml::Value::String("hostname_short".to_string()));
|
||||||
} else {
|
} else {
|
||||||
final_outputs.insert("hostname_short".to_string(), serde_yaml::Value::Null);
|
final_outputs.insert("hostname_short".to_string(), serde_yaml::Value::Null);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Override with provided outputs, but only if they're enabled
|
// Override with provided outputs, but only if they're enabled
|
||||||
if let Some(outs) = outputs {
|
if let Some(outs) = outputs {
|
||||||
for (key, value) in outs {
|
for (key, value) in outs {
|
||||||
@@ -105,11 +100,12 @@ impl HostnameMetaPlugin {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
base.outputs = final_outputs;
|
||||||
|
|
||||||
HostnameMetaPlugin {
|
HostnameMetaPlugin {
|
||||||
is_finalized: false,
|
is_finalized: false,
|
||||||
outputs: final_outputs,
|
base,
|
||||||
options: final_options,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -238,15 +234,15 @@ impl MetaPlugin for HostnameMetaPlugin {
|
|||||||
let short_hostname = full_hostname.split('.').next().unwrap_or(&full_hostname).to_string();
|
let short_hostname = full_hostname.split('.').next().unwrap_or(&full_hostname).to_string();
|
||||||
|
|
||||||
// Determine which hostnames to include based on options
|
// Determine which hostnames to include based on options
|
||||||
let hostname_enabled = self.options.get("hostname")
|
let hostname_enabled = self.base.options.get("hostname")
|
||||||
.and_then(|v| v.as_bool())
|
.and_then(|v| v.as_bool())
|
||||||
.unwrap_or(true);
|
.unwrap_or(true);
|
||||||
|
|
||||||
let hostname_full_enabled = self.options.get("hostname_full")
|
let hostname_full_enabled = self.base.options.get("hostname_full")
|
||||||
.and_then(|v| v.as_bool())
|
.and_then(|v| v.as_bool())
|
||||||
.unwrap_or(true);
|
.unwrap_or(true);
|
||||||
|
|
||||||
let hostname_short_enabled = self.options.get("hostname_short")
|
let hostname_short_enabled = self.base.options.get("hostname_short")
|
||||||
.and_then(|v| v.as_bool())
|
.and_then(|v| v.as_bool())
|
||||||
.unwrap_or(true);
|
.unwrap_or(true);
|
||||||
|
|
||||||
@@ -265,7 +261,7 @@ impl MetaPlugin for HostnameMetaPlugin {
|
|||||||
&& let Some(meta_data) = crate::meta_plugin::process_metadata_outputs(
|
&& let Some(meta_data) = crate::meta_plugin::process_metadata_outputs(
|
||||||
"hostname",
|
"hostname",
|
||||||
serde_yaml::Value::String(hostname_value.clone()),
|
serde_yaml::Value::String(hostname_value.clone()),
|
||||||
&self.outputs
|
self.base.outputs()
|
||||||
) {
|
) {
|
||||||
metadata.push(meta_data);
|
metadata.push(meta_data);
|
||||||
}
|
}
|
||||||
@@ -273,7 +269,7 @@ impl MetaPlugin for HostnameMetaPlugin {
|
|||||||
&& let Some(meta_data) = crate::meta_plugin::process_metadata_outputs(
|
&& let Some(meta_data) = crate::meta_plugin::process_metadata_outputs(
|
||||||
"hostname_full",
|
"hostname_full",
|
||||||
serde_yaml::Value::String(full_hostname.clone()),
|
serde_yaml::Value::String(full_hostname.clone()),
|
||||||
&self.outputs
|
self.base.outputs()
|
||||||
) {
|
) {
|
||||||
metadata.push(meta_data);
|
metadata.push(meta_data);
|
||||||
}
|
}
|
||||||
@@ -281,7 +277,7 @@ impl MetaPlugin for HostnameMetaPlugin {
|
|||||||
&& let Some(meta_data) = crate::meta_plugin::process_metadata_outputs(
|
&& let Some(meta_data) = crate::meta_plugin::process_metadata_outputs(
|
||||||
"hostname_short",
|
"hostname_short",
|
||||||
serde_yaml::Value::String(short_hostname.clone()),
|
serde_yaml::Value::String(short_hostname.clone()),
|
||||||
&self.outputs
|
self.base.outputs()
|
||||||
) {
|
) {
|
||||||
metadata.push(meta_data);
|
metadata.push(meta_data);
|
||||||
}
|
}
|
||||||
@@ -289,29 +285,29 @@ impl MetaPlugin for HostnameMetaPlugin {
|
|||||||
// Update outputs based on enabled status
|
// Update outputs based on enabled status
|
||||||
// Handle hostname output
|
// Handle hostname output
|
||||||
if hostname_enabled {
|
if hostname_enabled {
|
||||||
if let Some(output_value) = self.outputs.get_mut("hostname") {
|
if let Some(output_value) = self.base.outputs_mut().get_mut("hostname") {
|
||||||
*output_value = serde_yaml::Value::String(hostname_value);
|
*output_value = serde_yaml::Value::String(hostname_value);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
self.outputs.insert("hostname".to_string(), serde_yaml::Value::Null);
|
self.base.outputs_mut().insert("hostname".to_string(), serde_yaml::Value::Null);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle hostname_full output
|
// Handle hostname_full output
|
||||||
if hostname_full_enabled {
|
if hostname_full_enabled {
|
||||||
if let Some(output_value) = self.outputs.get_mut("hostname_full") {
|
if let Some(output_value) = self.base.outputs_mut().get_mut("hostname_full") {
|
||||||
*output_value = serde_yaml::Value::String(full_hostname);
|
*output_value = serde_yaml::Value::String(full_hostname);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
self.outputs.insert("hostname_full".to_string(), serde_yaml::Value::Null);
|
self.base.outputs_mut().insert("hostname_full".to_string(), serde_yaml::Value::Null);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle hostname_short output
|
// Handle hostname_short output
|
||||||
if hostname_short_enabled {
|
if hostname_short_enabled {
|
||||||
if let Some(output_value) = self.outputs.get_mut("hostname_short") {
|
if let Some(output_value) = self.base.outputs_mut().get_mut("hostname_short") {
|
||||||
*output_value = serde_yaml::Value::String(short_hostname);
|
*output_value = serde_yaml::Value::String(short_hostname);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
self.outputs.insert("hostname_short".to_string(), serde_yaml::Value::Null);
|
self.base.outputs_mut().insert("hostname_short".to_string(), serde_yaml::Value::Null);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mark as finalized since this plugin only needs to run once
|
// Mark as finalized since this plugin only needs to run once
|
||||||
@@ -324,11 +320,11 @@ impl MetaPlugin for HostnameMetaPlugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn outputs(&self) -> &std::collections::HashMap<String, serde_yaml::Value> {
|
fn outputs(&self) -> &std::collections::HashMap<String, serde_yaml::Value> {
|
||||||
&self.outputs
|
self.base.outputs()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn outputs_mut(&mut self) -> &mut std::collections::HashMap<String, serde_yaml::Value> {
|
fn outputs_mut(&mut self) -> &mut std::collections::HashMap<String, serde_yaml::Value> {
|
||||||
&mut self.outputs
|
self.base.outputs_mut()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn default_outputs(&self) -> Vec<String> {
|
fn default_outputs(&self) -> Vec<String> {
|
||||||
@@ -341,11 +337,11 @@ impl MetaPlugin for HostnameMetaPlugin {
|
|||||||
|
|
||||||
|
|
||||||
fn options(&self) -> &std::collections::HashMap<String, serde_yaml::Value> {
|
fn options(&self) -> &std::collections::HashMap<String, serde_yaml::Value> {
|
||||||
&self.options
|
self.base.options()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn options_mut(&mut self) -> &mut std::collections::HashMap<String, serde_yaml::Value> {
|
fn options_mut(&mut self) -> &mut std::collections::HashMap<String, serde_yaml::Value> {
|
||||||
&mut self.options
|
self.base.options_mut()
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -357,4 +353,4 @@ fn register_hostname_plugin() {
|
|||||||
register_meta_plugin(MetaPluginType::Hostname, |options, outputs| {
|
register_meta_plugin(MetaPluginType::Hostname, |options, outputs| {
|
||||||
Box::new(HostnameMetaPlugin::new(options, outputs))
|
Box::new(HostnameMetaPlugin::new(options, outputs))
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user