重构: df-core改名df-types(类型库语义准+全workspace机械改名54处)
This commit is contained in:
@@ -44,10 +44,10 @@ pub async fn advance_task_atomic(
|
||||
repo: &TaskRepo,
|
||||
id: &str,
|
||||
target_status: &str,
|
||||
) -> df_core::error::Result<TaskRecord> {
|
||||
) -> df_types::error::Result<TaskRecord> {
|
||||
// 1. target 合法性(7 态之一)。先于读库校验:即便任务不存在,也先拒绝非法状态值。
|
||||
if !is_valid_state(target_status) {
|
||||
return Err(df_core::error::Error::Validation(format!(
|
||||
return Err(df_types::error::Error::Validation(format!(
|
||||
"非法 target_status {:?},合法值: {}",
|
||||
target_status,
|
||||
ALL_STATES.join("/")
|
||||
@@ -58,19 +58,19 @@ pub async fn advance_task_atomic(
|
||||
let current = repo
|
||||
.get_by_id(id)
|
||||
.await?
|
||||
.ok_or_else(|| df_core::error::Error::NotFound(format!("任务 {} 不存在", id)))?;
|
||||
.ok_or_else(|| df_types::error::Error::NotFound(format!("任务 {} 不存在", id)))?;
|
||||
|
||||
// 3. 状态机校验(三类拒绝,错误类型区分供前端分辨):
|
||||
// - 同态(from==to):Validation「相同状态,无需推进」(空操作,非状态机违例)
|
||||
// - 非法转换(跳态/终态无后继等):InvalidState「非法状态转换 X→Y」
|
||||
let from = current.status.as_str();
|
||||
if from == target_status {
|
||||
return Err(df_core::error::Error::Validation(format!(
|
||||
return Err(df_types::error::Error::Validation(format!(
|
||||
"相同状态 {from:?},无需推进"
|
||||
)));
|
||||
}
|
||||
if !can_transition(from, target_status) {
|
||||
return Err(df_core::error::Error::InvalidState {
|
||||
return Err(df_types::error::Error::InvalidState {
|
||||
current: format!("{from}→{target_status}(非法状态转换)"),
|
||||
expected: target_status.to_string(),
|
||||
});
|
||||
@@ -83,7 +83,7 @@ pub async fn advance_task_atomic(
|
||||
.await?;
|
||||
|
||||
// 5. CAS 失败:并发已改动 status(或任务被删),拒绝静默成功。
|
||||
updated.ok_or_else(|| df_core::error::Error::InvalidState {
|
||||
updated.ok_or_else(|| df_types::error::Error::InvalidState {
|
||||
current: "已变更(并发推进或旁路修改)".to_string(),
|
||||
expected: from.to_string(),
|
||||
})
|
||||
@@ -268,7 +268,7 @@ mod tests {
|
||||
repo.insert(rec("t1", "todo")).await.unwrap();
|
||||
let err = advance_task_atomic(&repo, "t1", "done").await.unwrap_err();
|
||||
match err {
|
||||
df_core::error::Error::InvalidState { current, expected } => {
|
||||
df_types::error::Error::InvalidState { current, expected } => {
|
||||
assert!(current.contains("todo→done"), "非法转换消息应含 from→to,实际: {current}");
|
||||
assert!(current.contains("非法状态转换"), "消息应含「非法状态转换」,实际: {current}");
|
||||
assert_eq!(expected, "done");
|
||||
@@ -284,7 +284,7 @@ mod tests {
|
||||
repo.insert(rec("t1", "done")).await.unwrap();
|
||||
let err = advance_task_atomic(&repo, "t1", "todo").await.unwrap_err();
|
||||
match err {
|
||||
df_core::error::Error::InvalidState { current, .. } => {
|
||||
df_types::error::Error::InvalidState { current, .. } => {
|
||||
assert!(current.contains("done→todo"), "终态后继消息应含 from→to,实际: {current}");
|
||||
}
|
||||
other => panic!("终态后继应是 InvalidState,实际: {other:?}"),
|
||||
@@ -300,7 +300,7 @@ mod tests {
|
||||
repo.insert(rec("t1", "in_progress")).await.unwrap();
|
||||
let err = advance_task_atomic(&repo, "t1", "in_progress").await.unwrap_err();
|
||||
match err {
|
||||
df_core::error::Error::Validation(msg) => {
|
||||
df_types::error::Error::Validation(msg) => {
|
||||
assert!(msg.contains("相同状态"), "同态消息应含「相同状态」,实际: {msg}");
|
||||
}
|
||||
other => panic!("同态拒绝应是 Validation,实际: {other:?}"),
|
||||
@@ -312,14 +312,14 @@ mod tests {
|
||||
let repo = setup().await;
|
||||
repo.insert(rec("t1", "todo")).await.unwrap();
|
||||
let err = advance_task_atomic(&repo, "t1", "merged").await.unwrap_err();
|
||||
assert!(matches!(err, df_core::error::Error::Validation(_)));
|
||||
assert!(matches!(err, df_types::error::Error::Validation(_)));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn not_found_rejected() {
|
||||
let repo = setup().await;
|
||||
let err = advance_task_atomic(&repo, "nope", "in_progress").await.unwrap_err();
|
||||
assert!(matches!(err, df_core::error::Error::NotFound(_)));
|
||||
assert!(matches!(err, df_types::error::Error::NotFound(_)));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
|
||||
Reference in New Issue
Block a user