优化: R-PD-10 commands err_str helper 统一错误格式化(87 处)

mod.rs 加 pub fn err_str<E: ToString>,commands/ 10 文件 87 处 .map_err(|e| e.to_string()) → .map_err(err_str),残留 0(10 处复杂表达式 e 用于 format!/anyhow 保留)。行为零变化,统一入口为未来加日志/分类留点。批5,cargo workspace 0
This commit is contained in:
2026-06-15 05:45:04 +08:00
parent fddca9daf1
commit 892a642bd4
11 changed files with 105 additions and 94 deletions

View File

@@ -12,7 +12,7 @@ use df_storage::models::ProjectRecord;
use crate::state::AppState;
use super::now_millis;
use super::{err_str, now_millis};
/// 创建项目入参
#[derive(Debug, Deserialize)]
@@ -32,7 +32,7 @@ pub struct CreateProjectInput {
/// 列出未删除项目(过滤回收站)
#[tauri::command]
pub async fn list_projects(state: State<'_, AppState>) -> Result<Vec<ProjectRecord>, String> {
state.projects.list_active().await.map_err(|e| e.to_string())
state.projects.list_active().await.map_err(err_str)
}
/// 创建项目,返回完整记录
@@ -59,9 +59,9 @@ pub async fn create_project(
let root = std::path::PathBuf::from(p);
let detected = tokio::task::spawn_blocking(move || detect_stack(&root))
.await
.map_err(|e| e.to_string())?
.map_err(|e| e.to_string())?;
serde_json::to_string(&detected).map_err(|e| e.to_string())?
.map_err(err_str)?
.map_err(err_str)?;
serde_json::to_string(&detected).map_err(err_str)?
}
};
(Some(p.to_string()), Some(stack_json))
@@ -85,7 +85,7 @@ pub async fn create_project(
.projects
.insert(record.clone())
.await
.map_err(|e| e.to_string())?;
.map_err(err_str)?;
Ok(record)
}
@@ -153,14 +153,14 @@ pub async fn import_project(
let stack_json = match want_stack.as_deref().map(str::trim).filter(|s| !s.is_empty()) {
Some(s) => s.to_string(),
None => {
let detected = detect_stack(&root).map_err(|e| e.to_string())?;
serde_json::to_string(&detected).map_err(|e| e.to_string())?
let detected = detect_stack(&root).map_err(err_str)?;
serde_json::to_string(&detected).map_err(err_str)?
}
};
Ok((name, description, stack_json))
})
.await
.map_err(|e| e.to_string())??;
.map_err(err_str)??;
let now = now_millis();
let record = ProjectRecord {
@@ -178,7 +178,7 @@ pub async fn import_project(
.projects
.insert(record.clone())
.await
.map_err(|e| e.to_string())?;
.map_err(err_str)?;
Ok(record)
}
@@ -192,7 +192,7 @@ pub async fn get_project(
.projects
.get_by_id(&id)
.await
.map_err(|e| e.to_string())
.map_err(err_str)
}
/// 更新项目单个字段(字段名走 df-storage 白名单校验)
@@ -207,13 +207,13 @@ pub async fn update_project(
.projects
.update_field(&id, &field, &value)
.await
.map_err(|e| e.to_string())
.map_err(err_str)
}
/// 删除项目(软删 → 回收站,可恢复)
#[tauri::command]
pub async fn delete_project(state: State<'_, AppState>, id: String) -> Result<bool, String> {
state.projects.soft_delete(&id).await.map_err(|e| e.to_string())
state.projects.soft_delete(&id).await.map_err(err_str)
}
/// 列出回收站项目(deleted_at IS NOT NULL)
@@ -221,13 +221,13 @@ pub async fn delete_project(state: State<'_, AppState>, id: String) -> Result<bo
pub async fn list_deleted_projects(
state: State<'_, AppState>,
) -> Result<Vec<ProjectRecord>, String> {
state.projects.list_deleted().await.map_err(|e| e.to_string())
state.projects.list_deleted().await.map_err(err_str)
}
/// 恢复项目(从回收站还原,清 deleted_at)
#[tauri::command]
pub async fn restore_project(state: State<'_, AppState>, id: String) -> Result<bool, String> {
state.projects.restore(&id).await.map_err(|e| e.to_string())
state.projects.restore(&id).await.map_err(err_str)
}
/// 彻底删除项目(级联物理删 branches/releases/tasks,不可恢复)
@@ -237,7 +237,7 @@ pub async fn purge_project(state: State<'_, AppState>, id: String) -> Result<boo
.projects
.purge_with_descendants(&id)
.await
.map_err(|e| e.to_string())
.map_err(err_str)
}
// ============================================================
@@ -258,16 +258,16 @@ async fn find_binding_conflict(
.projects
.find_path_conflict(&norm, exclude_id)
.await
.map_err(|e| e.to_string())
.map_err(err_str)
}
/// 探测目录技术栈(前端选目录后实时预览)
#[tauri::command]
pub async fn scan_project_stack(path: String) -> Result<Vec<String>, String> {
let root = std::path::PathBuf::from(&path);
tokio::task::spawn_blocking(move || detect_stack(&root).map_err(|e| e.to_string()))
tokio::task::spawn_blocking(move || detect_stack(&root).map_err(err_str))
.await
.map_err(|e| e.to_string())?
.map_err(err_str)?
}
/// 检查目录是否已被其他项目绑定(防重复绑定)。返回占用项目(若有)。
@@ -298,24 +298,24 @@ pub async fn relocate_project_path(
let root = std::path::PathBuf::from(&new_path);
let stack = tokio::task::spawn_blocking(move || detect_stack(&root))
.await
.map_err(|e| e.to_string())?
.map_err(|e| e.to_string())?;
let stack_json = serde_json::to_string(&stack).map_err(|e| e.to_string())?;
.map_err(err_str)?
.map_err(err_str)?;
let stack_json = serde_json::to_string(&stack).map_err(err_str)?;
state
.projects
.update_field(&id, "path", &new_path)
.await
.map_err(|e| e.to_string())?;
.map_err(err_str)?;
state
.projects
.update_field(&id, "stack", &stack_json)
.await
.map_err(|e| e.to_string())?;
.map_err(err_str)?;
state
.projects
.get_by_id(&id)
.await
.map_err(|e| e.to_string())?
.map_err(err_str)?
.ok_or_else(|| "项目不存在".to_string())
}
@@ -364,11 +364,11 @@ pub async fn scan_project_with_ai(
Ok::<_, anyhow::Error>((stack, sample))
})
.await
.map_err(|e| e.to_string())?
.map_err(|e| e.to_string())?;
.map_err(err_str)?
.map_err(err_str)?;
// 2. 取默认 provider(优先 is_default,否则首个)
let providers = state.ai_providers.list_all().await.map_err(|e| e.to_string())?;
let providers = state.ai_providers.list_all().await.map_err(err_str)?;
let pc = providers
.iter()
.find(|p| p.is_default)