yang-base 0.1.0

个人使用构建后端服务器
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
//! Action Trait 定义
//!
//! 定义所有 action 必须实现的统一接口。
//!
//! # 主要组件
//!
//! - `Action`:Action trait,定义 action 的基本行为和元数据
//! - `Permission`:权限类型,表示 action 所需的权限
//!
//! # 示例
//!
//! ```rust,ignore
//! use yang_base::action::{Action, ActionContext, ApiResponse, Permission};
//! use yang_base::error::BaseError;
//! use async_trait::async_trait;
//!
//! // 定义自定义 action
//! pub struct MyAction;
//!
//! #[async_trait]
//! impl Action for MyAction {
//!     async fn execute(&self, context: ActionContext) -> Result<ApiResponse, BaseError> {
//!         // 获取参数
//!         let name: String = context.param("name")?;
//!         
//!         // 执行业务逻辑
//!         // ...
//!         
//!         Ok(ApiResponse::success(
//!             serde_json::json!({ "message": format!("Hello, {}!", name) }),
//!             "操作成功"
//!         ))
//!     }
//!     
//!     fn name(&self) -> &str {
//!         "my_action"
//!     }
//!     
//!     fn display_name(&self) -> &str {
//!         "我的操作"
//!     }
//!     
//!     fn description(&self) -> &str {
//!         "这是一个自定义操作示例"
//!     }
//!     
//!     fn permissions(&self) -> &[Permission] {
//!         &[Permission::new("my_action:execute")]
//!     }
//! }
//! ```

use crate::error::BaseError;
use async_trait::async_trait;

use super::{ActionContext, ApiResponse};

/// 权限类型
///
/// 表示 action 所需的权限,用于权限检查。
///
/// # 字段
///
/// - `name`: 权限名称(如 "user:create", "order:read")
///
/// # 示例
///
/// ```rust,ignore
/// use yang_base::action::Permission;
///
/// // 创建权限
/// let permission = Permission::new("user:create");
/// assert_eq!(permission.name(), "user:create");
///
/// // 检查权限名称
/// if permission.name() == "user:create" {
///     println!("这是创建用户的权限");
/// }
/// ```
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Permission {
    /// 权限名称
    name: String,
}

impl Permission {
    /// 创建新权限
    ///
    /// # 参数
    ///
    /// - `name`: 权限名称
    ///
    /// # 返回
    ///
    /// - 新的 Permission 实例
    ///
    /// # 示例
    ///
    /// ```rust,ignore
    /// use yang_base::action::Permission;
    ///
    /// let permission = Permission::new("user:create");
    /// ```
    pub fn new(name: impl Into<String>) -> Self {
        Self { name: name.into() }
    }

    /// 获取权限名称
    ///
    /// # 返回
    ///
    /// - 权限名称字符串引用
    ///
    /// # 示例
    ///
    /// ```rust,ignore
    /// use yang_base::action::Permission;
    ///
    /// let permission = Permission::new("user:create");
    /// assert_eq!(permission.name(), "user:create");
    /// ```
    pub fn name(&self) -> &str {
        &self.name
    }
}

