use eclipse_core::config::{BotConfig, Config};
use eclipse_core::runtime::Agent;
use std::collections::BTreeMap;
use std::sync::Arc;
pub type BotAgent = std::result::Result<Arc<Agent>, String>;
pub struct Bots {
agents: BTreeMap<String, BotAgent>,
declared: BTreeMap<String, BotConfig>,
}
impl Default for Bots {
fn default() -> Self {
Self::empty()
}
}
impl Bots {
pub fn empty() -> Self {
Self {
agents: BTreeMap::new(),
declared: BTreeMap::new(),
}
}
pub fn build(cfg: &Config, env: eclipse_core::config::EnvFn) -> Self {
let mut agents = BTreeMap::new();
let mut declared = BTreeMap::new();
for (name, bot) in &cfg.bots {
let built = Agent::for_bot(cfg, env, name)
.map(Arc::new)
.map_err(|e| e.to_string());
agents.insert(name.clone(), built);
declared.insert(name.clone(), bot.clone());
}
Self { agents, declared }
}
pub fn is_empty(&self) -> bool {
self.declared.is_empty()
}
pub fn declared(&self) -> impl Iterator<Item = (&str, &BotConfig)> {
self.declared.iter().map(|(name, bot)| (name.as_str(), bot))
}
pub fn agent(&self, name: &str) -> Option<&BotAgent> {
self.agents.get(name)
}
pub fn declares(&self, name: &str) -> bool {
self.declared.contains_key(name)
}
}