yang-base 0.2.1

个人使用构建后端服务器
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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
//! Action 响应结构
//!
//! 提供统一的 API 响应格式,包含状态码、消息和数据。

use crate::error::BaseError;
use serde::Serialize;
use std::path::PathBuf;

/// API 响应
///
/// 统一的 API 响应格式,用于所有 Action 的返回值
///
/// # 字段
///
/// - `code`: 状态码(0 表示成功,非零表示失败)
/// - `message`: 响应消息
/// - `data`: 响应数据(可选)
///
/// 标注 `#[non_exhaustive]`:未来新增字段不构成破坏性变更。
/// 请使用 [`ApiResponse::success`] / [`ApiResponse::fail`] / [`ApiResponse::from_error`] 等构造。
///
/// # 示例
///
/// ```rust,ignore
/// use yang_base::action::ApiResponse;
/// use serde_json::json;
///
/// // 创建成功响应
/// let response = ApiResponse::success(
///     json!({ "id": 123, "name": "Alice" }),
///     "操作成功"
/// );
/// assert_eq!(response.code, 0);
///
/// // 创建失败响应
/// let response = ApiResponse::fail(400001, "参数错误");
/// assert_eq!(response.code, 400001);
/// assert!(response.data.is_none());
/// ```
#[derive(Debug, Clone, Serialize)]
#[non_exhaustive]
pub struct ApiResponse {
    /// 状态码
    ///
    /// - 0: 成功
    /// - 非零: 失败(具体错误码由业务定义)
    pub code: i32,

    /// 响应消息
    ///
    /// 描述操作结果的文本信息
    pub message: String,

    /// 响应数据
    ///
    /// 成功时包含业务数据,失败时通常为 None
    #[serde(skip_serializing_if = "Option::is_none")]
    pub data: Option<serde_json::Value>,

    /// 非 JSON 附件(文件下载/预览/重定向),由传输层消费。
    ///
    /// 仅当 Action 以 [`ResponseBody`] 为输出时由派发边界填充;
    /// `serde(skip)` 保证普通 Action 的 JSON 线格式不受影响。
    #[serde(skip)]
    pub attachment: Option<ResponseAttachment>,
}

impl ApiResponse {
    /// 创建成功响应
    ///
    /// 状态码为 0,包含业务数据。序列化失败时返回 `BaseError::JsonSerializeFailed`。
    ///
    /// # 参数
    ///
    /// - `data`: 响应数据(任何可序列化的类型)
    /// - `message`: 成功消息
    ///
    /// # 返回
    ///
    /// - `Ok(ApiResponse)`: 序列化成功时返回响应实例
    /// - `Err(BaseError::JsonSerializeFailed)`: 序列化失败时返回错误
    ///
    /// # Errors
    ///
    /// - `BaseError::JsonSerializeFailed`: 当 `data` 无法被序列化为 JSON 时
    ///
    /// # 示例
    ///
    /// ```rust,ignore
    /// use yang_base::action::ApiResponse;
    /// use serde_json::json;
    ///
    /// // 返回单个对象
    /// let response = ApiResponse::success(
    ///     json!({ "id": 1, "name": "Alice" }),
    ///     "获取成功"
    /// )?;
    ///
    /// // 返回影响行数
    /// let response = ApiResponse::success(
    ///     json!({ "affected": 1 }),
    ///     "更新成功"
    /// )?;
    /// ```
    pub fn success<T: Serialize>(data: T, message: impl Into<String>) -> Result<Self, BaseError> {
        // 序列化数据,失败时返回结构化错误而非静默吞错
        let json_value = serde_json::to_value(data)
            .map_err(|e| BaseError::JsonSerializeFailed(e.to_string()))?;
        Ok(Self {
            code: 0,
            message: message.into(),
            data: Some(json_value),
            attachment: None,
        })
    }

