Module task

Module task 

Source
Expand description

Task definition and execution control.

This module contains the core Task trait and related types for defining workflow steps and controlling execution flow.

§Examples

§Basic Task Implementation

use graph_flow::{Task, TaskResult, NextAction, Context};
use async_trait::async_trait;

struct HelloTask;

#[async_trait]
impl Task for HelloTask {
    fn id(&self) -> &str {
        "hello_task"
    }

    async fn run(&self, context: Context) -> graph_flow::Result<TaskResult> {
        let name: String = context.get("name").await.unwrap_or("World".to_string());
        let greeting = format!("Hello, {}!", name);
         
        // Store result for next task
        context.set("greeting", greeting.clone()).await;
         
        Ok(TaskResult::new(Some(greeting), NextAction::Continue))
    }
}

§Task with Different Control Flow

struct ConditionalTask;

#[async_trait]
impl Task for ConditionalTask {
    fn id(&self) -> &str {
        "conditional_task"
    }

    async fn run(&self, context: Context) -> graph_flow::Result<TaskResult> {
        let user_input: Option<String> = context.get("user_input").await;
         
        match user_input {
            Some(input) if !input.is_empty() => {
                // Process input and continue automatically
                context.set("processed", input.to_uppercase()).await;
                Ok(TaskResult::new(
                    Some("Input processed".to_string()),
                    NextAction::ContinueAndExecute
                ))
            }
            _ => {
                // Wait for user input
                Ok(TaskResult::new(
                    Some("Please provide input".to_string()),
                    NextAction::WaitForInput
                ))
            }
        }
    }
}

Structs§

TaskResult
Result of a task execution.

Enums§

NextAction
Defines what should happen after a task completes.

Traits§

Task
Core trait that all tasks must implement.