pub struct JsonResponse<T> {
pub success: bool,
pub data: Option<T>,
pub error: Option<String>,
}Expand description
Standard JSON response structure for all CLI commands.
This structure provides a consistent format for all command outputs when JSON format is requested. Every command must wrap its output data in this structure to ensure API consistency.
§Structure
{
"success": true,
"data": { ... }
}Or on error:
{
"success": false,
"error": "Error message"
}§Examples
Creating a success response:
use sublime_cli_tools::output::JsonResponse;
use serde::Serialize;
#[derive(Serialize)]
struct InitResult {
config_file: String,
strategy: String,
}
let result = InitResult {
config_file: "repo.config.yaml".to_string(),
strategy: "independent".to_string(),
};
let response = JsonResponse::success(result);
assert_eq!(response.success, true);
assert!(response.data.is_some());
assert!(response.error.is_none());Creating an error response:
use sublime_cli_tools::output::JsonResponse;
let response: JsonResponse<()> = JsonResponse::error(
"Failed to initialize: directory is not empty".to_string()
);
assert_eq!(response.success, false);
assert!(response.data.is_none());
assert!(response.error.is_some());Fields§
§success: boolIndicates whether the operation was successful.
true: Operation completed successfully,datafield will be presentfalse: Operation failed,errorfield will be present
data: Option<T>The response data (only present when success is true).
This field contains the command-specific output data. It is omitted
from the JSON output when None to keep responses clean.
error: Option<String>The error message (only present when success is false).
This field contains a human-readable error message explaining what
went wrong. It is omitted from the JSON output when None.
Implementations§
Source§impl<T> JsonResponse<T>
impl<T> JsonResponse<T>
Sourcepub fn success(data: T) -> Self
pub fn success(data: T) -> Self
Creates a successful response with data.
The resulting JSON will include success: true and the provided data.
§Examples
use sublime_cli_tools::output::JsonResponse;
use serde::Serialize;
#[derive(Serialize)]
struct PackageInfo {
name: String,
version: String,
}
let info = PackageInfo {
name: "@org/core".to_string(),
version: "1.2.3".to_string(),
};
let response = JsonResponse::success(info);
assert!(response.success);
assert!(response.data.is_some());
assert!(response.error.is_none());Sourcepub fn error(message: String) -> Self
pub fn error(message: String) -> Self
Creates an error response with a message.
The resulting JSON will include success: false and the provided error message.
§Examples
use sublime_cli_tools::output::JsonResponse;
let response: JsonResponse<String> = JsonResponse::error(
"Package not found in workspace".to_string()
);
assert!(!response.success);
assert!(response.data.is_none());
assert_eq!(response.error, Some("Package not found in workspace".to_string()));Sourcepub fn is_success(&self) -> bool
pub fn is_success(&self) -> bool
Returns whether this response represents a success.
§Examples
use sublime_cli_tools::output::JsonResponse;
let success_response = JsonResponse::success("data");
assert!(success_response.is_success());
let error_response: JsonResponse<String> = JsonResponse::error("failed".to_string());
assert!(!error_response.is_success());Sourcepub fn is_error(&self) -> bool
pub fn is_error(&self) -> bool
Returns whether this response represents an error.
§Examples
use sublime_cli_tools::output::JsonResponse;
let success_response = JsonResponse::success("data");
assert!(!success_response.is_error());
let error_response: JsonResponse<String> = JsonResponse::error("failed".to_string());
assert!(error_response.is_error());Trait Implementations§
Source§impl<T: Clone> Clone for JsonResponse<T>
impl<T: Clone> Clone for JsonResponse<T>
Source§fn clone(&self) -> JsonResponse<T>
fn clone(&self) -> JsonResponse<T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<T: Debug> Debug for JsonResponse<T>
impl<T: Debug> Debug for JsonResponse<T>
Source§impl<T> Default for JsonResponse<T>
impl<T> Default for JsonResponse<T>
Source§fn default() -> Self
fn default() -> Self
Creates a default error response.
This is primarily used for initialization purposes and represents an error state with a generic message.
§Examples
use sublime_cli_tools::output::JsonResponse;
let response: JsonResponse<String> = JsonResponse::default();
assert!(!response.success);
assert_eq!(response.error, Some("Unknown error".to_string()));Source§impl<T: PartialEq> PartialEq for JsonResponse<T>
impl<T: PartialEq> PartialEq for JsonResponse<T>
Source§impl<T> Serialize for JsonResponse<T>where
T: Serialize,
impl<T> Serialize for JsonResponse<T>where
T: Serialize,
impl<T: Eq> Eq for JsonResponse<T>
impl<T> StructuralPartialEq for JsonResponse<T>
Auto Trait Implementations§
impl<T> Freeze for JsonResponse<T>where
T: Freeze,
impl<T> RefUnwindSafe for JsonResponse<T>where
T: RefUnwindSafe,
impl<T> Send for JsonResponse<T>where
T: Send,
impl<T> Sync for JsonResponse<T>where
T: Sync,
impl<T> Unpin for JsonResponse<T>where
T: Unpin,
impl<T> UnwindSafe for JsonResponse<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.