use serde::Serialize;
#[derive(Debug, Clone, Serialize)]
pub struct CliError {
pub code: String,
pub message: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub hint: Option<String>,
}
impl CliError {
#[must_use]
pub fn new(code: impl Into<String>, message: impl Into<String>) -> Self {
Self {
code: code.into(),
message: message.into(),
hint: None,
}
}
#[must_use]
pub fn with_hint(mut self, hint: impl Into<String>) -> Self {
self.hint = Some(hint.into());
self
}
}
#[derive(Debug, Clone, Serialize)]
pub struct CliOutput<T: Serialize> {
pub success: bool,
#[serde(skip_serializing_if = "Option::is_none")]
pub data: Option<T>,
#[serde(skip_serializing_if = "Option::is_none")]
pub error: Option<CliError>,
pub platform: String,
pub command: String,
}
impl<T: Serialize> CliOutput<T> {
#[must_use]
pub fn success(data: T, platform: &str, command: &str) -> Self {
Self {
success: true,
data: Some(data),
error: None,
platform: platform.into(),
command: command.into(),
}
}
#[must_use]
pub fn failure(error: CliError, platform: &str) -> Self {
Self {
success: false,
data: None,
error: Some(error),
platform: platform.into(),
command: String::new(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[derive(Debug, Clone, Serialize)]
struct SampleData {
url: String,
number: u64,
}
#[test]
fn test_success_output_serializes_with_data_no_error() {
let data = SampleData {
url: "https://github.com/user/repo/issues/1".into(),
number: 1,
};
let output = CliOutput::success(data, "github", "issue create");
let json = serde_json::to_string(&output).expect("failed to serialize");
assert!(json.contains(r#""success":true"#));
assert!(json.contains(r#""url""#));
assert!(json.contains(r#""number":1"#));
assert!(json.contains(r#""platform":"github""#));
assert!(json.contains(r#""command":"issue create""#));
assert!(!json.contains(r#""error""#));
assert!(!json.contains(r#""data":null"#));
}
#[test]
fn test_failure_output_serializes_with_error_no_data() {
let error = CliError::new("AUTH_FAILED", "Authentication failed");
let output: CliOutput<serde_json::Value> = CliOutput::failure(error, "github");
let json = serde_json::to_string(&output).expect("failed to serialize");
assert!(json.contains(r#""success":false"#));
assert!(json.contains(r#""code":"AUTH_FAILED""#));
assert!(json.contains(r#""message":"Authentication failed""#));
assert!(json.contains(r#""platform":"github""#));
assert!(!json.contains(r#""data""#));
}
#[test]
fn test_hint_omitted_when_none() {
let error = CliError::new("NOT_FOUND", "Resource not found");
let output: CliOutput<serde_json::Value> = CliOutput::failure(error, "gitlab");
let json = serde_json::to_string(&output).expect("failed to serialize");
assert!(!json.contains(r#""hint""#));
}
#[test]
fn test_with_hint_adds_hint() {
let error =
CliError::new("AUTH_FAILED", "Authentication failed").with_hint("Run 'gf auth login'");
let output: CliOutput<serde_json::Value> = CliOutput::failure(error, "github");
let json = serde_json::to_string(&output).expect("failed to serialize");
assert!(json.contains(r#""hint":"Run 'gf auth login'"#));
}
#[test]
fn test_cli_error_new_and_with_hint() {
let error = CliError::new("TEST_ERROR", "test message").with_hint("test hint");
assert_eq!(error.code, "TEST_ERROR");
assert_eq!(error.message, "test message");
assert_eq!(error.hint, Some("test hint".into()));
}
}