# Design Safer Rust APIs with Types

Rust APIs can allow invalid states or swapped values when invariants stay in runtime logic. This skill shows how to encode invariants with newtypes, typestate, builders, phantom types, and session types.

## Install

```bash
npx skillstore add emillindfors/type-driven-design
```

## Metadata

- Status: approved
- Slug: emillindfors-type-driven-design
- Skillstore revision: r1
- Version status: missing
- Tree hash: dc762a1e5f6dcafb63ccc79330d29b1b8521abc638ca4714318a5609220b2acc
- Author: EmilLindfors
- GitHub username: EmilLindfors
- License: MIT
- Repository: https://github.com/EmilLindfors/claude-marketplace/tree/main/plugins/rust-modern-patterns/skills/type-driven-design
- Ref: a06681402992ceae98ba04d54cfd4ab004862696
- Supported tools: Claude, Codex, Claude Code
- Audit status: complete
- Agent install advisory: allowed
- Manual install advisory: allowed
- Artifact signature: available
- Audit attestation: unavailable
- Human verification: not\_verified
- Risk factors: external\_commands, network
- Quality score: 77
- Quality tier: bronze
- Public page: https://skillstore.pages.dev/skills/emillindfors-type-driven-design
- Manifest: https://skillstore.pages.dev/api/skills/emillindfors-type-driven-design/manifest

## Capabilities

- Explains newtype wrappers for domain identifiers, units, validated values, and positive numbers.
- Shows typestate examples for connection and request workflows.
- Demonstrates builder patterns that require fields at compile time.
- Covers phantom types for type-safe identifiers and relationships.
- Introduces session types for authentication and transaction protocols.
- Provides trade-offs for when type-driven design is useful.

## Use Cases

- Prevent API Misuse: Design library APIs that make invalid method order and missing required values impossible to compile.
- Model Domain Invariants: Represent identifiers, units, and validated values with distinct types instead of shared primitive values.
- Teach Safer Rust Patterns: Explain type-level techniques with before-and-after examples that show compile-time guarantees.

## Prompt Templates

### Model a Newtype

```
I have [value type] used for [domain meaning]. Show a Rust newtype design that prevents value mix-ups, with constructors and usage examples.
```

### Add Typestate to a Workflow

```
Refactor this Rust workflow into a typestate API. The states are [states], and the allowed transitions are [transitions]. Explain the compile-time guarantees.
```

### Build a Compile-Time Builder

```
Design a Rust builder for [configuration object] where [required fields] must be set before build is available. Include optional fields and trade-offs.
```

### Design a Session Protocol

```
Create a session-type design for [protocol]. The protocol must enforce [authentication, transaction, cleanup, or other steps] at compile time.
```

## Limitations

- Focuses on Rust examples and does not provide language-agnostic implementations.
- Uses illustrative snippets that may need imports, real error types, and project-specific validation.
- Does not run compilers or test generated code by itself.
- May add API complexity when state spaces are highly dynamic.

## Best Practices

- Start with invariants that cause real bugs when they are only checked at runtime.
- Keep marker types and constructors small so the public API stays readable.
- Explain compile-time failures clearly so callers understand how to reach valid states.

## Anti Patterns

- Do not use typestate for every boolean or minor validation rule.
- Do not expose unchecked constructors that bypass the invariant encoded by the type.
- Do not hide complex generic errors from users without examples or type aliases.

## Security Audit

- Audited at: 2026-07-06T15:35:56.066\+00:00
- Summary: All static findings were adjudicated as false positives. The backtick detections are Markdown code fences, the URLs are examples or documentation links, and the reconnaissance matches are benign Rust type-design text.

## Stats

- Views: 150
- Downloads: 10
- Favorites: 0
- Popularity score: 0