    /// 创建成功响应(接受已序列化的 JSON 值)
    ///
    /// 当数据已经是 `serde_json::Value` 类型时,使用此方法可避免额外的序列化开销,
    /// 且不会失败,因此直接返回 `Self` 而非 `Result`。
    ///
    /// # 参数
    ///
    /// - `data`: 已序列化的 JSON 值
    /// - `message`: 成功消息
    ///
    /// # 返回
    ///
    /// - ApiResponse 实例
    ///
    /// # 示例
    ///
    /// ```rust,ignore
    /// use yang_base::action::ApiResponse;
    /// use serde_json::json;
    ///
    /// // 直接使用 serde_json::Value
    /// let data = json!({ "id": 1, "name": "Alice" });
    /// let response = ApiResponse::success_value(data, "获取成功");
    /// assert_eq!(response.code, 0);
    /// ```
    pub fn success_value(data: serde_json::Value, message: impl Into<String>) -> Self {
        Self {
            code: 0,
            message: message.into(),
            data: Some(data),
            attachment: None,
        }
    }

    /// 创建失败响应
    ///
    /// 状态码为非零错误码,不包含数据
    ///
    /// # 参数
    ///
    /// - `code`: 错误码(非零整数)
    /// - `message`: 错误消息
    ///
    /// # 返回
    ///
    /// - ApiResponse 实例
    ///
    /// # 示例
    ///
    /// ```rust,ignore
    /// use yang_base::action::ApiResponse;
    ///
    /// // 参数错误
    /// let response = ApiResponse::fail(400001, "参数缺失: username");
    ///
    /// // 权限错误
    /// let response = ApiResponse::fail(403001, "权限不足");
    ///
    /// // 业务错误
    /// let response = ApiResponse::fail(500001, "用户名已存在");
    /// ```
    pub fn fail(code: i32, message: impl Into<String>) -> Self {
        Self {
            code,
            message: message.into(),
            data: None,
            attachment: None,
        }
    }

    /// 创建携带附件的成功响应
    ///
    /// 由派发边界在识别到 Action 返回 [`ResponseBody`] 时调用。`data` 为 None,
    /// 附件信息仅对传输层可见(`serde(skip)`),JSON 线格式保持不变。
    ///
    /// # 参数
    ///
    /// - `attachment`: 传输层消费的附件描述
    /// - `message`: 成功消息
    ///
    /// # 返回
    ///
    /// - ApiResponse 实例
    pub fn attachment(attachment: ResponseAttachment, message: impl Into<String>) -> Self {
        Self {
            code: 0,
            message: message.into(),
            data: None,
            attachment: Some(attachment),
        }
    }

    /// 从 BaseError 创建失败响应
    ///
    /// 自动提取错误码和错误消息。接受引用以避免消费 `BaseError` 所有权,
    /// 调用方可在构建响应的同时保留 error 用于日志记录等后续用途。
    ///
    /// # 参数
    ///
    /// - `error`: BaseError 引用
    ///
    /// # 返回
    ///
    /// - ApiResponse 实例
    ///
    /// # 示例
    ///
    /// ```rust,ignore
    /// use yang_base::action::ApiResponse;
    /// use yang_base::error::BaseError;
    ///
    /// let error = BaseError::FieldRequired("username".to_string());
    /// let response = ApiResponse::from_error(&error);
    ///
    /// assert_ne!(response.code, 0);
    /// assert!(response.message.contains("username"));
    /// assert!(response.data.is_none());
    /// ```
    pub fn from_error(error: &BaseError) -> Self {
        Self::fail(error.code(), error.to_string())
    }
}

impl Default for ApiResponse {
    /// 创建默认响应
    ///
    /// 默认为成功响应,消息为 "OK",无数据
    fn default() -> Self {
        Self {
            code: 0,
            message: "OK".to_string(),
            data: None,
            attachment: None,
        }
    }
}

