
M05 Type Driven
Steer Rust modeling toward compile-time invariants with type state, newtypes, and sealed traits instead of piling on runtime checks.
Overview
m05-type-driven is an agent skill most often used in Build (also Ship review, Validate scope) that pushes Rust designs to encode invariants in types so invalid states are unrepresentable at compile time.
Install
npx skills add https://github.com/actionbook/rust-skills --skill m05-type-drivenWhat is this skill?
- Error-to-design-question table for primitive obsession, boolean flags, and optional sprawl
- Type state, newtypes, PhantomData, and marker traits to make invalid states unrepresentable
- Builder pattern and construction-time validation guidance
- Sealed traits and ZST patterns for API boundaries
- Bilingual triggers including 类型状态 and 类型驱动设计 for discovery
Adoption & trust: 958 installs on skills.sh; 1.2k GitHub stars; 3/3 security scanners passed (skills.sh audits).
What problem does it solve?
Your Rust model uses strings, flags, and runtime Err checks for rules the compiler could enforce, so bugs slip through until tests or production.
Who is it for?
Intermediate-to-advanced Rust solo builders designing APIs, domain models, or state machines who want fewer runtime guards.
Skip if: Greenfield teams that only need serde DTOs with no invariants, or problems that genuinely require dynamic validation only known at runtime.
When should I use this skill?
CRITICAL: Use for type-driven design when work involves type state, PhantomData, newtype, marker traits, builder patterns, sealed traits, ZST, or making invalid states unrepresentable.
What do I get? / Deliverables
You refactor toward newtypes, type state, sealed traits, and construction-time validation so invalid transitions fail at compile time with clearer APIs.
- Refactored Rust types with compile-time state and domain boundaries
- API signatures that document invariants without extra runtime checks
Recommended Skills
Journey fit
Spans multiple journey phases - primary shelf plus alternate fits below.
Type-driven design lands first when you shape domain models and APIs during implementation—the canonical Build shelf for Rust backend work. Backend Rust code benefits most from encoding states and semantics in the type system before features ship.
Where it fits
Name domain states as types in a spec so the implementation plan does not default to stringly IDs and bool flags.
Replace ad-hoc string statuses with type-state transitions so illegal workflow jumps do not compile.
Review a PR for primitive obsession and suggest newtypes or sealed traits before merge.
Refactor hot paths that rely on runtime Ok/Err guards into construction-validated types after an incident postmortem.
How it compares
Type-level design playbook for Rust, not a linter rule pack or generic brainstorming skill.
Common Questions / FAQ
Who is m05-type-driven for?
Rust-focused solo builders and coding agents who want compile-time guarantees via newtypes, type state, and sealed traits instead of pervasive runtime validation.
When should I use m05-type-driven?
In Build when modeling backend types; in Validate when scoping domain states in a spec; in Ship review when flagging primitive obsession or boolean state flags; whenever triggers mention type state, PhantomData, or making invalid states unrepresentable.
Is m05-type-driven safe to install?
It is procedural Rust guidance with no bundled executables; still review the Security Audits panel on this page before adding skills from third-party repos to your agent.
SKILL.md
READMESKILL.md - M05 Type Driven
# Type-Driven Design > **Layer 1: Language Mechanics** ## Core Question **How can the type system prevent invalid states?** Before reaching for runtime checks: - Can the compiler catch this error? - Can invalid states be unrepresentable? - Can the type encode the invariant? --- ## Error → Design Question | Pattern | Don't Just Say | Ask Instead | |---------|----------------|-------------| | Primitive obsession | "It's just a string" | What does this value represent? | | Boolean flags | "Add an is_valid flag" | Can states be types? | | Optional everywhere | "Check for None" | Is absence really possible? | | Validation at runtime | "Return Err if invalid" | Can we validate at construction? | --- ## Thinking Prompt Before adding runtime validation: 1. **Can the type encode the constraint?** - Numeric range → bounded types or newtypes - Valid states → type state pattern - Semantic meaning → newtype 2. **When is validation possible?** - At construction → validated newtype - At state transition → type state - Only at runtime → Result with clear error 3. **Who needs to know the invariant?** - Compiler → type-level encoding - API users → clear type signatures - Runtime only → documentation --- ## Trace Up ↑ When type design is unclear: ``` "Need to validate email format" ↑ Ask: Is this a domain value object? ↑ Check: m09-domain (Email as Value Object) ↑ Check: domain-* (validation requirements) ``` | Situation | Trace To | Question | |-----------|----------|----------| | What types to create | m09-domain | What's the domain model? | | State machine design | m09-domain | What are valid transitions? | | Marker trait usage | m04-zero-cost | Static or dynamic dispatch? | --- ## Trace Down ↓ From design to implementation: ``` "Need type-safe wrapper for primitives" ↓ Newtype: struct UserId(u64); "Need compile-time state validation" ↓ Type State: Connection<Connected> "Need to track phantom type parameters" ↓ PhantomData: PhantomData<T> "Need capability markers" ↓ Marker Trait: trait Validated {} "Need gradual construction" ↓ Builder: Builder::new().field(x).build() ``` --- ## Quick Reference | Pattern | Purpose | Example | |---------|---------|---------| | Newtype | Type safety | `struct UserId(u64);` | | Type State | State machine | `Connection<Connected>` | | PhantomData | Variance/lifetime | `PhantomData<&'a T>` | | Marker Trait | Capability flag | `trait Validated {}` | | Builder | Gradual construction | `Builder::new().name("x").build()` | | Sealed Trait | Prevent external impl | `mod private { pub trait Sealed {} }` | ## Pattern Examples ### Newtype ```rust struct Email(String); // Not just any string impl Email { pub fn new(s: &str) -> Result<Self, ValidationError> { // Validate once, trust forever validate_email(s)?; Ok(Self(s.to_string())) } } ``` ### Type State ```rust struct Connection<State>(TcpStream, PhantomData<State>); struct Disconnected; struct Connected; struct Authenticated; impl Connection<Disconnected> { fn connect(self) -> Connection<Connected> { ... } } impl Connection<Connected> { fn authenticate(self) -> Connection<Authenticated> { ... } } ``` --- ## Decision Guide | Need | Pattern | |------|---------| | Type safety for primitives | Newtype | | Compile-time state validation | Type State | | Lifetime/variance markers | PhantomData | | Capability flags | Marker Trait | | Gradual construction | Builder | | Closed set of impls | Sealed Trait | | Zero-sized type marker | ZST struct | --- ## Anti-Patterns | Anti-Pattern | Why Bad | Better | |--------------|---------|--------| | Boolean flags for states