use iggy::prelude::Identifier;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum AgentTopic<'a> {
Sessions,
Streams,
Heartbeats,
Control,
Memory,
Audit,
Registry,
WorkflowJournal,
Dlq,
Custom(&'a Identifier),
}
impl AgentTopic<'_> {
pub const fn name(&self) -> Option<&'static str> {
match self {
Self::Sessions => Some(laser_wire::topics::AGENT_SESSIONS),
Self::Streams => Some(laser_wire::topics::AGENT_STREAMS),
Self::Heartbeats => Some(laser_wire::topics::AGENT_HEARTBEATS),
Self::Control => Some(laser_wire::topics::AGENT_CONTROL),
Self::Memory => Some(laser_wire::topics::AGENT_MEMORY),
Self::Audit => Some(laser_wire::topics::AGENT_AUDIT),
Self::Registry => Some(laser_wire::topics::AGENT_REGISTRY),
Self::WorkflowJournal => Some(laser_wire::topics::AGENT_JOURNAL),
Self::Dlq => Some(laser_wire::topics::AGENT_DLQ),
Self::Custom(_) => None,
}
}
pub fn as_identifier(&self) -> Identifier {
match self {
Self::Custom(id) => (**id).clone(),
other => Identifier::named(other.name().expect("non-custom topic has a static name"))
.expect("static topic name is a valid identifier"),
}
}
pub fn topic_string(&self) -> String {
match self {
Self::Custom(id) => id.to_string(),
other => other
.name()
.expect("non-custom topic has a static name")
.to_owned(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn given_a_well_known_topic_when_converted_then_should_map_to_its_identifier() {
assert_eq!(AgentTopic::Sessions.name(), Some("agent.sessions"));
assert_eq!(
AgentTopic::Sessions.as_identifier(),
Identifier::named("agent.sessions").expect("the topic name is a valid identifier")
);
}
#[test]
fn given_a_custom_topic_when_converted_then_should_carry_its_identifier() {
let id = Identifier::named("agent.metrics").expect("the topic name is a valid identifier");
let topic = AgentTopic::Custom(&id);
assert_eq!(topic.name(), None);
assert_eq!(topic.as_identifier(), id);
}
}