anycms-core 0.5.3

A unified API response library supporting multiple Rust web frameworks
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
use crate::pagination::ResultPagination;
use serde::{Deserialize, Serialize};
use serde_json::Value;

// ============================================================
// Error Codes
// ============================================================

/// Standard error codes for API responses
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(i32)]
#[non_exhaustive]
pub enum ErrorCode {
    Success = 0,
    BadRequest = 400,
    Unauthorized = 401,
    Forbidden = 403,
    NotFound = 404,
    Conflict = 409,
    ValidationError = 422,
    InternalError = 500,
}

impl ErrorCode {
    /// Get the integer representation of the error code
    pub fn as_i32(self) -> i32 {
        self as i32
    }

    /// Convert error code to HTTP status message
    pub fn as_str(self) -> &'static str {
        match self {
            ErrorCode::Success => "Success",
            ErrorCode::BadRequest => "Bad Request",
            ErrorCode::Unauthorized => "Unauthorized",
            ErrorCode::Forbidden => "Forbidden",
            ErrorCode::NotFound => "Not Found",
            ErrorCode::Conflict => "Conflict",
            ErrorCode::ValidationError => "Unprocessable Entity",
            ErrorCode::InternalError => "Internal Server Error",
        }
    }

    /// Try to convert an integer to an ErrorCode
    pub fn from_i32(value: i32) -> Option<Self> {
        match value {
            0 => Some(ErrorCode::Success),
            400 => Some(ErrorCode::BadRequest),
            401 => Some(ErrorCode::Unauthorized),
            403 => Some(ErrorCode::Forbidden),
            404 => Some(ErrorCode::NotFound),
            409 => Some(ErrorCode::Conflict),
            422 => Some(ErrorCode::ValidationError),
            500 => Some(ErrorCode::InternalError),
            _ => None,
        }
    }
}

// ============================================================
// Field-level Validation Errors
// ============================================================

/// A single field-level validation error
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(not(feature = "snake-case"), serde(rename_all = "camelCase"))]
#[cfg_attr(feature = "snake-case", serde(rename_all = "snake_case"))]
pub struct FieldError {
    pub field: String,
    pub message: String,
}

impl FieldError {
    pub fn new(field: impl Into<String>, message: impl Into<String>) -> Self {
        Self {
            field: field.into(),
            message: message.into(),
        }
    }
}

// ============================================================
// Response Data Types
// ============================================================

/// Response data wrapper that can hold either a single value or a list
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(untagged)]
#[non_exhaustive]
pub enum ResponseData<T> {
    /// A single value
    Single(T),
    /// A list of values
    Multiple(Vec<T>),
    /// No data
    #[default]
    Empty,
}

impl<T> ResponseData<T> {
    /// Create a single value response data
    pub fn single(v: T) -> Self {
        ResponseData::Single(v)
    }

    /// Create a multiple values response data
    pub fn multiple(v: Vec<T>) -> Self {
        ResponseData::Multiple(v)
    }

    /// Check if this is a single value
    pub fn is_single(&self) -> bool {
        matches!(self, ResponseData::Single(_))
    }

    /// Check if this is multiple values
    pub fn is_multiple(&self) -> bool {
        matches!(self, ResponseData::Multiple(_))
    }

    /// Check if this is empty
    pub fn is_empty(&self) -> bool {
        matches!(self, ResponseData::Empty)
    }

    /// Get reference to the single value, if present
    pub fn as_single(&self) -> Option<&T> {
        match self {
            ResponseData::Single(v) => Some(v),
            _ => None,
        }
    }

    /// Get reference to the multiple values, if present
    pub fn as_multiple(&self) -> Option<&Vec<T>> {
        match self {
            ResponseData::Multiple(v) => Some(v),
            _ => None,
        }
    }

    /// Number of items in the data: 1 for `Single`, vec len for `Multiple`, 0 for `Empty`
    #[inline]
    pub fn len(&self) -> usize {
        match self {
            ResponseData::Single(_) => 1,
            ResponseData::Multiple(v) => v.len(),
            ResponseData::Empty => 0,
        }
    }

