1use serde::{Deserialize, Serialize};
24
25#[derive(Debug, Clone, Serialize, Deserialize)]
27#[serde(tag = "cmd")]
28pub enum Request {
29 #[serde(rename = "PING")]
31 Ping,
32
33 #[serde(rename = "GET_SECRET")]
35 GetSecret { provider: String },
36
37 #[serde(rename = "HAS_SECRET")]
39 HasSecret { provider: String },
40
41 #[serde(rename = "LIST_PROVIDERS")]
43 ListProviders,
44}
45
46#[derive(Debug, Clone, Serialize, Deserialize)]
48#[serde(untagged)]
49pub enum Response {
50 Pong { version: String },
52
53 Secret { value: String },
55
56 Exists { exists: bool },
58
59 Providers { providers: Vec<String> },
61
62 Error { message: String },
64}
65
66#[cfg(test)]
67mod tests {
68 use super::*;
69
70 #[test]
71 fn test_request_serialization() {
72 let ping = Request::Ping;
73 let json = serde_json::to_string(&ping).unwrap();
74 assert_eq!(json, r#"{"cmd":"PING"}"#);
75
76 let get_secret = Request::GetSecret {
77 provider: "anthropic".to_string(),
78 };
79 let json = serde_json::to_string(&get_secret).unwrap();
80 assert_eq!(json, r#"{"cmd":"GET_SECRET","provider":"anthropic"}"#);
81
82 let has_secret = Request::HasSecret {
83 provider: "openai".to_string(),
84 };
85 let json = serde_json::to_string(&has_secret).unwrap();
86 assert_eq!(json, r#"{"cmd":"HAS_SECRET","provider":"openai"}"#);
87
88 let list = Request::ListProviders;
89 let json = serde_json::to_string(&list).unwrap();
90 assert_eq!(json, r#"{"cmd":"LIST_PROVIDERS"}"#);
91 }
92
93 #[test]
94 fn test_response_deserialization() {
95 let json = r#"{"version":"0.9.0"}"#;
97 let response: Response = serde_json::from_str(json).unwrap();
98 assert!(matches!(response, Response::Pong { version } if version == "0.9.0"));
99
100 let json = r#"{"value":"sk-test-123"}"#;
102 let response: Response = serde_json::from_str(json).unwrap();
103 assert!(matches!(response, Response::Secret { value } if value == "sk-test-123"));
104
105 let json = r#"{"exists":true}"#;
107 let response: Response = serde_json::from_str(json).unwrap();
108 assert!(matches!(response, Response::Exists { exists } if exists));
109
110 let json = r#"{"providers":["anthropic","openai"]}"#;
112 let response: Response = serde_json::from_str(json).unwrap();
113 assert!(
114 matches!(response, Response::Providers { providers } if providers == vec!["anthropic", "openai"])
115 );
116
117 let json = r#"{"message":"Not found"}"#;
119 let response: Response = serde_json::from_str(json).unwrap();
120 assert!(matches!(response, Response::Error { message } if message == "Not found"));
121 }
122}