pub fn transition(
doc: &mut Document,
workflow: &WorkflowDefinition,
target_phase_id: &str,
) -> Result<(), WorkflowError>Expand description
Attempts to transition a document from its current phase to a specified target phase according to the rules defined in the provided workflow.
If the transition is valid, the document.current_phase field is updated in place
to the target_phase_id.
This function performs several checks:
- Verifies that the document’s
workflow_idmatches the providedworkflow.id(). - Ensures the
target_phase_idactually exists within theworkflowdefinition. - Handles initial transitions for new documents (those with an empty
current_phase), only allowing them to transition to the workflow’s designated “Start” phase. - Validates that a direct transition path exists from the document’s
current_phaseto thetarget_phase_idwithin theworkflowgraph.
§Arguments
doc- A mutable reference to theDocumentto be transitioned.workflow- TheWorkflowDefinitionthat defines the valid phases and transitions for this document.target_phase_id- Theidof the phase to which the document should transition.
§Returns
Ok(())if the transition was successful and the document’s phase was updated.Err(WorkflowError)if any of the validation checks fail (e.g., workflow mismatch, unknown phase, invalid transition, or no current phase on a non-new document).