use crate::hooks::HooksAPI;
use crate::hooks::events::GraphEvent;
use crate::hooks::schedule::{AllGraphSchedules, OnSystemStart};
use crate::node::NodeId;
use polaris_system::plugin::{Plugin, Version};
use polaris_system::resource::LocalResource;
use polaris_system::server::Server;
#[derive(Debug, Clone)]
pub struct SystemInfo {
node_id: NodeId,
system_name: &'static str,
}
impl LocalResource for SystemInfo {}
impl SystemInfo {
pub fn new(node_id: NodeId, system_name: &'static str) -> Self {
Self {
node_id,
system_name,
}
}
#[must_use]
pub fn node_id(&self) -> NodeId {
self.node_id.clone()
}
#[must_use]
pub fn system_name(&self) -> &'static str {
self.system_name
}
}
pub struct DevToolsPlugin {
trace_events: bool,
}
impl DevToolsPlugin {
#[must_use]
pub fn new() -> Self {
Self {
trace_events: false,
}
}
#[must_use]
pub fn with_event_tracing(mut self) -> Self {
self.trace_events = true;
self
}
}
impl Default for DevToolsPlugin {
fn default() -> Self {
Self::new()
}
}
impl Plugin for DevToolsPlugin {
const ID: &'static str = "polaris::dev_tools";
const VERSION: Version = Version::new(0, 0, 1);
fn build(&self, server: &mut Server) {
if !server.contains_api::<HooksAPI>() {
server.insert_api(HooksAPI::new());
}
let hooks = server
.api::<HooksAPI>()
.expect("HooksAPI should be present after initialization");
hooks
.register_provider::<OnSystemStart, _, _>(
"devtools_system_info",
|event: &GraphEvent| {
if let GraphEvent::SystemStart {
node_id,
node_name: system_name,
..
} = event
{
Some(SystemInfo::new(node_id.clone(), system_name))
} else {
None
}
},
)
.expect("DevToolsPlugin hook registration should not fail");
if self.trace_events {
hooks
.register_observer::<AllGraphSchedules, _>(
"devtools_event_trace",
|event: &GraphEvent| {
tracing::debug!("{event}");
},
)
.expect("DevToolsPlugin event tracing registration should not fail");
}
}
}