    /// Whether the data carries no items.
    ///
    /// Different from `is_empty()` which checks the `Empty` variant.
    /// `Multiple(vec![])` has `is_empty_data() == true` but `is_empty() == false`.
    #[inline]
    #[allow(clippy::len_zero)]
    pub fn is_empty_data(&self) -> bool {
        self.len() == 0
    }
}

impl<T> From<T> for ResponseData<T> {
    fn from(v: T) -> Self {
        ResponseData::Single(v)
    }
}

impl<T> From<Vec<T>> for ResponseData<T> {
    fn from(v: Vec<T>) -> Self {
        ResponseData::Multiple(v)
    }
}

// ============================================================
// API Response Types
// ============================================================

/// API response wrapper with unified structure
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(not(feature = "snake-case"), serde(rename_all = "camelCase"))]
#[cfg_attr(feature = "snake-case", serde(rename_all = "snake_case"))]
pub struct ApiResult<T> {
    pub success: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub data: Option<ResponseData<T>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub message: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub code: Option<i32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub pagination: Option<ResultPagination>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub errors: Option<Vec<FieldError>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub trace_id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub biz_code: Option<i32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub timestamp: Option<i64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub extra: Option<Value>,
}

impl<T> ApiResult<T> {
    // ── Internal constructor ──

    /// Internal constructor that creates the base struct with all optional fields as None.
    #[inline]
    fn new(
        success: bool,
        data: Option<ResponseData<T>>,
        message: Option<String>,
        code: Option<i32>,
    ) -> Self {
        ApiResult {
            success,
            data,
            message,
            code,
            pagination: None,
            errors: None,
            trace_id: None,
            biz_code: None,
            timestamp: None,
            extra: None,
        }
    }

    // ── Factory methods ──

    /// Create a successful response with a single value
    #[inline]
    pub fn value(v: T) -> Self {
        Self::new(true, Some(ResponseData::single(v)), None, None)
    }

    /// Create a successful response with a list of values
    #[inline]
    pub fn list(v: Vec<T>) -> Self {
        Self::new(true, Some(ResponseData::multiple(v)), None, None)
    }

    /// Create a failed response with a message (alias for `failure`)
    #[inline]
    pub fn fail(message: &str) -> Self {
        Self::failure(message)
    }

    /// Create a failed response with a message
    #[inline]
    pub fn failure(message: &str) -> Self {
        Self::new(false, None, Some(message.to_string()), None)
    }

    /// Create a successful response without data
    ///
    /// Alias for `ok()` - use `ok()` for clarity
    #[inline]
    pub fn success() -> Self {
        Self::ok()
    }

    /// Create a successful response without data (recommended method name)
    #[inline]
    pub fn ok() -> Self {
        Self::new(true, None, None, None)
    }

    /// Create a validation error response with field-level details
    pub fn validation_errors(errors: Vec<FieldError>) -> Self {
        let mut r = Self::new(
            false,
            None,
            Some("Validation failed".to_string()),
            Some(ErrorCode::ValidationError as i32),
        );
        r.errors = Some(errors);
        r
    }

    /// Convert a `Result<T, E>` into `ApiResult<T>`, mapping all errors to `InternalError`
    pub fn from_result<E: std::fmt::Display>(result: Result<T, E>) -> Self {
        match result {
            Ok(data) => Self::value(data),
            Err(err) => Self::new(
                false,
                None,
                Some(err.to_string()),
                Some(ErrorCode::InternalError as i32),
            ),
        }
    }

    // ── Query methods ──

    /// Whether this is a successful response
    #[inline]
    pub fn is_success(&self) -> bool {
        self.success
    }

    /// Whether this is an error response
    #[inline]
    pub fn is_error(&self) -> bool {
        !self.success
    }

    /// Get the error code if this is an error response
    pub fn error_code(&self) -> Option<ErrorCode> {
        if !self.success {
            self.code.and_then(ErrorCode::from_i32)
        } else {
            None
        }
    }

