Improved plugin polling and added a struct for the polling result

This commit is contained in:
Gvidas Juknevičius 2024-06-17 14:11:47 +03:00
parent 08eca1a2d0
commit 3b107f09cd
Signed by: MCorange
GPG Key ID: 12B1346D720B7FBB

View File

@ -1,4 +1,4 @@
use std::path::PathBuf; use std::{collections::HashMap, path::PathBuf};
use self::plugin::Plugin; use self::plugin::Plugin;
@ -39,7 +39,11 @@ impl PlugMan {
self.load(entry.path().to_path_buf())?; self.load(entry.path().to_path_buf())?;
} }
} }
if self.plugins.len() == 0 {
log::error!("No plugins found"); log::error!("No plugins found");
return Err(PluginError::NoPlugins);
}
log::info!("Loaded {} plugins", self.plugins.len()); log::info!("Loaded {} plugins", self.plugins.len());
Ok(()) Ok(())
@ -67,8 +71,8 @@ impl PlugMan {
} }
} }
pub fn poll_plugins(&mut self) -> Vec<(String, String)> { pub fn poll_plugins(&mut self) -> HashMap<String, PolledText> {
let mut answers = Vec::new(); let mut answers = HashMap::new();
let len = self.len_cap / self.plugins.len(); let len = self.len_cap / self.plugins.len();
for plugin in &self.plugins { for plugin in &self.plugins {
if !plugin.enabled() { if !plugin.enabled() {
@ -76,11 +80,15 @@ impl PlugMan {
} }
match plugin.poll(len) { match plugin.poll(len) {
Ok(v) => { Ok(v) => {
if !v.is_empty() { let mut pth = (plugin.name().clone(), PolledText::new(v));
answers.push((plugin.name().clone(), 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)] #[derive(Debug)]
pub enum PluginError { pub enum PluginError {
DlOpenError, DlOpenError,
IoError IoError,
NoPlugins
} }
impl From<dlopen::Error> for PluginError { impl From<dlopen::Error> for PluginError {