Skip to main content

Workflow

Struct Workflow 

Source
pub struct Workflow { /* private fields */ }
Expand description

Directed acyclic graph (DAG) representing a workflow.

The workflow maintains tasks as nodes in a petgraph DiGraph, with edges representing hard dependencies between tasks. The graph is validated for cycles on every dependency addition.

§Example

let mut workflow = Workflow::new();
workflow.add_task(MockTask::new("a", "Task A"));
workflow.add_task(MockTask::new("b", "Task B").depends_on("a"));
workflow.add_dependency("b", "a")?;
let order = workflow.execution_order()?;

Implementations§

Source§

impl Workflow

Source

pub fn new() -> Self

Creates a new empty workflow.

Source

pub fn add_task(&mut self, task: Box<dyn WorkflowTask>) -> NodeIndex

Adds a task to the workflow.

The task is added as an isolated node. Dependencies must be added separately using add_dependency.

§Arguments
  • task - Boxed trait object implementing WorkflowTask
§Returns

The NodeIndex of the newly added task in the graph.

§Example
let mut workflow = Workflow::new();
let task = Box::new(MockTask::new("task-1", "First Task"));
workflow.add_task(task);
Source

pub fn add_dependency( &mut self, from_task: impl Into<TaskId>, to_task: impl Into<TaskId>, ) -> Result<(), WorkflowError>

Adds a dependency edge between two tasks.

Creates a directed edge from from_task to to_task, indicating that to_task depends on from_task (from_task must execute first).

§Arguments
  • from_task - Task ID of the prerequisite (executes first)
  • to_task - Task ID of the dependent (executes after)
§Returns
  • Ok(()) if dependency added successfully
  • Err(WorkflowError::CycleDetected) if edge creates a cycle
  • Err(WorkflowError::TaskNotFound) if either task doesn’t exist
§Example
workflow.add_dependency("task-a", "task-b")?;
// task-a must execute before task-b
Source

pub fn execution_order(&self) -> Result<Vec<TaskId>, WorkflowError>

Returns tasks in topological execution order.

Uses Kahn’s algorithm (via petgraph) to produce an ordering where all dependencies appear before their dependents.

§Returns
  • Ok(Vec<TaskId>) - Tasks in execution order
  • Err(WorkflowError::CycleDetected) - If graph contains a cycle
  • Err(WorkflowError::EmptyWorkflow) - If workflow has no tasks
Source

pub fn execution_layers(&self) -> Result<Vec<Vec<TaskId>>, WorkflowError>

Returns tasks grouped into parallel execution layers.

Tasks in the same layer have no dependencies between them and can execute concurrently. Tasks in layer N only depend on tasks in layers < N.

This uses the longest path distance from any root (task with in-degree = 0) to determine the layer. All tasks at distance 0 are independent roots, tasks at distance 1 depend only on roots, etc.

§Returns
  • Ok(Vec<Vec<TaskId>>) - Tasks grouped into layers, where each inner vec contains tasks that can execute in parallel
  • Err(WorkflowError::CycleDetected) - If graph contains a cycle
  • Err(WorkflowError::EmptyWorkflow) - If workflow has no tasks
§Example

For a diamond DAG (a -> b, a -> c, b -> d, c -> d), returns:

vec![
    vec!["a"],      // Layer 0: root task
    vec!["b", "c"], // Layer 1: independent tasks
    vec!["d"],      // Layer 2: depends on b and c
]
Source

pub fn task_ids(&self) -> Vec<TaskId>

Returns all task IDs in the workflow.

Source

pub fn task_count(&self) -> usize

Returns the number of tasks in the workflow.

Source

pub fn contains_task(&self, id: &TaskId) -> bool

Checks if a task ID exists in the workflow.

Source

pub fn task_dependencies(&self, id: &TaskId) -> Option<Vec<TaskId>>

Returns actual dependencies for a task (from graph edges).

Returns the task IDs that this task depends on, based on the actual graph edges rather than task metadata.

Source

pub fn task_name(&self, id: &TaskId) -> Option<String>

Returns the name of a task.

Source

pub fn apply_suggestions( &mut self, suggestions: Vec<DependencySuggestion>, ) -> Result<usize, WorkflowError>

Applies dependency suggestions to the workflow.

§Arguments
  • suggestions - Vector of dependency suggestions to apply
§Returns
  • Ok(usize) - Number of dependencies applied
  • Err(WorkflowError) - If a cycle is detected
§Note

Dependencies that already exist are skipped.

Source

pub fn preview_suggestions( &self, suggestions: &[DependencySuggestion], ) -> Vec<String>

Generates human-readable preview of dependency suggestions.

§Arguments
  • suggestions - Vector of dependency suggestions to preview
§Returns

Vector of human-readable strings describing each suggestion

Trait Implementations§

Source§

impl Default for Workflow

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl TryFrom<YamlWorkflow> for Workflow

Source§

type Error = YamlWorkflowError

The type returned in the event of a conversion error.
Source§

fn try_from(yaml_workflow: YamlWorkflow) -> Result<Self, Self::Error>

Performs the conversion.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more