use std::{fmt::Debug, sync::Arc};
use utils::sync::Mutex;
use crate::application::{Receiver, Sender};
use crate::{
application::Events,
configuration::{Configuration, ConfigurationEvent},
core::{listener::Listener, Entity, EntityHandle},
};
#[cfg(feature = "headed")]
#[doc(hidden)]
pub mod http;
pub trait Inspectable: Send + Sync {
fn as_string(&self) -> String;
fn class_name(&self) -> &'static str {
std::any::type_name::<Self>()
}
fn set(&mut self, key: &str, value: &str) -> Result<(), String> {
Err(
"Inspector mutation is not implemented. The most likely cause is that this inspectable type did not override set."
.to_string(),
)
}
}
pub struct Inspector {
entities: Mutex<Vec<EntityHandle<dyn Inspectable>>>,
events: Sender<Events>,
configuration: Configuration,
}
impl Inspector {
pub fn new(tx: Sender<Events>, configuration: Configuration) -> Self {
let entities = Mutex::new(Vec::<EntityHandle<dyn Inspectable>>::with_capacity(32768));
Self {
entities,
events: tx,
configuration,
}
}
pub fn configuration_events(&self) -> Vec<ConfigurationEvent> {
self.configuration.events()
}
pub fn get_entities(&self, class: Option<&str>) -> Vec<EntityHandle<dyn Inspectable>> {
let entities = self.entities.lock();
let mut result = Vec::new();
for entity in entities.iter() {
if let Some(class) = class {
if entity.class_name() == class {
result.push(entity.clone());
}
} else {
result.push(entity.clone());
}
}
result
}
pub fn call_set(&self, index: usize, key: &str, value: &str) -> Result<(), String> {
let entities = self.entities.lock();
let entity = entities.get(index).ok_or(
"Inspector entity not found. The most likely cause is that the entity index came from an outdated inspection response."
.to_string(),
)?;
Err("Inspector mutation dispatch is not implemented. The most likely cause is that Inspector::call_set is still a placeholder.".to_string())
}
pub fn close_application(&self) {
self.events.send(Events::Close).unwrap();
}
}