第 10 节,共 25 节
变体爆炸
目录
像 start_with_budget、start_with_slots、start_with_snapshot_hook、start_with_budget_and_slots 这样的一族函数,说明缺少一个请求结构体。用一个入口函数和一个选项结构体替换这一族函数:
pub struct StartRequest {
pub workflow: WorkflowRef,
pub task: Option<String>,
pub at_once: Option<NonZeroUsize>,
pub budget: Option<Money>,
pub origin: Option<Origin>,
#[cfg(any(test, feature = "test-hooks"))]
pub faults: Faults,
}
impl StartRequest {
pub fn new(workflow: WorkflowRef) -> Self {
Self {
workflow,
task: None,
at_once: None,
budget: None,
origin: None,
#[cfg(any(test, feature = "test-hooks"))]
faults: Faults::default(),
}
}
}
pub async fn start(deps: &Deps, request: StartRequest) -> Result<Outcome<RunId>, StartError> {
todo!()
}
调用方写成 StartRequest { budget: Some(limit), ..StartRequest::new(workflow) }。请求不可缺少的字段是 new 的参数,因此没有工作流的请求无法编译;而 Default 会造出这样的请求。新增一个选项只是新增一个字段,而不是新增一个函数并修改每个调用方。仅用于测试的注入点(faults、hooks)是只在测试构建中存在的字段。单靠 cfg(test) 只覆盖该 crate 自己的单元测试,因此集成测试和其他 crate 通过 test-hooks feature 启用这些字段,思路与 14.2 中的伪驱动相同。在每个测试命令中都以同样的方式启用它(第 22.3 节);否则,每次在启用该 feature 的构建和未启用的构建之间切换,都会重新编译这个 crate 以及它之上的一切。