1use std::fmt;
4
5use serde::{Deserialize, Serialize};
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
8#[serde(rename_all = "snake_case")]
9pub enum HarnessId {
10 Claude,
11 Codex,
12 Pi,
13 #[serde(rename = "opencode")]
14 OpenCode,
15 Cursor,
16}
17
18impl HarnessId {
19 pub const fn as_str(self) -> &'static str {
20 match self {
21 Self::Claude => "claude",
22 Self::Codex => "codex",
23 Self::Pi => "pi",
24 Self::OpenCode => "opencode",
25 Self::Cursor => "cursor",
26 }
27 }
28}
29
30impl fmt::Display for HarnessId {
31 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32 f.write_str(self.as_str())
33 }
34}
35
36#[derive(Debug, Clone, Copy, PartialEq, Eq)]
39pub enum Host {
40 Harness(HarnessId),
41 MeridianFlow,
42}
43
44impl From<HarnessId> for Host {
45 fn from(harness: HarnessId) -> Self {
46 Self::Harness(harness)
47 }
48}
49
50impl Host {
51 pub const ALL: &'static [Host] = &[
52 Self::Harness(HarnessId::Claude),
53 Self::Harness(HarnessId::Codex),
54 Self::Harness(HarnessId::Pi),
55 Self::Harness(HarnessId::OpenCode),
56 Self::Harness(HarnessId::Cursor),
57 Self::MeridianFlow,
58 ];
59
60 pub const fn as_str(self) -> &'static str {
61 match self {
62 Self::Harness(harness) => harness.as_str(),
63 Self::MeridianFlow => "meridian-flow",
64 }
65 }
66
67 pub fn parse(name: &str) -> Option<Self> {
68 Self::ALL.iter().copied().find(|host| host.as_str() == name)
69 }
70}
71
72impl Serialize for Host {
73 fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
74 serializer.serialize_str(self.as_str())
75 }
76}
77
78pub const ALL: &[HarnessId] = &[
79 HarnessId::Claude,
80 HarnessId::Codex,
81 HarnessId::Pi,
82 HarnessId::OpenCode,
83 HarnessId::Cursor,
84];
85
86pub fn names() -> String {
88 ALL.iter()
89 .map(|harness| harness.as_str())
90 .collect::<Vec<_>>()
91 .join(", ")
92}
93
94pub(crate) const RESERVED_CONSUMER_NAMESPACES: &[&str] = &["meridian-flow", "meridian-cli"];
98
99pub fn parse(name: &str) -> Option<HarnessId> {
100 let normalized = name.trim().to_ascii_lowercase();
101 ALL.iter()
102 .copied()
103 .find(|harness| harness.as_str() == normalized)
104}
105
106pub fn normalize_name(name: &str) -> Option<String> {
107 parse(name).map(|id| id.as_str().to_owned())
108}
109
110#[cfg(test)]
111mod tests {
112 use super::*;
113
114 #[test]
115 fn parse_and_normalize_are_case_insensitive() {
116 assert_eq!(parse("OpenCode"), Some(HarnessId::OpenCode));
117 assert_eq!(normalize_name(" OpenCode "), Some("opencode".to_string()));
118 assert_eq!(parse("gemini"), None);
119 }
120
121 #[test]
122 fn serde_names_round_trip_registry_names() {
123 for &harness in ALL {
124 let json = serde_json::to_string(&harness).unwrap();
125 assert_eq!(json, format!("\"{}\"", harness.as_str()));
126 assert_eq!(serde_json::from_str::<HarnessId>(&json).unwrap(), harness);
127 }
128 assert!(serde_json::from_str::<HarnessId>("\"open_code\"").is_err());
129 }
130}