webull_rs/models/
response.rs

1use serde::{Deserialize, Serialize};
2
3/// Common API response structure.
4#[derive(Debug, Clone, Serialize, Deserialize)]
5pub struct ApiResponse<T> {
6    /// Success flag
7    pub success: bool,
8
9    /// Response data
10    #[serde(skip_serializing_if = "Option::is_none")]
11    pub data: Option<T>,
12
13    /// Error code
14    #[serde(skip_serializing_if = "Option::is_none")]
15    pub code: Option<String>,
16
17    /// Error message
18    #[serde(skip_serializing_if = "Option::is_none")]
19    pub message: Option<String>,
20}
21
22impl<T> ApiResponse<T> {
23    /// Create a new successful response.
24    pub fn success(data: T) -> Self {
25        Self {
26            success: true,
27            data: Some(data),
28            code: None,
29            message: None,
30        }
31    }
32
33    /// Create a new error response.
34    pub fn error(code: impl Into<String>, message: impl Into<String>) -> Self {
35        Self {
36            success: false,
37            data: None,
38            code: Some(code.into()),
39            message: Some(message.into()),
40        }
41    }
42
43    /// Check if the response is successful.
44    pub fn is_success(&self) -> bool {
45        self.success
46    }
47
48    /// Get the data from the response.
49    pub fn get_data(&self) -> Option<&T> {
50        self.data.as_ref()
51    }
52}
53
54/// Pagination information.
55#[derive(Debug, Clone, Serialize, Deserialize)]
56pub struct Pagination {
57    /// Current page
58    pub page: u32,
59
60    /// Page size
61    pub page_size: u32,
62
63    /// Total number of items
64    pub total: u32,
65
66    /// Total number of pages
67    pub total_pages: u32,
68}
69
70/// Paginated API response.
71#[derive(Debug, Clone, Serialize, Deserialize)]
72pub struct PaginatedResponse<T> {
73    /// Success flag
74    pub success: bool,
75
76    /// Response data
77    #[serde(skip_serializing_if = "Option::is_none")]
78    pub data: Option<Vec<T>>,
79
80    /// Pagination information
81    #[serde(skip_serializing_if = "Option::is_none")]
82    pub pagination: Option<Pagination>,
83
84    /// Error code
85    #[serde(skip_serializing_if = "Option::is_none")]
86    pub code: Option<String>,
87
88    /// Error message
89    #[serde(skip_serializing_if = "Option::is_none")]
90    pub message: Option<String>,
91}
92
93impl<T> PaginatedResponse<T> {
94    /// Create a new successful paginated response.
95    pub fn success(data: Vec<T>, pagination: Pagination) -> Self {
96        Self {
97            success: true,
98            data: Some(data),
99            pagination: Some(pagination),
100            code: None,
101            message: None,
102        }
103    }
104
105    /// Create a new error response.
106    pub fn error(code: impl Into<String>, message: impl Into<String>) -> Self {
107        Self {
108            success: false,
109            data: None,
110            pagination: None,
111            code: Some(code.into()),
112            message: Some(message.into()),
113        }
114    }
115
116    /// Check if the response is successful.
117    pub fn is_success(&self) -> bool {
118        self.success
119    }
120
121    /// Get the data from the response.
122    pub fn get_data(&self) -> Option<&Vec<T>> {
123        self.data.as_ref()
124    }
125
126    /// Get the pagination information.
127    pub fn get_pagination(&self) -> Option<&Pagination> {
128        self.pagination.as_ref()
129    }
130}