use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ContextCause {
Seed,
Message,
ModelReply,
ToolResult,
ProducedPart,
Compaction,
Transform,
ContextTool,
Hook,
FanOut,
Interaction,
Resume,
Framework,
}
impl ContextCause {
pub fn wire(&self) -> &'static str {
match self {
Self::Seed => "seed",
Self::Message => "message",
Self::ModelReply => "model_reply",
Self::ToolResult => "tool_result",
Self::ProducedPart => "produced_part",
Self::Compaction => "compaction",
Self::Transform => "transform",
Self::ContextTool => "context_tool",
Self::Hook => "hook",
Self::FanOut => "fan_out",
Self::Interaction => "interaction",
Self::Resume => "resume",
Self::Framework => "framework",
}
}
}
#[cfg(test)]
mod tests {
use super::*;
const ALL: &[ContextCause] = &[
ContextCause::Seed,
ContextCause::Message,
ContextCause::ModelReply,
ContextCause::ToolResult,
ContextCause::ProducedPart,
ContextCause::Compaction,
ContextCause::Transform,
ContextCause::ContextTool,
ContextCause::Hook,
ContextCause::FanOut,
ContextCause::Interaction,
ContextCause::Resume,
ContextCause::Framework,
];
#[test]
fn each_cause_serializes_as_its_wire_name() {
for cause in ALL {
assert_eq!(
serde_json::to_value(cause).expect("a plain enum serializes"),
serde_json::Value::String(cause.wire().to_string()),
"{cause:?}"
);
let back: ContextCause = serde_json::from_value(serde_json::json!(cause.wire()))
.expect("its own wire name parses back");
assert_eq!(&back, cause);
}
}
#[test]
fn no_two_causes_share_a_wire_name() {
let mut seen = std::collections::HashSet::new();
for cause in ALL {
assert!(seen.insert(cause.wire()), "{cause:?} repeats a wire name");
}
assert_eq!(seen.len(), ALL.len());
}
}