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;
@ -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<String, PolledText> {
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<dlopen::Error> for PluginError {