Rust axum service

From makifbaysal/tasktrooper · View interactive version

Rust axum service is an AI agent skill from the GitHub repository makifbaysal/tasktrooper. Use when building or extending a Rust service - axum on tokio (or the repository's actix-web / rocket), typed errors with IntoResponse, serde at the boundary, sqlx queries and migrations, never blocking the runtime, tracing, clippy

How to use this skill

Skill file

Rust Service (axum on tokio)

Overview

Rust moves a class of bugs to compile time, and leaves two to you: panics on a request path and blocking the async runtime. Most of the rules below are about those two.

Core principle: the repository's framework, error crates, database crate and workspace layout win. Handlers stay thin; the domain is plain Rust that compiles without the web framework.

Layers

| Layer | Responsibility | axum surface |

|-------|----------------|--------------|

| Router / handler | Extract and validate input, call one service, map the result | Router::new().route(...), extractors (State, Path, Json), impl IntoResponse |

| Service | Use-case orchestration and business rules | a struct holding its ports, async fn methods |

| Domain | Types and invariants: newtypes whose constructors validate | plain structs and enums, thiserror error enum |

| Adapter | Database, outbound HTTP, queues | a type implementing the port trait (sqlx, reqwest) |

In a workspace the domain is often its own crate; a crate that does not depend on axum cannot leak it.

Patterns

Worked Example

POST /tasks:

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
pub struct CreateTask { title: String }

pub async fn create_task(
    State(state): State<AppState>,
    user: AuthUser,
    Json(body): Json<CreateTask>,
) -> Result<(StatusCode, Json<TaskResponse>), AppError> {
    let title = TaskTitle::try_from(body.title)?;            // 400 through AppError on a bad title
    let task = state.tasks.create(user.id, title).await?;    // the service owns the rule set
    Ok((StatusCode::CREATED, Json(TaskResponse::from(task))))
}

TaskTitle::try_from trims and rejects empty or over-200-character titles, so no Task with an invalid title can exist anywhere in the program.

Verify

The repository's commands first (list_component_checks, the Makefile, CI). Otherwise: cargo fmt --check, cargo clippy --all-targets --all-features -- -D warnings, cargo test. For the runtime check build once — cargo build --release (or the workspace's -p <api>) — and start target/release/<api> in the background with its port and DATABASE_URL set, rather than cargo run, whose child a terminal timeout can orphan.

Common Mistakes

Red Flags

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.