    // ── Builder methods ──

    /// Add extra metadata to the response
    #[inline]
    pub fn with_extra(mut self, key: &str, value: Value) -> Self {
        match self.extra {
            Some(ref mut v) => {
                v[key] = value;
            }
            None => {
                let mut v = serde_json::Map::new();
                v.insert(key.to_string(), value);
                self.extra = Some(v.into());
            }
        }
        self
    }

    /// Set error code for the response
    #[inline]
    pub fn with_code(mut self, code: i32) -> Self {
        self.code = Some(code);
        self
    }

    /// Set pagination metadata for list responses
    #[inline]
    pub fn with_pagination(mut self, pagination: ResultPagination) -> Self {
        self.pagination = Some(pagination);
        self
    }

    /// Set message for the response
    #[inline]
    pub fn with_message(mut self, message: &str) -> Self {
        self.message = Some(message.to_string());
        self
    }

    /// Set field-level validation errors
    #[inline]
    pub fn with_errors(mut self, errors: Vec<FieldError>) -> Self {
        self.errors = Some(errors);
        self
    }

    /// Add a single field-level validation error
    #[inline]
    pub fn with_error(mut self, field: impl Into<String>, message: impl Into<String>) -> Self {
        match self.errors {
            Some(ref mut v) => v.push(FieldError::new(field, message)),
            None => self.errors = Some(vec![FieldError::new(field, message)]),
        }
        self
    }

    /// Set request trace ID
    #[inline]
    pub fn with_trace_id(mut self, id: impl Into<String>) -> Self {
        self.trace_id = Some(id.into());
        self
    }

    /// Set business error code (independent of HTTP status code)
    #[inline]
    pub fn with_biz_code(mut self, code: i32) -> Self {
        self.biz_code = Some(code);
        self
    }

    /// Set timestamp (Unix milliseconds)
    #[inline]
    pub fn with_timestamp(mut self, ts: i64) -> Self {
        self.timestamp = Some(ts);
        self
    }

    /// Set timestamp to current time automatically
    pub fn with_current_timestamp(self) -> Self {
        use std::time::{SystemTime, UNIX_EPOCH};
        let ts = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .map(|d| d.as_millis() as i64)
            .unwrap_or(0);
        self.with_timestamp(ts)
    }
}

// ============================================================
// From Implementations for Error Conversion
// ============================================================

impl<T> From<anyhow::Error> for ApiResult<T> {
    fn from(err: anyhow::Error) -> Self {
        Self::new(
            false,
            None,
            Some(err.to_string()),
            Some(ErrorCode::InternalError as i32),
        )
    }
}

impl<T> From<std::io::Error> for ApiResult<T> {
    fn from(err: std::io::Error) -> Self {
        Self::new(
            false,
            None,
            Some(err.to_string()),
            Some(ErrorCode::InternalError as i32),
        )
    }
}

impl<T> From<std::string::FromUtf8Error> for ApiResult<T> {
    fn from(err: std::string::FromUtf8Error) -> Self {
        Self::new(
            false,
            None,
            Some(err.to_string()),
            Some(ErrorCode::InternalError as i32),
        )
    }
}

/// Convert any `Result<T, E>` where E implements Display to `ApiResult<T>`
impl<T, E: std::fmt::Display + std::fmt::Debug> From<Result<T, E>> for ApiResult<T> {
    fn from(result: Result<T, E>) -> Self {
        match result {
            Ok(data) => Self::value(data),
            Err(err) => Self::new(
                false,
                None,
                Some(format!("{}", err)),
                Some(ErrorCode::InternalError as i32),
            ),
        }
    }
}

/// Type alias for standard Result with `Box<dyn Error>`.
///
/// Commonly used as the return type for API handlers:
/// `-> DefaultResult<impl Responder>` where `Ok(ApiResult::value(data))` is returned.
pub type DefaultResult<T> = Result<T, Box<dyn std::error::Error>>;