charging-cabinet/tasks/018-code-optimizations.md
2026-07-02 05:38:01 +08:00

64 lines
1.6 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 任务018代码优化
## 目标
完成审查提出的3个改进建议。
## 优化清单
### 1. KEYS → SCAN
**位置:** `api-server/src/routes/nodes.rs``api-server/src/commands.rs`
**当前:**
```rust
let keys = redis.keys("node:*").await?;
```
**改进:**
```rust
async fn scan_nodes(redis: &redis::aio::ConnectionManager) -> Result<Vec<String>> {
let mut nodes = Vec::new();
let mut cursor = 0i64;
loop {
let (new_cursor, keys): (i64, Vec<String>) = redis
.scan(cursor)
.await?;
nodes.extend(keys);
if new_cursor == 0 { break; }
cursor = new_cursor;
}
Ok(nodes)
}
```
### 2. JWT常量去重
**位置:** `api-server/src/middleware/auth.rs``api-server/src/config.rs`
**当前:** `JWT_SECRET_DEFAULT` 在两个文件重复定义
**改进:** 只在 `config.rs` 定义,`auth.rs``Config` 读取
### 3. rand API更新
**位置:** `device-server/src/main.rs``api-server/src/routes/organizations.rs`
**当前:** 可能用了旧版 `rand::thread_rng()``gen_range`
**改进:** 确认使用 rand 0.8+ API
```rust
use rand::Rng;
let mut rng = rand::thread_rng();
let value = rng.gen_range(0..CHARSET.len());
```
## 质量约束
1. 完成代码后执行 `cargo check` + `cargo clippy`,零报错零警告
2. 分层拆分单函数≤80行命名语义化完整注释
3. 所有外部IO/网络请求异常捕获禁止裸panic
4. 分支逻辑全覆盖,不遗漏兜底分支
5. 常量抽离不使用废弃API
6. Rust内存安全合理管理所有权禁用unsafe无合理理由