JsonResponse

Struct JsonResponse 

Source
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: bool

Indicates whether the operation was successful.

  • true: Operation completed successfully, data field will be present
  • false: Operation failed, error field 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>

Source

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());
Source

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()));
Source

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());
Source

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>

Source§

fn clone(&self) -> JsonResponse<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for JsonResponse<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Default for JsonResponse<T>

Source§

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>

Source§

fn eq(&self, other: &JsonResponse<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T> Serialize for JsonResponse<T>
where T: Serialize,

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl<T: Eq> Eq for JsonResponse<T>

Source§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,