1use serde::Serialize;
8
9#[derive(Debug, Clone, Serialize)]
13pub struct CliError {
14 pub code: String,
16 pub message: String,
18 #[serde(skip_serializing_if = "Option::is_none")]
20 pub hint: Option<String>,
21}
22
23impl CliError {
24 #[must_use]
26 pub fn new(code: impl Into<String>, message: impl Into<String>) -> Self {
27 Self {
28 code: code.into(),
29 message: message.into(),
30 hint: None,
31 }
32 }
33
34 #[must_use]
36 pub fn with_hint(mut self, hint: impl Into<String>) -> Self {
37 self.hint = Some(hint.into());
38 self
39 }
40}
41
42#[derive(Debug, Clone, Serialize)]
48pub struct CliOutput<T: Serialize> {
49 pub success: bool,
51 #[serde(skip_serializing_if = "Option::is_none")]
53 pub data: Option<T>,
54 #[serde(skip_serializing_if = "Option::is_none")]
56 pub error: Option<CliError>,
57 pub platform: String,
59 pub command: String,
61}
62
63impl<T: Serialize> CliOutput<T> {
64 #[must_use]
66 pub fn success(data: T, platform: &str, command: &str) -> Self {
67 Self {
68 success: true,
69 data: Some(data),
70 error: None,
71 platform: platform.into(),
72 command: command.into(),
73 }
74 }
75
76 #[must_use]
78 pub fn failure(error: CliError, platform: &str) -> Self {
79 Self {
80 success: false,
81 data: None,
82 error: Some(error),
83 platform: platform.into(),
84 command: String::new(),
85 }
86 }
87}
88
89#[cfg(test)]
90mod tests {
91 use super::*;
92
93 #[derive(Debug, Clone, Serialize)]
94 struct SampleData {
95 url: String,
96 number: u64,
97 }
98
99 #[test]
100 fn test_success_output_serializes_with_data_no_error() {
101 let data = SampleData {
102 url: "https://github.com/user/repo/issues/1".into(),
103 number: 1,
104 };
105 let output = CliOutput::success(data, "github", "issue create");
106 let json = serde_json::to_string(&output).expect("failed to serialize");
107
108 assert!(json.contains(r#""success":true"#));
109 assert!(json.contains(r#""url""#));
110 assert!(json.contains(r#""number":1"#));
111 assert!(json.contains(r#""platform":"github""#));
112 assert!(json.contains(r#""command":"issue create""#));
113 assert!(!json.contains(r#""error""#));
114 assert!(!json.contains(r#""data":null"#));
115 }
116
117 #[test]
118 fn test_failure_output_serializes_with_error_no_data() {
119 let error = CliError::new("AUTH_FAILED", "Authentication failed");
120 let output: CliOutput<serde_json::Value> = CliOutput::failure(error, "github");
121 let json = serde_json::to_string(&output).expect("failed to serialize");
122
123 assert!(json.contains(r#""success":false"#));
124 assert!(json.contains(r#""code":"AUTH_FAILED""#));
125 assert!(json.contains(r#""message":"Authentication failed""#));
126 assert!(json.contains(r#""platform":"github""#));
127 assert!(!json.contains(r#""data""#));
128 }
129
130 #[test]
131 fn test_hint_omitted_when_none() {
132 let error = CliError::new("NOT_FOUND", "Resource not found");
133 let output: CliOutput<serde_json::Value> = CliOutput::failure(error, "gitlab");
134 let json = serde_json::to_string(&output).expect("failed to serialize");
135
136 assert!(!json.contains(r#""hint""#));
137 }
138
139 #[test]
140 fn test_with_hint_adds_hint() {
141 let error =
142 CliError::new("AUTH_FAILED", "Authentication failed").with_hint("Run 'gf auth login'");
143 let output: CliOutput<serde_json::Value> = CliOutput::failure(error, "github");
144 let json = serde_json::to_string(&output).expect("failed to serialize");
145
146 assert!(json.contains(r#""hint":"Run 'gf auth login'"#));
147 }
148
149 #[test]
150 fn test_cli_error_new_and_with_hint() {
151 let error = CliError::new("TEST_ERROR", "test message").with_hint("test hint");
152
153 assert_eq!(error.code, "TEST_ERROR");
154 assert_eq!(error.message, "test message");
155 assert_eq!(error.hint, Some("test hint".into()));
156 }
157}