refactor: simplify error handling and conditionals in meta plugins

This commit is contained in:
Andrew Phillips
2025-09-12 10:26:02 -03:00
committed by Andrew Phillips (aider)
parent 022bc70f53
commit 27d3ecad04
2 changed files with 36 additions and 39 deletions

View File

@@ -67,10 +67,10 @@ impl MagicFileMetaPlugin {
// Use the existing cookie and just change flags
if let Some(cookie) = &self.cookie {
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)
.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
let trimmed = result.trim();
@@ -86,7 +86,7 @@ impl MagicFileMetaPlugin {
Ok(cleaned.to_string())
} 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() {
if let Ok(result) = self.get_magic_result(*flags) {
if !result.is_empty() {
// Use process_metadata_outputs to handle output mapping
if let Some(meta_data) = crate::meta_plugin::process_metadata_outputs(
name,
serde_yaml::Value::String(result),
self.base.outputs()
) {
metadata.push(meta_data);
}
if let Ok(result) = self.get_magic_result(*flags)
&& !result.is_empty() {
// Use process_metadata_outputs to handle output mapping
if let Some(meta_data) = crate::meta_plugin::process_metadata_outputs(
name,
serde_yaml::Value::String(result),
self.base.outputs()
) {
metadata.push(meta_data);
}
}
}
@@ -242,4 +241,4 @@ fn register_magic_file_plugin() {
register_meta_plugin(MetaPluginType::MagicFile, |options, outputs| {
Box::new(MagicFileMetaPlugin::new(options, outputs))
});
}
}

View File

@@ -8,7 +8,6 @@ use once_cell::sync::Lazy;
pub mod magic_file;
pub mod exec;
pub mod digest;
pub mod text;
pub mod read_time;
pub mod read_rate;
pub mod hostname;
@@ -18,12 +17,13 @@ pub mod shell;
pub mod shell_pid;
pub mod keep_pid;
pub mod env;
// pub mod text; // Removed duplicate
#[cfg(feature = "magic")]
pub use magic_file::MagicFileMetaPlugin;
pub use exec::MetaPluginExec;
pub use digest::DigestMetaPlugin;
pub use text::TextMetaPlugin;
// pub use text::TextMetaPlugin; // Removed duplicate
pub use read_time::ReadTimeMetaPlugin;
pub use read_rate::ReadRateMetaPlugin;
pub use hostname::HostnameMetaPlugin;
@@ -34,6 +34,8 @@ pub use shell_pid::ShellPidMetaPlugin;
pub use keep_pid::KeepPidMetaPlugin;
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.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MetaData {
@@ -225,11 +227,10 @@ pub fn process_metadata_outputs(internal_name: &str, value: serde_yaml::Value, o
return None;
}
// Check for boolean false to disable the output
if let Some(false_val) = mapping.as_bool() {
if !false_val {
debug!("META: Skipping disabled output: {}", internal_name);
return None;
}
if let Some(false_val) = mapping.as_bool()
&& !false_val {
debug!("META: Skipping disabled output: {}", internal_name);
return None;
}
if let Some(custom_name) = mapping.as_str() {
// Convert the value to a string representation
@@ -350,6 +351,7 @@ pub trait MetaPlugin where Self: 'static {
None
}
/// Initializes the plugin.
///
/// # Returns
@@ -416,7 +418,6 @@ pub trait MetaPlugin where Self: 'static {
}
/// Method to downcast to concrete type (for checking finalization state).
///
/// # Returns
@@ -428,7 +429,7 @@ pub trait MetaPlugin where Self: 'static {
}
/// 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()));
/// 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.
pub fn register_meta_plugin(
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);
}
@@ -463,25 +464,22 @@ pub fn get_meta_plugin(
let mut split_whitespace = true;
if let Some(opts) = &options {
if let Some(command_value) = opts.get("command") {
if let Some(command_str) = command_value.as_str() {
let parts: Vec<&str> = command_str.split_whitespace().collect();
if !parts.is_empty() {
program_name = parts[0].to_string();
args = parts[1..].iter().map(|s| s.to_string()).collect();
}
if let Some(command_value) = opts.get("command")
&& let Some(command_str) = command_value.as_str() {
let parts: Vec<&str> = command_str.split_whitespace().collect();
if !parts.is_empty() {
program_name = parts[0].to_string();
args = parts[1..].iter().map(|s| s.to_string()).collect();
}
}
// Handle other options if needed
if let Some(split_value) = opts.get("split_whitespace") {
if let Some(split_bool) = split_value.as_bool() {
split_whitespace = split_bool;
}
if let Some(split_value) = opts.get("split_whitespace")
&& let Some(split_bool) = split_value.as_bool() {
split_whitespace = split_bool;
}
if let Some(name_value) = opts.get("name") {
if let Some(name_str) = name_value.as_str() {
meta_name = name_str.to_string();
}
if let Some(name_value) = opts.get("name")
&& let Some(name_str) = name_value.as_str() {
meta_name = name_str.to_string();
}
}