From 3b107f09cd48b52b3d3ce60a2bd472439a486f43 Mon Sep 17 00:00:00 2001 From: MCorange Date: Mon, 17 Jun 2024 14:11:47 +0300 Subject: [PATCH] Improved plugin polling and added a struct for the polling result --- src/plugman/mod.rs | 44 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 37 insertions(+), 7 deletions(-) diff --git a/src/plugman/mod.rs b/src/plugman/mod.rs index fbdc0c4..257adf9 100644 --- a/src/plugman/mod.rs +++ b/src/plugman/mod.rs @@ -1,4 +1,4 @@ -use std::path::PathBuf; +use std::{collections::HashMap, path::PathBuf}; use self::plugin::Plugin; @@ -39,7 +39,11 @@ impl PlugMan { self.load(entry.path().to_path_buf())?; } } + if self.plugins.len() == 0 { log::error!("No plugins found"); + return Err(PluginError::NoPlugins); + } + log::info!("Loaded {} plugins", self.plugins.len()); Ok(()) @@ -67,8 +71,8 @@ impl PlugMan { } } - pub fn poll_plugins(&mut self) -> Vec<(String, String)> { - let mut answers = Vec::new(); + pub fn poll_plugins(&mut self) -> HashMap { + let mut answers = HashMap::new(); let len = self.len_cap / self.plugins.len(); for plugin in &self.plugins { if !plugin.enabled() { @@ -76,11 +80,15 @@ impl PlugMan { } match plugin.poll(len) { Ok(v) => { - if !v.is_empty() { - answers.push((plugin.name().clone(), v)); + let mut pth = (plugin.name().clone(), PolledText::new(v)); + if pth.1.text().is_empty() { + pth.1.disable() } + + + answers.insert(pth.0, pth.1); }, - Err(e) => eprintln!("Failed to poll plugin: {e}"), + Err(e) => log::error!("Failed to poll plugin: {e}"), } } @@ -88,10 +96,32 @@ impl PlugMan { } } +#[derive(Debug, Clone)] +pub struct PolledText { + text: String, + disabled: bool +} + +impl PolledText { + pub fn new(text: String) -> Self { + Self { text, disabled: false } + } + pub fn text(&self) -> &String { + &self.text + } + pub fn disabled(&self) -> bool { + self.disabled + } + pub fn disable(&mut self) { + self.disabled = true; + } +} + #[derive(Debug)] pub enum PluginError { DlOpenError, - IoError + IoError, + NoPlugins } impl From for PluginError {