/// Action 接口
///
/// 所有 action 必须实现此 trait,定义 action 的执行逻辑和元数据。
///
/// # 必须实现的方法
///
/// - `execute`: 执行 action 的业务逻辑(异步方法)
/// - `name`: 返回 action 名称
///
/// # 可选实现的方法(提供默认实现)
///
/// - `display_name`: 返回 action 显示名称(默认返回 name)
/// - `description`: 返回 action 描述(默认返回空字符串)
/// - `permissions`: 返回 action 所需权限列表(默认返回空列表)
/// - `params_schema`: 返回参数结构 Schema(默认返回 None)
/// - `is_public`: 标记是否为公开 action(默认返回 false)
///
/// # 示例
///
/// ```rust,ignore
/// use yang_base::action::{Action, ActionContext, ApiResponse, Permission};
/// use yang_base::error::BaseError;
/// use async_trait::async_trait;
/// use serde_json::json;
///
/// // 定义登录 action
/// pub struct LoginAction;
///
/// #[async_trait]
/// impl Action for LoginAction {
///     async fn execute(&self, context: ActionContext) -> Result<ApiResponse, BaseError> {
///         // 获取参数
///         let username: String = context.param("username")?;
///         let password: String = context.param("password")?;
///         
///         // 验证用户
///         // ...
///         
///         Ok(ApiResponse::success(
///             json!({ "token": "xxx", "user": { "id": 1, "username": username } }),
///             "登录成功"
///         ))
///     }
///     
///     fn name(&self) -> &str {
///         "login"
///     }
///     
///     fn display_name(&self) -> &str {
///         "用户登录"
///     }
///     
///     fn description(&self) -> &str {
///         "用户通过用户名和密码登录系统"
///     }
///     
///     fn is_public(&self) -> bool {
///         true // 登录不需要认证
///     }
///     
///     fn params_schema(&self) -> Option<serde_json::Value> {
///         Some(json!({
///             "type": "object",
///             "properties": {
///                 "username": { "type": "string", "description": "用户名" },
///                 "password": { "type": "string", "description": "密码" }
///             },
///             "required": ["username", "password"]
///         }))
///     }
/// }
/// ```
#[async_trait]
pub trait Action: Send + Sync {
    /// 执行 action
    ///
    /// 这是 action 的核心方法,包含具体的业务逻辑实现。
    ///
    /// # 参数
    ///
    /// - `context`: Action 执行上下文,包含请求信息、用户信息和全局工具
    ///
    /// # 返回
    ///
    /// - `Ok(ApiResponse)`: 执行成功,返回 API 响应
    /// - `Err(BaseError)`: 执行失败,返回错误信息
    ///
    /// # 示例
    ///
    /// ```rust,ignore
    /// use yang_base::action::{Action, ActionContext, ApiResponse};
    /// use yang_base::error::BaseError;
    /// use async_trait::async_trait;
    /// use serde_json::json;
    ///
    /// pub struct GetUserAction;
    ///
    /// #[async_trait]
    /// impl Action for GetUserAction {
    ///     async fn execute(&self, context: ActionContext) -> Result<ApiResponse, BaseError> {
    ///         // 获取用户 ID
    ///         let user_id: i64 = context.param("id")?;
    ///         
    ///         // 查询用户
    ///         let user = query_user_by_id(user_id).await?;
    ///         
    ///         Ok(ApiResponse::success(user, "获取成功"))
    ///     }
    ///     
    ///     fn name(&self) -> &str {
    ///         "get_user"
    ///     }
    /// }
    /// ```
    async fn execute(&self, context: ActionContext) -> Result<ApiResponse, BaseError>;

    /// 获取 action 名称
    ///
    /// 返回 action 的唯一标识符,用于路由匹配。
    ///
    /// # 返回
    ///
    /// - action 名称字符串引用
    ///
    /// # 示例
    ///
    /// ```rust,ignore
    /// use yang_base::action::Action;
    ///
    /// pub struct AddAction;
    ///
    /// impl Action for AddAction {
    ///     fn name(&self) -> &str {
    ///         "add"
    ///     }
    ///     
    ///     // ... 其他方法
    /// }
    /// ```
    fn name(&self) -> &str;

    /// 获取 action 显示名称
    ///
    /// 返回 action 的友好显示名称,用于 UI 展示。
    /// 默认实现返回 `name()` 的值。
    ///
    /// # 返回
    ///
    /// - action 显示名称字符串引用
    ///
    /// # 示例
    ///
    /// ```rust,ignore
    /// use yang_base::action::Action;
    ///
    /// pub struct AddAction;
    ///
    /// impl Action for AddAction {
    ///     fn name(&self) -> &str {
    ///         "add"
    ///     }
    ///     
    ///     fn display_name(&self) -> &str {
    ///         "新增记录"
    ///     }
    ///     
    ///     // ... 其他方法
    /// }
    /// ```
    fn display_name(&self) -> &str {
        self.name()
    }