/// Action 的特殊业务响应体:文件下载、文件预览、重定向。
///
/// Action 将本类型作为 `type Output` 返回时,派发边界
/// ([`DynAction::dispatch`](crate::action::DynAction))会识别并把它转为
/// [`ApiResponse::attachment`],普通输出的 JSON 线格式不受影响。
///
/// # 示例
///
/// ```rust,ignore
/// use yang_base::action::ResponseBody;
///
/// async fn index(&self, ctx: ActionContext, input: Self::Input) -> Result<Self::Output, BaseError> {
///     // 文件下载
///     Ok(ResponseBody::download("/data/report.pdf", "report.pdf"))
///     // 文件预览
///     // Ok(ResponseBody::preview("/data/a.png"))
///     // 重定向
///     // Ok(ResponseBody::redirect("https://example.com/next"))
/// }
/// ```
#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum ResponseBody {
    /// 文件下载:传输层以 `Content-Disposition: attachment` 返回文件字节。
    Download {
        /// 服务器本地文件路径。
        path: PathBuf,
        /// 下载文件名(写入 Content-Disposition 的 filename 参数)。
        filename: String,
    },
    /// 文件预览:传输层以 `Content-Disposition: inline` 返回文件字节。
    Preview {
        /// 服务器本地文件路径。
        path: PathBuf,
    },
    /// 重定向:传输层返回 302 状态码与 `Location` 头。
    Redirect {
        /// 目标地址。
        url: String,
    },
}

impl ResponseBody {
    /// 构造文件下载响应。
    pub fn download(path: impl Into<PathBuf>, filename: impl Into<String>) -> Self {
        Self::Download {
            path: path.into(),
            filename: filename.into(),
        }
    }

    /// 构造文件预览响应。
    pub fn preview(path: impl Into<PathBuf>) -> Self {
        Self::Preview { path: path.into() }
    }

    /// 构造重定向响应。
    pub fn redirect(url: impl Into<String>) -> Self {
        Self::Redirect { url: url.into() }
    }
}

/// 一次响应携带的非 JSON 附件描述,由传输层消费,不参与 JSON 序列化。
///
/// 与 [`ResponseBody`] 结构一一对应;拆开是为了让 Action 输出类型满足
/// `Serialize + JsonSchema` 契约,而响应上的附件字段不受该契约约束。
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ResponseAttachment {
    /// 文件下载(`Content-Disposition: attachment`)。
    Download {
        /// 服务器本地文件路径。
        path: PathBuf,
        /// 下载文件名。
        filename: String,
    },
    /// 文件预览(`Content-Disposition: inline`)。
    Preview {
        /// 服务器本地文件路径。
        path: PathBuf,
    },
    /// 重定向(302 + `Location`)。
    Redirect {
        /// 目标地址。
        url: String,
    },
}

impl From<ResponseBody> for ResponseAttachment {
    fn from(body: ResponseBody) -> Self {
        match body {
            ResponseBody::Download { path, filename } => Self::Download { path, filename },
            ResponseBody::Preview { path } => Self::Preview { path },
            ResponseBody::Redirect { url } => Self::Redirect { url },
        }
    }
}

