Section 10 of 25
The variant explosion
Contents
A family of functions such as start_with_budget, start_with_slots, start_with_snapshot_hook, start_with_budget_and_slots is a signal that a request struct is missing. Replace the family with one entry function and one options struct:
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!()
}
Callers write StartRequest { budget: Some(limit), ..StartRequest::new(workflow) }. The fields a request cannot do without are the arguments of new, so a request without a workflow does not compile; a Default would make one. A new option is one new field, not a new function and a new line in every caller. Test-only injection points (faults, hooks) are fields that exist only in a test build. cfg(test) alone covers only the crate's own unit tests, so integration tests and other crates switch the fields on with a test-hooks feature, the same idea as the fake driver in 14.2. Switch it on the same way in every test command (section 22.3); otherwise every switch between a build with the feature and one without it compiles the crate and everything above it again.