rust-viewflow 0.1.0

Rust workflow library inspired by Viewflow, compatible with Axum and Actix-web
Documentation
#[cfg(feature = "actix")]
use std::sync::Arc;

#[cfg(feature = "actix")]
use actix_web::{web, HttpResponse, Scope};

#[cfg(feature = "actix")]
use crate::api::{ApiResponse, CompleteTaskRequest, CreateWorkflowRequest, WorkflowApi};
#[cfg(feature = "actix")]
use crate::core::{TaskState, WorkflowState};

#[cfg(feature = "actix")]
type ApiState = web::Data<Arc<dyn WorkflowApi>>;

#[cfg(feature = "actix")]
async fn create_workflow(req: web::Json<CreateWorkflowRequest>, api: ApiState) -> HttpResponse {
    match api.create_workflow(req.into_inner()).await {
        Ok(workflow) => HttpResponse::Created().json(ApiResponse::success(workflow)),
        Err(e) => {
            HttpResponse::BadRequest().json(ApiResponse::<WorkflowState>::error(e.to_string()))
        }
    }
}

#[cfg(feature = "actix")]
async fn get_workflow(path: web::Path<String>, api: ApiState) -> HttpResponse {
    let id = path.into_inner();
    match api.get_workflow(&id).await {
        Ok(workflow) => HttpResponse::Ok().json(ApiResponse::success(workflow)),
        Err(e) => HttpResponse::NotFound().json(ApiResponse::<WorkflowState>::error(e.to_string())),
    }
}

#[cfg(feature = "actix")]
async fn get_tasks(path: web::Path<String>, api: ApiState) -> HttpResponse {
    let id = path.into_inner();
    match api.get_tasks(&id).await {
        Ok(tasks) => HttpResponse::Ok().json(ApiResponse::success(tasks)),
        Err(e) => {
            HttpResponse::NotFound().json(ApiResponse::<Vec<TaskState>>::error(e.to_string()))
        }
    }
}

#[cfg(feature = "actix")]
async fn complete_task(
    path: web::Path<String>,
    req: web::Json<CompleteTaskRequest>,
    api: ApiState,
) -> HttpResponse {
    let id = path.into_inner();
    match api.complete_task(&id, req.into_inner()).await {
        Ok(task) => HttpResponse::Ok().json(ApiResponse::success(task)),
        Err(e) => HttpResponse::BadRequest().json(ApiResponse::<TaskState>::error(e.to_string())),
    }
}

#[cfg(feature = "actix")]
pub fn workflow_scope(api: Arc<dyn WorkflowApi>) -> Scope {
    web::scope("")
        .app_data(web::Data::from(api))
        .route("/workflows", web::post().to(create_workflow))
        .route("/workflows/{id}", web::get().to(get_workflow))
        .route("/workflows/{id}/tasks", web::get().to(get_tasks))
        .route("/tasks/{id}/complete", web::post().to(complete_task))
}