use crate::action::{ActionIncarnation, ActionKey, ActionRow};
use crate::component::ComponentId;
use crate::error::RegistryError;
use crate::event::LifecycleState;
use crate::status::ComponentStatus;
use super::ComponentRegistry;
use super::support::{snapshot, state};
impl ComponentRegistry {
pub fn action_snapshot(&self) -> Result<Vec<ActionRow>, RegistryError> {
let records = self
.records
.lock()
.map_err(|_| RegistryError::SynchronizationPoisoned)?;
let mut rows = Vec::new();
for record in records.values() {
if state(record)? != LifecycleState::Running {
continue;
}
let incarnation = ActionIncarnation::new(
record
.incarnation
.load(std::sync::atomic::Ordering::Acquire),
);
for declaration in &record.meta.actions {
rows.push(ActionRow {
key: ActionKey {
component_id: record.meta.id,
action_id: declaration.id.clone(),
},
component_name: record.meta.name.clone(),
description: declaration.description.clone(),
request_schema: declaration.request_schema.clone(),
response_schema: declaration.response_schema.clone(),
requirements: declaration.requirements.clone(),
incarnation,
});
}
}
rows.sort_by(|left, right| left.key.cmp(&right.key));
Ok(rows)
}
pub fn status(&self, id: ComponentId) -> Result<Option<ComponentStatus>, RegistryError> {
self.optional_record(id)?
.map(|record| snapshot(id, &record))
.transpose()
}
pub fn len(&self) -> Result<usize, RegistryError> {
self.records
.lock()
.map(|records| records.len())
.map_err(|_| RegistryError::SynchronizationPoisoned)
}
pub fn is_empty(&self) -> Result<bool, RegistryError> {
self.len().map(|length| length == 0)
}
}