1use std::fmt;
2use std::str::FromStr;
3
4use serde::{Deserialize, Serialize};
5
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
22#[serde(rename_all = "lowercase")]
23pub enum Transport {
24 Stdio,
26 Http,
28 Sse,
30}
31
32impl fmt::Display for Transport {
33 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
34 match self {
35 Self::Stdio => write!(f, "stdio"),
36 Self::Http => write!(f, "http"),
37 Self::Sse => write!(f, "sse"),
38 }
39 }
40}
41
42#[derive(Debug, Clone, thiserror::Error)]
44#[error("unknown transport: {0}")]
45pub struct TransportParseError(pub String);
46
47impl FromStr for Transport {
48 type Err = TransportParseError;
49
50 fn from_str(s: &str) -> Result<Self, Self::Err> {
51 match s {
52 "stdio" => Ok(Self::Stdio),
53 "http" => Ok(Self::Http),
54 "sse" => Ok(Self::Sse),
55 other => Err(TransportParseError(other.to_string())),
56 }
57 }
58}
59
60impl TryFrom<&str> for Transport {
61 type Error = TransportParseError;
62
63 fn try_from(s: &str) -> Result<Self, Self::Error> {
64 s.parse()
65 }
66}
67
68#[cfg(test)]
69mod transport_tests {
70 use super::*;
71
72 #[test]
73 fn from_str_accepts_known_variants() {
74 assert_eq!("stdio".parse::<Transport>().unwrap(), Transport::Stdio);
75 assert_eq!("http".parse::<Transport>().unwrap(), Transport::Http);
76 assert_eq!("sse".parse::<Transport>().unwrap(), Transport::Sse);
77 }
78
79 #[test]
80 fn from_str_returns_error_for_unknown() {
81 let err = "websocket".parse::<Transport>().unwrap_err();
82 assert!(err.to_string().contains("websocket"));
83 }
84
85 #[test]
86 fn try_from_str_returns_error_for_unknown() {
87 assert!(Transport::try_from("bogus").is_err());
88 }
89}
90
91#[derive(Debug, Clone, Copy, Default)]
93pub enum OutputFormat {
94 #[default]
96 Text,
97 Json,
99 StreamJson,
101}
102
103impl OutputFormat {
104 pub(crate) fn as_arg(&self) -> &'static str {
105 match self {
106 Self::Text => "text",
107 Self::Json => "json",
108 Self::StreamJson => "stream-json",
109 }
110 }
111}
112
113#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
115#[serde(rename_all = "camelCase")]
116pub enum PermissionMode {
117 #[default]
119 Default,
120 AcceptEdits,
122 #[deprecated(
134 since = "0.5.1",
135 note = "use claude_wrapper::dangerous::DangerousClient instead; \
136 direct BypassPermissions usage is a footgun and will go \
137 away in a future major release"
138 )]
139 BypassPermissions,
140 DontAsk,
142 Plan,
144 Auto,
146}
147
148impl PermissionMode {
149 pub(crate) fn as_arg(&self) -> &'static str {
150 match self {
151 Self::Default => "default",
152 Self::AcceptEdits => "acceptEdits",
153 #[allow(deprecated)]
154 Self::BypassPermissions => "bypassPermissions",
155 Self::DontAsk => "dontAsk",
156 Self::Plan => "plan",
157 Self::Auto => "auto",
158 }
159 }
160}
161
162#[derive(Debug, Clone, Copy, Default)]
164pub enum InputFormat {
165 #[default]
167 Text,
168 StreamJson,
170}
171
172impl InputFormat {
173 pub(crate) fn as_arg(&self) -> &'static str {
174 match self {
175 Self::Text => "text",
176 Self::StreamJson => "stream-json",
177 }
178 }
179}
180
181#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
183#[serde(rename_all = "lowercase")]
184pub enum Effort {
185 Low,
187 Medium,
189 High,
191 Xhigh,
193 Max,
195}
196
197impl Effort {
198 pub(crate) fn as_arg(&self) -> &'static str {
199 match self {
200 Self::Low => "low",
201 Self::Medium => "medium",
202 Self::High => "high",
203 Self::Xhigh => "xhigh",
204 Self::Max => "max",
205 }
206 }
207}
208
209#[derive(Debug, Clone, Copy, Default)]
211pub enum Scope {
212 #[default]
214 Local,
215 User,
217 Project,
219}
220
221impl Scope {
222 pub(crate) fn as_arg(&self) -> &'static str {
223 match self {
224 Self::Local => "local",
225 Self::User => "user",
226 Self::Project => "project",
227 }
228 }
229}
230
231#[cfg(feature = "json")]
248#[derive(Debug, Clone, Deserialize, Serialize)]
249#[serde(rename_all = "camelCase")]
250pub struct AuthStatus {
251 #[serde(default)]
253 pub logged_in: bool,
254 #[serde(default)]
256 pub auth_method: Option<String>,
257 #[serde(default)]
259 pub api_provider: Option<String>,
260 #[serde(default)]
262 pub email: Option<String>,
263 #[serde(default)]
265 pub org_id: Option<String>,
266 #[serde(default)]
268 pub org_name: Option<String>,
269 #[serde(default)]
271 pub subscription_type: Option<String>,
272 #[serde(flatten)]
274 pub extra: std::collections::HashMap<String, serde_json::Value>,
275}
276
277#[cfg(feature = "json")]
279#[derive(Debug, Clone, Deserialize, Serialize)]
280pub struct QueryMessage {
281 #[serde(default)]
283 pub role: String,
284 #[serde(default)]
286 pub content: serde_json::Value,
287 #[serde(flatten)]
289 pub extra: std::collections::HashMap<String, serde_json::Value>,
290}
291
292#[cfg(feature = "json")]
294#[derive(Debug, Clone, Deserialize, Serialize)]
295pub struct QueryResult {
296 #[serde(default)]
298 pub result: String,
299 #[serde(default)]
301 pub session_id: String,
302 #[serde(default, rename = "total_cost_usd", alias = "cost_usd")]
304 pub cost_usd: Option<f64>,
305 #[serde(default)]
307 pub duration_ms: Option<u64>,
308 #[serde(default)]
310 pub num_turns: Option<u32>,
311 #[serde(default)]
313 pub is_error: bool,
314 #[serde(flatten)]
316 pub extra: std::collections::HashMap<String, serde_json::Value>,
317}
318
319#[cfg(all(test, feature = "json"))]
320mod tests {
321 use super::*;
322
323 #[test]
324 fn query_result_deserializes_total_cost_usd() {
325 let json =
326 r#"{"result":"hello","session_id":"s1","total_cost_usd":0.042,"is_error":false}"#;
327 let qr: QueryResult = serde_json::from_str(json).unwrap();
328 assert_eq!(qr.cost_usd, Some(0.042));
329 }
330
331 #[test]
332 fn query_result_deserializes_cost_usd_alias() {
333 let json = r#"{"result":"hello","session_id":"s1","cost_usd":0.01,"is_error":false}"#;
334 let qr: QueryResult = serde_json::from_str(json).unwrap();
335 assert_eq!(qr.cost_usd, Some(0.01));
336 }
337
338 #[test]
339 fn query_result_missing_cost_defaults_to_none() {
340 let json = r#"{"result":"hello","session_id":"s1","is_error":false}"#;
341 let qr: QueryResult = serde_json::from_str(json).unwrap();
342 assert_eq!(qr.cost_usd, None);
343 }
344
345 #[test]
346 fn query_result_from_stream_result_event() {
347 let json = r#"{"type":"result","subtype":"success","result":"streamed","session_id":"sess-1","total_cost_usd":0.03,"num_turns":1,"is_error":false}"#;
351 let qr: QueryResult = serde_json::from_str(json).unwrap();
352 assert_eq!(qr.cost_usd, Some(0.03));
353 assert_eq!(qr.num_turns, Some(1));
354 assert_eq!(qr.session_id, "sess-1");
355 assert_eq!(qr.result, "streamed");
356 assert_eq!(
357 qr.extra.get("type").and_then(|v| v.as_str()),
358 Some("result")
359 );
360 }
361
362 #[test]
363 fn query_result_deserializes_num_turns() {
364 let json = r#"{"result":"done","session_id":"s2","total_cost_usd":0.1,"num_turns":5,"is_error":false}"#;
365 let qr: QueryResult = serde_json::from_str(json).unwrap();
366 assert_eq!(qr.num_turns, Some(5));
367 assert_eq!(qr.cost_usd, Some(0.1));
368 }
369
370 #[test]
371 fn query_result_serializes_as_total_cost_usd() {
372 let qr = QueryResult {
373 result: "ok".into(),
374 session_id: "s1".into(),
375 cost_usd: Some(0.05),
376 duration_ms: None,
377 num_turns: Some(3),
378 is_error: false,
379 extra: Default::default(),
380 };
381 let json = serde_json::to_string(&qr).unwrap();
382 assert!(json.contains("\"total_cost_usd\""));
383 assert!(json.contains("\"num_turns\""));
384 }
385}