vtiger_client/types.rs
1use indexmap::IndexMap;
2use serde::{Deserialize, Serialize};
3
4/// Supported export formats for Vtiger data.
5///
6/// Each format has different characteristics:
7/// - JSON creates a single file with all records in an array
8/// - JSON Lines creates one JSON object per line (streaming-friendly)
9/// - CSV creates a traditional spreadsheet format with headers
10///
11/// # Examples
12///
13/// ```no_run
14/// use vtiger_client::ExportFormat;
15///
16/// let formats = vec![
17/// ExportFormat::Json,
18/// ExportFormat::JsonLines,
19/// ExportFormat::CSV,
20/// ];
21/// ```
22#[derive(Debug, PartialEq)]
23pub enum ExportFormat {
24 /// JSON format (.json) - exports data as a single JSON array
25 Json,
26 /// JSON Lines format (.jsonl) - exports each record as a separate JSON object on its own line
27 JsonLines,
28 /// CSV format (.csv) - exports data as comma-separated values with headers
29 CSV,
30}
31
32/// Response wrapper for all Vtiger REST API calls.
33///
34/// This struct represents the standard response format returned by the Vtiger API.
35/// All API responses follow this structure, containing a success flag, optional result data,
36/// and optional error information.
37///
38/// # Fields
39///
40/// * `success` - Indicates whether the API call was successful
41/// * `result` - Contains the response data when the call succeeds
42/// * `error` - Contains error details when the call fails
43///
44/// # Examples
45///
46/// ```no_run
47/// use vtiger_client::{ApiError, VtigerResponse};
48/// use indexmap::IndexMap;
49///
50/// // Successful response
51/// let success_response = VtigerResponse {
52/// success: true,
53/// result: Some(IndexMap::new()),
54/// error: None,
55/// };
56///
57/// // Error response
58/// let error_response = VtigerResponse {
59/// success: false,
60/// result: None,
61/// error: Some(ApiError {
62/// code: "404".to_string(),
63/// message: "Not Found".to_string(),
64/// }),
65/// };
66/// ```
67#[derive(Debug, Deserialize, Serialize)]
68pub struct VtigerResponse {
69 /// Indicates whether the API call was successful
70 pub success: bool,
71 /// Contains the response data when `success` is true
72 pub result: Option<IndexMap<String, serde_json::Value>>,
73 /// Contains error details when `success` is false
74 pub error: Option<ApiError>,
75}
76
77/// Response format for Vtiger query operations that return multiple records.
78///
79/// This struct is used specifically for API calls that return arrays of data,
80/// such as `SELECT` queries or list operations. It follows the same success/error
81/// pattern as [`VtigerResponse`] but contains a vector of records instead of a single object.
82///
83/// # Examples
84///
85/// ```no_run
86/// use indexmap::IndexMap;
87/// use serde_json::json;
88/// use vtiger_client::VtigerQueryResponse;
89///
90/// // Query response with multiple records
91/// let response = VtigerQueryResponse {
92/// success: true,
93/// result: Some(vec![
94/// IndexMap::from([("id".to_string(), json!("1"))]),
95/// IndexMap::from([("id".to_string(), json!("2"))]),
96/// ]),
97/// error: None,
98/// };
99/// ```
100#[derive(Debug, Deserialize, Serialize)]
101pub struct VtigerQueryResponse {
102 /// Whether the query operation succeeded
103 pub success: bool,
104 /// Array of records returned by the query (present when successful)
105 pub result: Option<Vec<IndexMap<String, serde_json::Value>>>,
106 /// Error details (present when failed)
107 pub error: Option<ApiError>,
108}
109
110/// Error information returned by the Vtiger API.
111///
112/// When an API call fails, the Vtiger API returns structured error information
113/// containing both a code for programmatic handling and a human-readable message.
114///
115/// # Examples
116///
117/// ```no_run
118/// use vtiger_client::ApiError;
119/// let error = ApiError {
120/// code: "AUTHENTICATION_FAILURE".to_string(),
121/// message: "Invalid username or access key".to_string(),
122/// };
123/// ```
124#[derive(Debug, Deserialize, Serialize)]
125pub struct ApiError {
126 /// Error code for programmatic error handling
127 pub code: String,
128 /// Human-readable error message
129 pub message: String,
130}