refactor: simplify error handling and conditionals in meta plugins
This commit is contained in:
committed by
Andrew Phillips (aider)
parent
022bc70f53
commit
27d3ecad04
@@ -67,10 +67,10 @@ impl MagicFileMetaPlugin {
|
|||||||
// Use the existing cookie and just change flags
|
// Use the existing cookie and just change flags
|
||||||
if let Some(cookie) = &self.cookie {
|
if let Some(cookie) = &self.cookie {
|
||||||
cookie.set_flags(flags)
|
cookie.set_flags(flags)
|
||||||
.map_err(|e| io::Error::new(io::ErrorKind::Other, format!("Failed to set magic flags: {}", e)))?;
|
.map_err(|e| io::Error::other(format!("Failed to set magic flags: {}", e)))?;
|
||||||
|
|
||||||
let result = cookie.buffer(&self.buffer)
|
let result = cookie.buffer(&self.buffer)
|
||||||
.map_err(|e| io::Error::new(io::ErrorKind::Other, format!("Failed to analyze buffer: {}", e)))?;
|
.map_err(|e| io::Error::other(format!("Failed to analyze buffer: {}", e)))?;
|
||||||
|
|
||||||
// Clean up the result - remove extra whitespace and take first part if needed
|
// Clean up the result - remove extra whitespace and take first part if needed
|
||||||
let trimmed = result.trim();
|
let trimmed = result.trim();
|
||||||
@@ -86,7 +86,7 @@ impl MagicFileMetaPlugin {
|
|||||||
|
|
||||||
Ok(cleaned.to_string())
|
Ok(cleaned.to_string())
|
||||||
} else {
|
} else {
|
||||||
Err(io::Error::new(io::ErrorKind::Other, "Magic cookie not initialized"))
|
Err(io::Error::other("Magic cookie not initialized"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -102,16 +102,15 @@ impl MagicFileMetaPlugin {
|
|||||||
];
|
];
|
||||||
|
|
||||||
for (name, flags) in types_to_process.iter() {
|
for (name, flags) in types_to_process.iter() {
|
||||||
if let Ok(result) = self.get_magic_result(*flags) {
|
if let Ok(result) = self.get_magic_result(*flags)
|
||||||
if !result.is_empty() {
|
&& !result.is_empty() {
|
||||||
// Use process_metadata_outputs to handle output mapping
|
// Use process_metadata_outputs to handle output mapping
|
||||||
if let Some(meta_data) = crate::meta_plugin::process_metadata_outputs(
|
if let Some(meta_data) = crate::meta_plugin::process_metadata_outputs(
|
||||||
name,
|
name,
|
||||||
serde_yaml::Value::String(result),
|
serde_yaml::Value::String(result),
|
||||||
self.base.outputs()
|
self.base.outputs()
|
||||||
) {
|
) {
|
||||||
metadata.push(meta_data);
|
metadata.push(meta_data);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -242,4 +241,4 @@ fn register_magic_file_plugin() {
|
|||||||
register_meta_plugin(MetaPluginType::MagicFile, |options, outputs| {
|
register_meta_plugin(MetaPluginType::MagicFile, |options, outputs| {
|
||||||
Box::new(MagicFileMetaPlugin::new(options, outputs))
|
Box::new(MagicFileMetaPlugin::new(options, outputs))
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -8,7 +8,6 @@ use once_cell::sync::Lazy;
|
|||||||
pub mod magic_file;
|
pub mod magic_file;
|
||||||
pub mod exec;
|
pub mod exec;
|
||||||
pub mod digest;
|
pub mod digest;
|
||||||
pub mod text;
|
|
||||||
pub mod read_time;
|
pub mod read_time;
|
||||||
pub mod read_rate;
|
pub mod read_rate;
|
||||||
pub mod hostname;
|
pub mod hostname;
|
||||||
@@ -18,12 +17,13 @@ pub mod shell;
|
|||||||
pub mod shell_pid;
|
pub mod shell_pid;
|
||||||
pub mod keep_pid;
|
pub mod keep_pid;
|
||||||
pub mod env;
|
pub mod env;
|
||||||
|
// pub mod text; // Removed duplicate
|
||||||
|
|
||||||
#[cfg(feature = "magic")]
|
#[cfg(feature = "magic")]
|
||||||
pub use magic_file::MagicFileMetaPlugin;
|
pub use magic_file::MagicFileMetaPlugin;
|
||||||
pub use exec::MetaPluginExec;
|
pub use exec::MetaPluginExec;
|
||||||
pub use digest::DigestMetaPlugin;
|
pub use digest::DigestMetaPlugin;
|
||||||
pub use text::TextMetaPlugin;
|
// pub use text::TextMetaPlugin; // Removed duplicate
|
||||||
pub use read_time::ReadTimeMetaPlugin;
|
pub use read_time::ReadTimeMetaPlugin;
|
||||||
pub use read_rate::ReadRateMetaPlugin;
|
pub use read_rate::ReadRateMetaPlugin;
|
||||||
pub use hostname::HostnameMetaPlugin;
|
pub use hostname::HostnameMetaPlugin;
|
||||||
@@ -34,6 +34,8 @@ pub use shell_pid::ShellPidMetaPlugin;
|
|||||||
pub use keep_pid::KeepPidMetaPlugin;
|
pub use keep_pid::KeepPidMetaPlugin;
|
||||||
pub use env::EnvMetaPlugin;
|
pub use env::EnvMetaPlugin;
|
||||||
|
|
||||||
|
type PluginConstructor = fn(Option<HashMap<String, serde_yaml::Value>>, Option<HashMap<String, serde_yaml::Value>>) -> Box<dyn MetaPlugin>;
|
||||||
|
|
||||||
/// Represents metadata to be stored.
|
/// Represents metadata to be stored.
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
pub struct MetaData {
|
pub struct MetaData {
|
||||||
@@ -225,11 +227,10 @@ pub fn process_metadata_outputs(internal_name: &str, value: serde_yaml::Value, o
|
|||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
// Check for boolean false to disable the output
|
// Check for boolean false to disable the output
|
||||||
if let Some(false_val) = mapping.as_bool() {
|
if let Some(false_val) = mapping.as_bool()
|
||||||
if !false_val {
|
&& !false_val {
|
||||||
debug!("META: Skipping disabled output: {}", internal_name);
|
debug!("META: Skipping disabled output: {}", internal_name);
|
||||||
return None;
|
return None;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if let Some(custom_name) = mapping.as_str() {
|
if let Some(custom_name) = mapping.as_str() {
|
||||||
// Convert the value to a string representation
|
// Convert the value to a string representation
|
||||||
@@ -350,6 +351,7 @@ pub trait MetaPlugin where Self: 'static {
|
|||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Initializes the plugin.
|
/// Initializes the plugin.
|
||||||
///
|
///
|
||||||
/// # Returns
|
/// # Returns
|
||||||
@@ -416,7 +418,6 @@ pub trait MetaPlugin where Self: 'static {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/// Method to downcast to concrete type (for checking finalization state).
|
/// Method to downcast to concrete type (for checking finalization state).
|
||||||
///
|
///
|
||||||
/// # Returns
|
/// # Returns
|
||||||
@@ -428,7 +429,7 @@ pub trait MetaPlugin where Self: 'static {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Global registry for meta plugins.
|
/// Global registry for meta plugins.
|
||||||
static META_PLUGIN_REGISTRY: Lazy<Mutex<HashMap<MetaPluginType, fn(Option<HashMap<String, serde_yaml::Value>>, Option<HashMap<String, serde_yaml::Value>>) -> Box<dyn MetaPlugin>>>> =
|
static META_PLUGIN_REGISTRY: Lazy<Mutex<HashMap<MetaPluginType, PluginConstructor>>> =
|
||||||
Lazy::new(|| Mutex::new(HashMap::new()));
|
Lazy::new(|| Mutex::new(HashMap::new()));
|
||||||
|
|
||||||
/// Register a meta plugin with the global registry.
|
/// Register a meta plugin with the global registry.
|
||||||
@@ -439,7 +440,7 @@ static META_PLUGIN_REGISTRY: Lazy<Mutex<HashMap<MetaPluginType, fn(Option<HashMa
|
|||||||
/// * `constructor` - The constructor function for creating plugin instances.
|
/// * `constructor` - The constructor function for creating plugin instances.
|
||||||
pub fn register_meta_plugin(
|
pub fn register_meta_plugin(
|
||||||
meta_plugin_type: MetaPluginType,
|
meta_plugin_type: MetaPluginType,
|
||||||
constructor: fn(Option<HashMap<String, serde_yaml::Value>>, Option<HashMap<String, serde_yaml::Value>>) -> Box<dyn MetaPlugin>
|
constructor: PluginConstructor
|
||||||
) {
|
) {
|
||||||
META_PLUGIN_REGISTRY.lock().unwrap().insert(meta_plugin_type, constructor);
|
META_PLUGIN_REGISTRY.lock().unwrap().insert(meta_plugin_type, constructor);
|
||||||
}
|
}
|
||||||
@@ -463,25 +464,22 @@ pub fn get_meta_plugin(
|
|||||||
let mut split_whitespace = true;
|
let mut split_whitespace = true;
|
||||||
|
|
||||||
if let Some(opts) = &options {
|
if let Some(opts) = &options {
|
||||||
if let Some(command_value) = opts.get("command") {
|
if let Some(command_value) = opts.get("command")
|
||||||
if let Some(command_str) = command_value.as_str() {
|
&& let Some(command_str) = command_value.as_str() {
|
||||||
let parts: Vec<&str> = command_str.split_whitespace().collect();
|
let parts: Vec<&str> = command_str.split_whitespace().collect();
|
||||||
if !parts.is_empty() {
|
if !parts.is_empty() {
|
||||||
program_name = parts[0].to_string();
|
program_name = parts[0].to_string();
|
||||||
args = parts[1..].iter().map(|s| s.to_string()).collect();
|
args = parts[1..].iter().map(|s| s.to_string()).collect();
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Handle other options if needed
|
// Handle other options if needed
|
||||||
if let Some(split_value) = opts.get("split_whitespace") {
|
if let Some(split_value) = opts.get("split_whitespace")
|
||||||
if let Some(split_bool) = split_value.as_bool() {
|
&& let Some(split_bool) = split_value.as_bool() {
|
||||||
split_whitespace = split_bool;
|
split_whitespace = split_bool;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if let Some(name_value) = opts.get("name") {
|
if let Some(name_value) = opts.get("name")
|
||||||
if let Some(name_str) = name_value.as_str() {
|
&& let Some(name_str) = name_value.as_str() {
|
||||||
meta_name = name_str.to_string();
|
meta_name = name_str.to_string();
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user