pub struct ApiResponse<T: Serialize> {
pub api_version: &'static str,
pub timestamp: u64,
pub request_id: Option<String>,
pub command: Option<String>,
pub success: bool,
pub data: Option<T>,
pub error: Option<ApiError>,
}Expand description
Unified API response envelope.
All CLI commands and daemon endpoints should return this structure for consistent agent parsing.
§Example
use rch_common::api::{ApiResponse, ApiError};
use rch_common::ErrorCode;
// Success response
#[derive(serde::Serialize)]
struct WorkerList {
workers: Vec<String>,
count: usize,
}
let data = WorkerList {
workers: vec!["worker-1".to_string(), "worker-2".to_string()],
count: 2,
};
let response = ApiResponse::ok("workers list", data);
// Error response
let error = ApiError::from_code(ErrorCode::ConfigNotFound);
let response: ApiResponse<()> = ApiResponse::err("config show", error);Fields§
§api_version: &'static strAPI version for compatibility detection.
timestamp: u64Unix timestamp when response was generated.
request_id: Option<String>Optional request ID for correlation/tracing.
command: Option<String>Command that produced this response (e.g., “workers list”).
success: boolWhether the operation succeeded.
data: Option<T>Response data on success.
error: Option<ApiError>Error details on failure.
Implementations§
Source§impl<T: Serialize> ApiResponse<T>
impl<T: Serialize> ApiResponse<T>
Sourcepub fn ok(command: impl Into<String>, data: T) -> Self
pub fn ok(command: impl Into<String>, data: T) -> Self
Create a successful response.
§Arguments
command- Command name that produced this responsedata- Response payload
Sourcepub fn ok_data(data: T) -> Self
pub fn ok_data(data: T) -> Self
Create a successful response without command name.
Useful for daemon endpoints where command context isn’t applicable.
Sourcepub fn with_request_id(self, id: impl Into<String>) -> Self
pub fn with_request_id(self, id: impl Into<String>) -> Self
Add a request ID for correlation.
Sourcepub fn with_command(self, command: impl Into<String>) -> Self
pub fn with_command(self, command: impl Into<String>) -> Self
Add or override command name.
Source§impl<T: Serialize> ApiResponse<T>
impl<T: Serialize> ApiResponse<T>
Trait Implementations§
Source§impl<T: Clone + Serialize> Clone for ApiResponse<T>
impl<T: Clone + Serialize> Clone for ApiResponse<T>
Source§fn clone(&self) -> ApiResponse<T>
fn clone(&self) -> ApiResponse<T>
Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl<T> Deserialize<'static> for ApiResponse<T>where
T: Deserialize<'static> + Serialize,
impl<T> Deserialize<'static> for ApiResponse<T>where
T: Deserialize<'static> + Serialize,
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'static>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'static>,
Deserialize this value from the given Serde deserializer. Read more
Source§impl<T: Serialize + JsonSchema> JsonSchema for ApiResponse<T>
impl<T: Serialize + JsonSchema> JsonSchema for ApiResponse<T>
Source§fn schema_name() -> String
fn schema_name() -> String
The name of the generated JSON Schema. Read more
Source§fn schema_id() -> Cow<'static, str>
fn schema_id() -> Cow<'static, str>
Returns a string that uniquely identifies the schema produced by this type. Read more
Source§fn json_schema(generator: &mut SchemaGenerator) -> Schema
fn json_schema(generator: &mut SchemaGenerator) -> Schema
Generates a JSON Schema for this type. Read more
Source§fn is_referenceable() -> bool
fn is_referenceable() -> bool
Whether JSON Schemas generated for this type should be re-used where possible using the
$ref keyword. Read moreAuto Trait Implementations§
impl<T> Freeze for ApiResponse<T>where
T: Freeze,
impl<T> RefUnwindSafe for ApiResponse<T>where
T: RefUnwindSafe,
impl<T> Send for ApiResponse<T>where
T: Send,
impl<T> Sync for ApiResponse<T>where
T: Sync,
impl<T> Unpin for ApiResponse<T>where
T: Unpin,
impl<T> UnsafeUnpin for ApiResponse<T>where
T: UnsafeUnpin,
impl<T> UnwindSafe for ApiResponse<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
Mutably borrows from an owned value. Read more