/// 派发边界的统一输出去向:[`ResponseBody`] 转为附件响应,其余照旧序列化进 `data`。
///
/// 通过 `&dyn Any` downcast 识别 `ResponseBody`,不引入新的 trait bound,
/// 因此现有 Action 的输出契约与 `Result<ApiResponse, BaseError>` 派发签名保持不变。
pub(crate) fn wrap_dispatch_output<T>(output: T, message: &str) -> Result<ApiResponse, BaseError>
where
    T: Serialize + 'static,
{
    let erased = &output as &dyn std::any::Any;
    if let Some(body) = erased.downcast_ref::<ResponseBody>() {
        return Ok(ApiResponse::attachment(body.clone().into(), message));
    }
    ApiResponse::success(output, message)
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    #[test]
    fn test_success_response() {
        // success 现在返回 Result,需要用 ? 或 unwrap
        let response =
            ApiResponse::success(json!({ "id": 123, "name": "Alice" }), "操作成功").unwrap();

        assert_eq!(response.code, 0);
        assert_eq!(response.message, "操作成功");
        assert!(response.data.is_some());

        let data = response.data.unwrap();
        assert_eq!(data["id"], 123);
        assert_eq!(data["name"], "Alice");
    }

    #[test]
    fn test_success_response_with_null() {
        // 使用 success_value 传入已有的 JSON 值(不会失败)
        let response = ApiResponse::success_value(serde_json::Value::Null, "操作成功");

        assert_eq!(response.code, 0);
        assert_eq!(response.message, "操作成功");
        assert!(response.data.is_some());
        assert_eq!(response.data.unwrap(), serde_json::Value::Null);
    }

    #[test]
    fn test_success_value_response() {
        // 测试 success_value 便捷构造器
        let data = json!({ "count": 42 });
        let response = ApiResponse::success_value(data, "查询成功");

        assert_eq!(response.code, 0);
        assert_eq!(response.message, "查询成功");
        assert!(response.data.is_some());
        assert_eq!(response.data.unwrap()["count"], 42);
    }

    #[test]
    fn test_success_serialize_error_propagation() {
        // 构造一个序列化时主动返回错误的类型
        struct AlwaysFailSerialize;

        impl serde::Serialize for AlwaysFailSerialize {
            fn serialize<S: serde::Serializer>(&self, _serializer: S) -> Result<S::Ok, S::Error> {
                use serde::ser::Error;
                Err(S::Error::custom("测试序列化失败"))
            }
        }

        let result = ApiResponse::success(AlwaysFailSerialize, "测试");
        // 序列化失败应该返回 JsonSerializeFailed 错误
        assert!(result.is_err(), "序列化失败应该返回错误");
        if let Err(BaseError::JsonSerializeFailed(_)) = result {
            // 正确:返回了 JsonSerializeFailed
        } else {
            panic!("期望 JsonSerializeFailed 错误");
        }
    }

    #[test]
    fn test_fail_response() {
        let response = ApiResponse::fail(400001, "参数错误");

        assert_eq!(response.code, 400001);
        assert_eq!(response.message, "参数错误");
        assert!(response.data.is_none());
    }

    #[test]
    fn test_from_error() {
        let error = BaseError::FieldRequired("username".to_string());
        let response = ApiResponse::from_error(&error);

        assert_ne!(response.code, 0);
        assert!(response.message.contains("username"));
        assert!(response.data.is_none());
        // error 仍可用(未被消费)
        assert_eq!(error.code(), 600006);
    }

    #[test]
    fn test_default_response() {
        let response = ApiResponse::default();

        assert_eq!(response.code, 0);
        assert_eq!(response.message, "OK");
        assert!(response.data.is_none());
    }

    #[test]
    fn test_serialize_response() {
        // success 现在返回 Result
        let response = ApiResponse::success(json!({ "count": 10 }), "查询成功").unwrap();

        let json = serde_json::to_string(&response).unwrap();
        assert!(json.contains("\"code\":0"));
        assert!(json.contains("\"message\":\"查询成功\""));
        assert!(json.contains("\"data\""));
    }

    #[test]
    fn test_serialize_fail_response() {
        let response = ApiResponse::fail(500001, "服务器错误");

        let json = serde_json::to_string(&response).unwrap();
        assert!(json.contains("\"code\":500001"));
        assert!(json.contains("\"message\":\"服务器错误\""));
        // data 字段应该被跳过
        assert!(!json.contains("\"data\""));
    }

    #[test]
    fn test_response_body_constructors() {
        // download 携带路径与下载文件名
        let download = ResponseBody::download("/tmp/report.pdf", "report.pdf");
        assert_eq!(
            download,
            ResponseBody::Download {
                path: std::path::PathBuf::from("/tmp/report.pdf"),
                filename: "report.pdf".to_string(),
            }
        );
        // preview 仅携带路径
        assert_eq!(
            ResponseBody::preview("/tmp/a.png"),
            ResponseBody::Preview {
                path: std::path::PathBuf::from("/tmp/a.png"),
            }
        );
        // redirect 仅携带目标地址
        assert_eq!(
            ResponseBody::redirect("https://example.com/next"),
            ResponseBody::Redirect {
                url: "https://example.com/next".to_string(),
            }
        );
    }

    #[test]
    fn test_response_body_into_attachment() {
        // ResponseBody 到传输层附件描述的一一映射
        let attachment: ResponseAttachment = ResponseBody::download("/tmp/a.bin", "a.bin").into();
        assert_eq!(
            attachment,
            ResponseAttachment::Download {
                path: std::path::PathBuf::from("/tmp/a.bin"),
                filename: "a.bin".to_string(),
            }
        );
        let attachment: ResponseAttachment = ResponseBody::preview("/tmp/b.png").into();
        assert_eq!(
            attachment,
            ResponseAttachment::Preview {
                path: std::path::PathBuf::from("/tmp/b.png"),
            }
        );
        let attachment: ResponseAttachment = ResponseBody::redirect("/home").into();
        assert_eq!(
            attachment,
            ResponseAttachment::Redirect {
                url: "/home".to_string(),
            }
        );
    }

    #[test]
    fn test_response_body_implements_action_output_contract() {
        // 作为 Action::Output 必须满足 Serialize + JsonSchema + 'static
        fn assert_output_contract<T: Serialize + schemars::JsonSchema + 'static>() {}
        assert_output_contract::<ResponseBody>();

        let body = ResponseBody::redirect("/next");
        let json = serde_json::to_string(&body).unwrap();
        assert!(json.contains("/next"), "ResponseBody 应可序列化: {json}");
        // schema 生成不应 panic(output_schema 由构建期消费)
        let _schema = schemars::schema_for!(ResponseBody);
    }

    #[test]
    fn test_attachment_response_json_wire_format_unchanged() {
        // 附件响应序列化后不得出现 attachment 字段,保持 JSON 线格式不变
        let response = ApiResponse::attachment(
            ResponseAttachment::Redirect {
                url: "/next".to_string(),
            },
            "成功",
        );
        assert_eq!(response.code, 0);
        assert!(response.data.is_none());
        let json = serde_json::to_string(&response).unwrap();
        assert!(
            !json.contains("attachment"),
            "线格式不得包含附件信息: {json}"
        );
        assert!(json.contains("\"code\":0"));
    }

    #[test]
    fn test_plain_response_has_no_attachment() {
        // 普通成功/失败响应不带附件
        let ok = ApiResponse::success(json!({"a": 1}), "ok").unwrap();
        assert!(ok.attachment.is_none());
        let fail = ApiResponse::fail(1, "bad");
        assert!(fail.attachment.is_none());
        let default = ApiResponse::default();
        assert!(default.attachment.is_none());
    }

    #[test]
    fn test_wrap_dispatch_output_recognizes_response_body() {
        // ResponseBody 输出转为附件响应,不进 data
        let response = wrap_dispatch_output(ResponseBody::redirect("/next"), "成功").unwrap();
        assert_eq!(response.code, 0);
        assert!(response.data.is_none());
        assert_eq!(
            response.attachment,
            Some(ResponseAttachment::Redirect {
                url: "/next".to_string(),
            })
        );
    }

    #[test]
    fn test_wrap_dispatch_output_passes_through_plain_output() {
        // 普通输出照旧序列化进 data,attachment 为 None
        #[derive(Serialize)]
        struct Plain {
            value: i32,
        }
        let response = wrap_dispatch_output(Plain { value: 7 }, "成功").unwrap();
        assert_eq!(response.code, 0);
        assert_eq!(response.data.unwrap()["value"], 7);
        assert!(response.attachment.is_none());
    }
}