diff --git a/src/meta_plugin/hostname.rs b/src/meta_plugin/hostname.rs index d63c73d..cc1d56f 100644 --- a/src/meta_plugin/hostname.rs +++ b/src/meta_plugin/hostname.rs @@ -1,4 +1,4 @@ -use crate::meta_plugin::{MetaPlugin, MetaPluginType}; +use crate::meta_plugin::{MetaPlugin, MetaPluginType, BaseMetaPlugin}; use smart_default::SmartDefault; @@ -6,16 +6,7 @@ use smart_default::SmartDefault; pub struct HostnameMetaPlugin { #[default = false] is_finalized: bool, - #[default(std::collections::HashMap::new())] - outputs: std::collections::HashMap, - #[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, + base: BaseMetaPlugin, } impl HostnameMetaPlugin { @@ -23,13 +14,17 @@ impl HostnameMetaPlugin { options: Option>, outputs: Option>, ) -> HostnameMetaPlugin { - // Start with default options - let mut final_options = std::collections::HashMap::new(); - // Set default values - hostname is now boolean only - final_options.insert("hostname".to_string(), serde_yaml::Value::Bool(true)); - final_options.insert("hostname_full".to_string(), serde_yaml::Value::Bool(true)); - final_options.insert("hostname_short".to_string(), serde_yaml::Value::Bool(true)); - + let mut base = BaseMetaPlugin::new(); + + // Set default outputs + let default_outputs = &["hostname", "hostname_full", "hostname_short"]; + base.initialize_plugin(default_outputs, options, outputs); + + // 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 if let Some(opts) = options { for (key, value) in opts { @@ -37,54 +32,54 @@ impl HostnameMetaPlugin { if key == "hostname" && let serde_yaml::Value::String(s) = &value { if s == "false" { - final_options.insert(key, serde_yaml::Value::Bool(false)); + base.options.insert(key, serde_yaml::Value::Bool(false)); continue; } else if s == "true" { - final_options.insert(key, serde_yaml::Value::Bool(true)); + base.options.insert(key, serde_yaml::Value::Bool(true)); continue; } } - final_options.insert(key, value); + base.options.insert(key, value); } } - + // 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()) .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()) .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()) .unwrap_or(true); - + // Start with default outputs, setting disabled ones to None let mut final_outputs = std::collections::HashMap::new(); - + // Handle hostname output if hostname_enabled { final_outputs.insert("hostname".to_string(), serde_yaml::Value::String("hostname".to_string())); } else { final_outputs.insert("hostname".to_string(), serde_yaml::Value::Null); } - + // Handle hostname_full output if hostname_full_enabled { final_outputs.insert("hostname_full".to_string(), serde_yaml::Value::String("hostname_full".to_string())); } else { final_outputs.insert("hostname_full".to_string(), serde_yaml::Value::Null); } - + // Handle hostname_short output if hostname_short_enabled { final_outputs.insert("hostname_short".to_string(), serde_yaml::Value::String("hostname_short".to_string())); } else { final_outputs.insert("hostname_short".to_string(), serde_yaml::Value::Null); } - + // Override with provided outputs, but only if they're enabled if let Some(outs) = outputs { for (key, value) in outs { @@ -105,11 +100,12 @@ impl HostnameMetaPlugin { } } } - + + base.outputs = final_outputs; + HostnameMetaPlugin { is_finalized: false, - outputs: final_outputs, - options: final_options, + base, } } @@ -238,15 +234,15 @@ impl MetaPlugin for HostnameMetaPlugin { let short_hostname = full_hostname.split('.').next().unwrap_or(&full_hostname).to_string(); // 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()) .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()) .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()) .unwrap_or(true); @@ -265,7 +261,7 @@ impl MetaPlugin for HostnameMetaPlugin { && let Some(meta_data) = crate::meta_plugin::process_metadata_outputs( "hostname", serde_yaml::Value::String(hostname_value.clone()), - &self.outputs + self.base.outputs() ) { metadata.push(meta_data); } @@ -273,7 +269,7 @@ impl MetaPlugin for HostnameMetaPlugin { && let Some(meta_data) = crate::meta_plugin::process_metadata_outputs( "hostname_full", serde_yaml::Value::String(full_hostname.clone()), - &self.outputs + self.base.outputs() ) { metadata.push(meta_data); } @@ -281,7 +277,7 @@ impl MetaPlugin for HostnameMetaPlugin { && let Some(meta_data) = crate::meta_plugin::process_metadata_outputs( "hostname_short", serde_yaml::Value::String(short_hostname.clone()), - &self.outputs + self.base.outputs() ) { metadata.push(meta_data); } @@ -289,29 +285,29 @@ impl MetaPlugin for HostnameMetaPlugin { // Update outputs based on enabled status // Handle hostname output 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); } } 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 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); } } 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 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); } } 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 @@ -324,11 +320,11 @@ impl MetaPlugin for HostnameMetaPlugin { } fn outputs(&self) -> &std::collections::HashMap { - &self.outputs + self.base.outputs() } fn outputs_mut(&mut self) -> &mut std::collections::HashMap { - &mut self.outputs + self.base.outputs_mut() } fn default_outputs(&self) -> Vec { @@ -341,11 +337,11 @@ impl MetaPlugin for HostnameMetaPlugin { fn options(&self) -> &std::collections::HashMap { - &self.options + self.base.options() } fn options_mut(&mut self) -> &mut std::collections::HashMap { - &mut self.options + self.base.options_mut() } } @@ -357,4 +353,4 @@ fn register_hostname_plugin() { register_meta_plugin(MetaPluginType::Hostname, |options, outputs| { Box::new(HostnameMetaPlugin::new(options, outputs)) }); -} \ No newline at end of file +}