rust-viewflow 0.1.0

Rust workflow library inspired by Viewflow, compatible with Axum and Actix-web
Documentation
use crate::api::{ApiResponse, CompleteTaskRequest, CreateWorkflowRequest, WorkflowApi};
use crate::core::{TaskState, WorkflowState};
use axum::{
    extract::{Json, Path},
    http::StatusCode,
    routing::{get, post},
    Router,
};
use std::sync::Arc;

/// Axum router for workflow API
pub fn create_router(api: Arc<dyn WorkflowApi>) -> Router {
    let api_state = api;

    Router::new()
        .route(
            "/workflows",
            post({
                let api = api_state.clone();
                move |Json(req): Json<CreateWorkflowRequest>| {
                    let api = api.clone();
                    async move {
                        match api.create_workflow(req).await {
                            Ok(workflow) => {
                                (StatusCode::CREATED, Json(ApiResponse::success(workflow)))
                            }
                            Err(e) => (
                                StatusCode::BAD_REQUEST,
                                Json(ApiResponse::<WorkflowState>::error(e.to_string())),
                            ),
                        }
                    }
                }
            }),
        )
        .route(
            "/workflows/:id",
            get({
                let api = api_state.clone();
                move |Path(id): Path<String>| {
                    let api = api.clone();
                    async move {
                        match api.get_workflow(&id).await {
                            Ok(workflow) => (StatusCode::OK, Json(ApiResponse::success(workflow))),
                            Err(e) => (
                                StatusCode::NOT_FOUND,
                                Json(ApiResponse::<WorkflowState>::error(e.to_string())),
                            ),
                        }
                    }
                }
            }),
        )
        .route(
            "/workflows/:id/tasks",
            get({
                let api = api_state.clone();
                move |Path(id): Path<String>| {
                    let api = api.clone();
                    async move {
                        match api.get_tasks(&id).await {
                            Ok(tasks) => (StatusCode::OK, Json(ApiResponse::success(tasks))),
                            Err(e) => (
                                StatusCode::NOT_FOUND,
                                Json(ApiResponse::<Vec<TaskState>>::error(e.to_string())),
                            ),
                        }
                    }
                }
            }),
        )
        .route(
            "/tasks/:id/complete",
            post({
                let api = api_state.clone();
                move |Path(id): Path<String>, Json(req): Json<CompleteTaskRequest>| {
                    let api = api.clone();
                    async move {
                        match api.complete_task(&id, req).await {
                            Ok(task) => (StatusCode::OK, Json(ApiResponse::success(task))),
                            Err(e) => (
                                StatusCode::BAD_REQUEST,
                                Json(ApiResponse::<TaskState>::error(e.to_string())),
                            ),
                        }
                    }
                }
            }),
        )
}