    /// 获取 action 描述
    ///
    /// 返回 action 的详细描述信息,用于文档生成和 UI 提示。
    /// 默认实现返回空字符串。
    ///
    /// # 返回
    ///
    /// - action 描述字符串引用
    ///
    /// # 示例
    ///
    /// ```rust,ignore
    /// use yang_base::action::Action;
    ///
    /// pub struct AddAction;
    ///
    /// impl Action for AddAction {
    ///     fn name(&self) -> &str {
    ///         "add"
    ///     }
    ///     
    ///     fn description(&self) -> &str {
    ///         "向数据表中添加一条新记录"
    ///     }
    ///     
    ///     // ... 其他方法
    /// }
    /// ```
    fn description(&self) -> &str {
        ""
    }

    /// 获取权限要求
    ///
    /// 返回执行此 action 所需的权限列表。
    /// 默认实现返回空列表(不需要任何权限)。
    ///
    /// # 返回
    ///
    /// - 权限列表切片引用
    ///
    /// # 示例
    ///
    /// ```rust,ignore
    /// use yang_base::action::{Action, Permission};
    ///
    /// pub struct DeleteUserAction;
    ///
    /// impl Action for DeleteUserAction {
    ///     fn name(&self) -> &str {
    ///         "delete_user"
    ///     }
    ///     
    ///     fn permissions(&self) -> &[Permission] {
    ///         &[
    ///             Permission::new("user:delete"),
    ///             Permission::new("admin:access")
    ///         ]
    ///     }
    ///     
    ///     // ... 其他方法
    /// }
    /// ```
    fn permissions(&self) -> &[Permission] {
        &[]
    }

    /// 获取参数 Schema
    ///
    /// 返回 action 参数的 JSON Schema 定义,用于参数验证和文档生成。
    /// 默认实现返回 None(不提供 Schema)。
    ///
    /// # 返回
    ///
    /// - `Some(Value)`: 参数 Schema(JSON 格式)
    /// - `None`: 不提供 Schema
    ///
    /// # 示例
    ///
    /// ```rust,ignore
    /// use yang_base::action::Action;
    /// use serde_json::json;
    ///
    /// pub struct CreateUserAction;
    ///
    /// impl Action for CreateUserAction {
    ///     fn name(&self) -> &str {
    ///         "create_user"
    ///     }
    ///     
    ///     fn params_schema(&self) -> Option<serde_json::Value> {
    ///         Some(json!({
    ///             "type": "object",
    ///             "properties": {
    ///                 "username": {
    ///                     "type": "string",
    ///                     "minLength": 3,
    ///                     "maxLength": 20,
    ///                     "description": "用户名"
    ///                 },
    ///                 "email": {
    ///                     "type": "string",
    ///                     "format": "email",
    ///                     "description": "邮箱地址"
    ///                 },
    ///                 "age": {
    ///                     "type": "integer",
    ///                     "minimum": 0,
    ///                     "maximum": 150,
    ///                     "description": "年龄"
    ///                 }
    ///             },
    ///             "required": ["username", "email"]
    ///         }))
    ///     }
    ///     
    ///     // ... 其他方法
    /// }
    /// ```
    fn params_schema(&self) -> Option<serde_json::Value> {
        None
    }

    /// 是否为公开 action
    ///
    /// 标记此 action 是否为公开访问(不需要认证)。
    /// 默认实现返回 false(需要认证)。
    ///
    /// # 返回
    ///
    /// - `true`: 公开 action,不需要认证
    /// - `false`: 需要认证
    ///
    /// # 示例
    ///
    /// ```rust,ignore
    /// use yang_base::action::Action;
    ///
    /// pub struct LoginAction;
    ///
    /// impl Action for LoginAction {
    ///     fn name(&self) -> &str {
    ///         "login"
    ///     }
    ///     
    ///     fn is_public(&self) -> bool {
    ///         true // 登录不需要认证
    ///     }
    ///     
    ///     // ... 其他方法
    /// }
    ///
    /// pub struct GetProfileAction;
    ///
    /// impl Action for GetProfileAction {
    ///     fn name(&self) -> &str {
    ///         "get_profile"
    ///     }
    ///     
    ///     fn is_public(&self) -> bool {
    ///         false // 获取个人信息需要认证(默认值)
    ///     }
    ///     
    ///     // ... 其他方法
    /// }
    /// ```
    fn is_public(&self) -> bool {
        false
    }
}